黄石网站建设维护,自己做网站销售,创建一个网站流程,网站建设申请方案文样一般地#xff0c;当需要使用数字的时候#xff0c;我们通常使用内置数据类型#xff0c;如#xff1a;byte、int、long、double 等。然而#xff0c;在实际开发过程中#xff0c;我们经常会遇到需要使用对象#xff0c;而不是内置数据类型的情形。为了解决这个问题当需要使用数字的时候我们通常使用内置数据类型如byte、int、long、double 等。然而在实际开发过程中我们经常会遇到需要使用对象而不是内置数据类型的情形。为了解决这个问题Java 语言为每一个内置数据类型提供了对应的包装类。所以Java语言预定义了8种包装类型:byte - Byteshort - Shortint - Integerlong - Longfloat - Floatdouble - Doublechar - Characterboolean - Boolean例如:Integer.toBinaryString()可得到一个整数的二进制代码Integer.toHexString()可得到一个整数的十六进制代码Integer.parseInt()可将一个String转换成intInteger.toString()可将一个int转换成String字符串和数值类型的转换字符串 - 数值: XXX.parseXXX(), 如Integer.parseInt()数值 - 字符串: toString(), String.valueOf()java中的自动装箱与拆箱/*** author YY哥哥* java中的自动装箱与拆箱* 简单一点说装箱就是自动将基本数据类型转换为包装器类型拆箱就是自动将包装器类型转换为基本数据类型。*/public class TestDemo { public static void main(String[] args) { /** Integer x 128; // 装箱相当于 Integer.valueOf(128); int y x; //相当于 x.intValue() 拆箱 System.out.println(y); */ /** 对于–128到127(默认是127)之间的值,被装箱后会被放在内存里进行重用 但是如果超出了这个值,系统会重新new 一个对象 */ Integer x 200; Integer y 200; /** 注意 与 equals的区别 它比较的是对象的地址 equals 比较的是对象的内容 */ if(xy) { System.out.println(true); } else { System.out.println(false); } }}Java 会对 -128 ~ 127 的整数进行缓存所以当定义两个变量初始化值位于 -128 ~ 127 之间时两个变量使用了同一地址Integer a123;Integer b123;System.out.println(ab); // 输出 trueSystem.out.println(a.equals(b)); // 输出 true当两个 Integer 变量的数值超出 -128 ~ 127 范围时, 变量使用了不同地址a1230;b1230;System.out.println(ab); // 输出 falseSystem.out.println(a.equals(b)); // 输出 trueJava 中 int 和 Integer 的区别1. int 是基本数据类型int 变量存储的是数值。Integer 是引用类型实际是一个对象Integer 存储的是引用对象的地址。Integer x new Integer(100);Integer y new Integer(100);System.out.print(x y); //false解释因为 new 生成的是两个对象其内存地址不同。2. int 和 Integer 所占内存比较Integer 对象会占用更多的内存。Integer 是一个对象需要存储对象的元数据。但是 int 是一个原始类型的数据所以占用的空间更少。3. 非 new 生成的 Integer 变量与 new Integer() 生成的变量比较结果为 false。/** * 比较非new生成的Integer变量与new生成的Integer变量 */public class TestDemo { public static void main(String[] args) { Integer i new Integer(200); Integer j 200; System.out.print(i j); //输出false }}解释非 new 生成的 Integer 变量指向的是 java 常量池中的对象而 new Integer() 生成的变量指向堆中新建的对象两者在内存中的地址不同。所以输出为 false。4. 两个非 new 生成的 Integer 对象进行比较如果两个变量的值在区间 [-128,127] 之间比较结果为 true否则结果为 false。/** * 比较两个非new生成的Integer变量 */public class TestDemo { public static void main(String[] args) { Integer x1 127; Integer y1 127; System.out.println(x1 y1);//输出true Integer x2 128; Integer y2 128; System.out.println(x2 y2);//输出false }}解释java 在编译 Integer x1 127 时会翻译成 Integer x1 Integer.valueOf(127)。5. Integer 变量(无论是否是 new 生成的)与 int 变量比较只要两个变量的值是相等的结果都为 true。/** * 比较Integer变量与int变量 */public class TestDemo { public static void main(String[] args) { Integer x1 200; Integer x2 new Integer(200); int y 200; System.out.println(x1 y);//输出true System.out.println(x2 y);//输出true }}解释包装类 Integer 变量在与基本数据类型 int 变量比较时Integer 会自动拆包装为 int然后进行比较实际上就是两个 int 变量进行比较值相等所以为 true。谢谢大家关注