在Java的世界里,数据类型分为基本类型(值类型)和包装类型(引用类型),值类型不是对象,不能调用Object对象提供的方法,如toString()、hashCode()等。
C#、Python、Go等也有类似值类型和引用类型的概念
JAVA的引用类型有两类:Class和Interface。网上有些把数组(Array)也算一类,个人认为数组(Array)是Class的一个具体形式,应该归属于Class;当然数组是一个比较特殊的对象,其class带有“[“这种特殊符号,单独算一类也说得通,我们今天的重点并不是数组这个类型,就不在细究。
通俗比喻:值类型就是现金,要用直接用;引用类型是存折,要用还得先去银行取现。—-(摘自网上)
注: 1字节(byte)=8位(bit),1B=8b。 注: 263=9223372036854774808
分类基本类型引用类型取值范围字节长度整型byteByte-128~1271字节整型shortShort-32768~327672字节整型intInteger-2147483648~21474836474字节整型longLong −263 ~ 263−1 8字节浮点型floatFloat 2−149 ~ (2−2−23)∗2127 4字节浮点型doubleDouble 2−1074 ~ 21024−1 8字节字符型charCharacter整个Unicode字符集2字节逻辑型booleanBooleantrue~ false1位byte初始值为0,Byte初始值为null;
byte b1 = 127; Byte b2 = 127; Byte b3 = new Byte("127"); System.out.println(b1 == b2);//b2自动拆箱为byte,所以为true; System.out.println(b1 == b3);//同上 Byte b5 = 127;//被编译成-> Byte b5 = Byte.valueOf(127); Byte b6 = 127; System.out.println(b5 == b6);//byte值范围内Byte.valueOf会进行引用缓存,所以为true。 Byte b7 = new Byte("127"); System.out.println(b6 == b7);//不同引用,所以为false。 Byte b8 = new Byte("127"); Byte b9 = new Byte("127"); System.out.println(b8 == b9);//不同引用,所以为false。short初始值为0,Short初始值为null;
short s1 = 128; Short s2 = 128; Short s3 = new Short("128"); System.out.println(s1 == s2);//s2自动拆箱为short,所以为true; System.out.println(s1 == s3);//同上 Short s5 = 127;//被编译成-> Short s5 = Short.valueOf(127); Short s6 = 127; System.out.println(s5 == s6);//byte值范围内Short.valueOf会进行引用缓存,所以为true。 Short s7 = new Short("127"); System.out.println(s6 == s7);//不同引用,所以为false。 Short s8 = 128; Short s9 = 128; System.out.println(s8 == s9);//128超出byte值范围,所以为false。 Short s10 = new Short("128"); Short s11 = new Short("128"); System.out.println(s10 == s11);//不同引用,所以为false。int初始值为0,Integer初始值为null;
int i1 = 128; Integer i2 = 128; Integer i3 = new Integer(128); System.out.println(i1 == i2);//i2自动拆箱为int,所以为true; System.out.println(i1 == i3);//同上 Integer i5 = 127;//被编译成-> Integer i5 = Integer.valueOf(127); Integer i6 = 127; System.out.println(i5 == i6);//byte值范围内Integer.valueOf会进行引用缓存,所以为true。 Integer i7 = new Integer(127); System.out.println(i6 == i7);//不同引用,所以为false。 Integer i8 = 128; Integer i9 = 128; System.out.println(i8 == i9);//128超出byte值范围,所以为false。 Integer i10 = new Integer(128); Integer i11 = new Integer(128); System.out.println(i10 == i11);//不同引用,所以为false。long初始值为0l,Long初始值为null;
long l1 = 128; Long l2 = 128l; Long l3 = new Long(128); System.out.println(l1 == l2);//l2自动拆箱为long,所以为true; System.out.println(l1 == l3);//同上 Long l5 = 127l;//被编译成-> Long l5 = Long.valueOf(127); Long l6 = 127l; System.out.println(l5 == l6);//byte值范围内Long.valueOf会进行引用缓存,所以为true。 Long l7 = new Long(127); System.out.println(l6 == l7);//不同引用,所以为false。 Long l8 = 128l; Long l9 = 128l; System.out.println(l8 == l9);//128超出byte值范围,所以为false。 Long l10 = new Long(128); Long l11 = new Long(128); System.out.println(l10 == l11);//不同引用,所以为false。浮点型和整型不同,没有内置缓存机制,所以除了有值类型存在,否则是无法”==”比较。
字符型和整型类似,内部创建了一个缓存,不过只有128个长度,从0开始的Character对象。
boolean初始值为false,Boolean初始值为null;
boolean b1 = true; Boolean b2 = true; Boolean b3 = new Boolean(true); System.out.println(b1 == b2);//b2自动拆箱为boolean,所以为true; System.out.println(b1 == b3);//同上 Boolean b5 = true;//被编译成-> Boolean b5 = Boolean.valueOf(true); Boolean b6 = true; System.out.println(b5 == b6);//直接赋值进行比较为true。 Boolean b7 = new Boolean(true); System.out.println(b6 == b7);//不同引用,所以为false。 Boolean b8 = new Boolean(true); Boolean b9 = new Boolean(true); System.out.println(b8 == b9);//不同引用,所以为false。逻辑型和浮点型型的规则类似,没有内置缓存机制,所以除了有值类型存在,否则是无法”==”比较 =============================悬崖================================ 如有理解不到位欢迎大家指正,欢迎讨论!!!