2022.4.22 常用类 包装类、Biglnteger、BigDecimal

包装类

基本数据类型的包装类

基本数据类型大小包装类
byte 1B Byte
short 2B Short
int 4B Integer
long 8B Long
float 4B Float
double 8B Double
char 2B Character
boolean 1b Boolean

Integer

Integer类包装一个对象中的原始类型int的值。类型为Integer的对象包含一个单一字段,其类型为int 。

此外,该类还提供了一些将int转换为String和String转换为int,以及在处理int时有用的其他常量和方法。

属性与构造方法

 1 package com.xing.Integer;
 2 
 3 public class Demo01 {
 4     public static void main(String[] args) {
 5         System.out.println(Integer.MAX_VALUE);//最大值
 6         System.out.println(Integer.MIN_VALUE);//最小值
 7 
 8         Integer i1 = new Integer(90);
 9         System.out.println(i1);//90  默认调用toString   打印value值
10         System.out.println(i1.toString());
11         /*
12         public String toString() {
13             return toString(value);
14         }
15         */
16 
17         Integer i2 = new Integer("99");
18         System.out.println(i2);//99
19         System.out.println(i2.toString());
20 
21         //构造器中传递的字符串只能是数字字符串,否则报错
22         Integer i3 = new Integer("abc");
23         System.out.println(i3);
24 
25         Integer a = -12;
26         Integer b = -12;
27         Integer c = 235;
28         Integer d = 235;
29 
30         System.out.println(a.equals(b));//true
31         System.out.println(a == b);//true
32         System.out.println(c.equals(d));//true
33         System.out.println(c == d);//false
34 
35 
36 
37     }
38 }

 

Integer值的比较有个坑:对于Integer var = ?,在-128至127范围内的赋值, Integer 对象是在IntegerCache.cache 产生,会复用已有对象,这个区间内的 Integer 值可以直接使用进行判断,但是这个区间之外的所有数据,都会在堆上产生,并不会复用已有对象;所以,在上面,我们的c和d两个,虽然值是一样的,但是地址不一样。这是一个大坑,很多人会在项目中使用==来比较Integer!强烈建议,必须使用equals来比较!

String类型与int类型转换

 1 package com.xing.Integer;
 2 
 3 public class Demo02 {
 4     public static void main(String[] args) {
 5         //int 转换为String
 6         int i = 10;
 7         String s1 = i + "";//法一
 8         String s2 = String.valueOf(i);//法二
 9         String s3 = Integer.toString(i);//法三
10 
11         //String 转换为int
12         String str = "666";
13         //将字符串参数解析为带符号的十进制整数。
14         int x = Integer.parseInt(str);//法一
15 
16         //法二
17         Integer integer1 = new Integer(str);
18         int y = integer1.intValue();//将Integer转换为int类型  
19 
20         //int y1 = new Integer(str).intValue();//与上面同理
21         
22         //法三
23         Integer integer2 = Integer.valueOf(str);//将String转换为Integer类型
24         int z = integer2.intValue();//将Integer转换为int类型
25 
26     }
27 }

 

Integer进制转换

 1 package com.xing.Integer;
 2 
 3 public class Demo03 {
 4     public static void main(String[] args) {
 5         int i = 10;
 6         System.out.println(Integer.toBinaryString(i));//转换为2进制
 7         System.out.println(Integer.toOctalString(i));//转换为8进制
 8         System.out.println(Integer.toHexString(i));//转换为16进制
 9         
10         //其他进制转换为10进制
11         System.out.println(Integer.parseInt("100",8));//将100转换为8进制
12         System.out.println(Integer.parseInt("zz",36));//将zz转换为36进制
13         
14     }
15 }

自动装箱和自动拆箱

可以将包装类当做基本数据类型使用,反之亦然

自动装箱:将基本数据类型转换成对应的包装类类型Integer int1 = new Integer.valueOf(100);

自动拆箱:将包装类转换为对应的基本数据类型int1.intValue();

 1 package com.xing.Integer;
 2 
 3 public class Demo04 {
 4     public static void main(String[] args) {
 5         int i = 100;
 6         Integer integer = new Integer(i);//标准的装箱
 7 
 8         Integer integer1 = 100; //相当于 Integer integer1 = Integer.valueOf(100);   自动装箱
 9         System.out.println(integer1);//有一个拆箱操作:integer1 = integer1.intValue();   
10     }
11 }

Biglnteger

对于超出int范围的数据进行运算

 1 package com.xing.Math;
 2 
 3 import java.math.BigDecimal;
 4 import java.math.BigInteger;
 5 
 6 public class Demo03 {
 7     public static void main(String[] args) {
 8         System.out.println(2147483647+1);//-2147483648
 9         
10         BigInteger b1 = new BigInteger("2147483647");//里面字符串
11         BigInteger b2 = new BigInteger("1");//里面字符串
12         System.out.println(b1.add(b2));//b1+b2  2147483648
13 
14     }
15 }

BigDecimal

float和double在计算的时候很容易出现精度丢失的情况,Java设计了BigDecimal,不可变的,任意精度的有符号十进制数

 1 package com.xing.Math;
 2 
 3 import java.math.BigDecimal;
 4 
 5 public class Demo03 {
 6     public static void main(String[] args) {
 7         System.out.println(0.01+0.09);//0.09999999999999999
 8 
 9         BigDecimal b1 = new BigDecimal("0.01");//里面是字符串
10         BigDecimal b2 = new BigDecimal("0.09");
11         System.out.println(b1.add(b2));//b1+b2  0.10
12         System.out.println(b1.multiply(b2));//b1*b2
13         System.out.println(b1.divide(b2));//b1/b2   除不尽不行报错
14         System.out.println(b1.subtract(b2));//b1-b2
15     }
16 }

 

 

 

posted @ 2022-04-25 13:11  暴躁C语言  阅读(83)  评论(0)    收藏  举报