黑马程序员-----基本数据类型对象包装类

it2022-05-05  112

------- android培训、java培训、期待与您交流! ----------

黑马程序员-----基本数据类型对象包装类

1.1 基本数据类型对象包装类。

byte Byteshort shortint Integerlong Longboolean Booleanfloat Floatdouble Doublechar Character

 

基本数据类型对象包装类的最常见作用,就是用于基本数据类型和字符串类型之间做转换

基本数据类型转成字符串。

基本数据类型+""

基本数据类型.toString(基本数据类型值);

如: Integer.toString(34);//将34整数变成"34";

字符串转成基本数据类型。

xxx a = Xxx.parseXxx(String);

int a = Integer.parseInt("123");

double b = Double.parseDouble("12.23");

boolean b = Boolean.parseBoolean("true");

Integer i = new Integer("123");

int num = i.intValue();

 

十进制转成其他进制。 toBinaryString(); toHexString(); toOctalString();

其他进制转成十进制。 parseInt(string,radix);

 

示例1:

1 class IntegerDemo 2 { 3 public static void sop(String str) 4 { 5 System.out.println(str); 6 } 7 8 public static void main(String[] args) 9 { 10 //整数类型的最大值。 11 //sop("int max :"+Integer.MAX_VALUE); 12 13 // 将一个字符串转成整数。 14 15 int num = Integer.parseInt("123");//必须传入数字格式的字符串。 16 //long x = Long.parseLong("123"); 17 18 // sop("num="+(num+4)); 19 20 // sop(Integer.toBinaryString(-6)); 21 // sop(Integer.toHexString(60)); 22 23 int x = Integer.parseInt("3c",16); 24 25 sop("x="+x); 26 27 28 } 29 }

 

1.2 JDK1.5版本以后出现的新特性。

示例2:

1 class IntegerDemo1 2 { 3 public static void main(String[] args) 4 { 5 6 // Integer x = new Integer(4); 7 8 Integer x = 4;//自动装箱。//new Integer(4) 9 10 x = x/* x.intValue() */ + 2;//x+2:x 进行自动拆箱。变成成了int类型。和2进行加法运算。 11 //再将和进行装箱赋给x。 12 13 14 15 Integer m = 128; 16 Integer n = 128; 17 18 sop("m==n:"+(m==n)); 19 20 Integer a = 127; 21 Integer b = 127; 22 23 sop("a==b:"+(a==b));//结果为true。因为a和b指向了同一个Integer对象。 24 //因为当数值在byte范围内容,对于新特性,如果该数值已经存在,则不会在开辟新的空间。 25 } 26 27 public static void method() 28 { 29 Integer x = new Integer("123"); 30 31 Integer y = new Integer(123); 32 33 sop("x==y:"+(x==y)); 34 sop("x.equals(y):"+x.equals(y)); 35 } 36 37 public static void sop(String str) 38 { 39 System.out.println(str); 40 } 41 42 }

 

转载于:https://www.cnblogs.com/jiandonn/p/4576586.html


最新回复(0)