前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java 包装类

Java 包装类

作者头像
Mirror王宇阳
发布2020-11-10 22:08:54
4950
发布2020-11-10 22:08:54
举报
文章被收录于专栏:Mirror的技术成长

Java 基本数据的包装类

基本数据类型包装类的定义:

Java在设计之初的设计原则就是:“一切皆对象”,一切的操作都要求用对象的形式进行表述。

class MyInt { // 一个类 private int num ; //类包装的基本数据 public MyInt(int num) { this.num = num ; } public int intValue() { //将类包装的数据内容返回 >>> [拆包] return this.num ; } } public class TestDemo { public static void main(String [] args) { MyInt mi = new MyInt(10) ; // 将 int 包装为类 int temp = mi.intValue() ; // 将类对象中包装的数据取出 System.out.println( temp * 2 ) ; } }

Java为了方便用户使用,专门提供了一组包装类;

基本类型:byde(Byte) short(Short) int(Integer) long(Long) float(Float) double(Double) char(Character) Boolean(Boolean)

对象型包装类(Object直接子类):Character , Boolean

数值型包装类(Number直接子类):Byte , Short , Integer , Long , Float

Number是一个抽象类,里面一共定义了六个操作方法:

IntValue() , doubleValue() , floatValue() , longValue() , byteValue() , shortValue()

装箱与拆箱操作

····【JDK 1.5版本】

·装箱操作:将基本数据类型变为包装类的形式;

每个包装类的构造方法,都可以接收各自的数据类型的变量

·拆箱操作:从包装类当中取出被包装的数据;

利用Number类中提供的一系列: xxxValue()方法完成

public class TestDemo { public static void main(String [] args) { Integer obj = new Integer(10) ; //将基本类型装箱 int temp = obj.intValue() ; // 将基本数据类型拆箱 System.out.println( temp * 2 ) ; } }//将上一个MyInt类,变为了Integer系统包装类

public class TestDemo { public static void main(String [] args) { Boolean obj = new Boolean(10) ; //将基本类型装箱 boolean temp = obj.Boolean() ; // 将基本数据类型拆箱 System.out.println( temp ) ; } }// 即使不是Number子类的方法,但是也在Object中沿用了这个 装箱和拆箱的形式

···【JDK 1.5+版本】

Java在1.5+后加入了自动装箱和自动拆箱的机制,并且可以直接使用包装类的对象进行数学计算。

public class TestDemo { public static void main(String [] args) { Integer obj = 10 ; //自动装箱 int temp = obj ; // 自动拆箱 obj ++ ; // 包装类直接进行数学计算 System.out.println( temp * obj ) ; } }//使用自动装箱、拆箱和数学计算,就省去了手工拆箱的部分

两者不同:

public class TestDemo { public static void main(String [] args) { Integer obja = 10 ; Integer objb = 10 ; Integer objc = new Integer(10) ; System.out.println(obja == objb) ; // true System.out.println(obja == objc) ; // falue System.out.println(objb == objc) ; // falue System.out.println(obja.equals(objc)) ; // true } }

~

在使用包装类的时候,很少使用构造方法完成,几乎全部使用直接赋值(与String相同),但是在判断内容是否相等的时侯,一定要使用.equals()方法。

Object可以接收一切的引用数据类型……

public class TestDemo { public static void main(String [] args) { Object obj = 10 ; //先包装再转换 //Object不可以直接向下转型为int int temp = (Integer) obj ; //向下变为Integer后,再执行自动拆箱 System.out.println( temp * 2 ) ; } }

基本数据类型 —— 自动装箱(成为对象) —— 向上转型为Object

数据类型转换(核心):

在包装类中提供将String类转换为普通数据类型的方法

·Integer类:public static int parseInt(String s)

·Double类:public static double parseDouble(String s)

·Boolean类:public static boolean parseBoolean(String s)

~~Character类中并不存在String变为字符的方法,因为String中有一个chatAt()的方法根据索引取出字符内容。

public class TestDemo { public static void main(String [] args) { String str = "123" ; //字符串型 int temp = Integer.parseInt(str) ; //使用parseInt将str转为int型 System.out.println( temp * 2 ) ; } }

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-05-12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档