标签:
1、包装类的概述
· 将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据。
· 常用的操作之一:用于基本数据类型与字符串之间的转换。
· 基本类型和包装类的对应
/** * Integer的构造方法: * public Integer(int value) * public Integer(String s) * 注意:这个字符串必须是由数字字符组成 */ public class IntegerDemo01{ public static void main(String[] args) { //方式一: int i =100; Integer ii = new Integer(i); System.out.println("ii:"+ii); //ii:100 //方式二: String s = "100"; Integer iii = new Integer(s); System.out.println("iii:"+iii); //iii:100 } }
public class IntegerDemo02 { public static void main(String[] args) { //输出100的二进制,八进制和十六进制 System.out.println(Integer.toBinaryString(100)); //1100100 System.out.println(Integer.toOctalString(100)); //144 System.out.println(Integer.toHexString(100)); //64 System.out.println(Integer.MAX_VALUE); //2147483647 System.out.println(Integer.MIN_VALUE); //-2147483648 } }
3、int类型和String类型的相互转换
//int --> String : String.valueOf(int) //String --> int : Integer.paraseInt(String) public class IntegerDemo03 { public static void main(String[] args) { //int --> String int num = 100; //方式一: String s1 = "" + num; System.out.println("s1:" + s1); //方式二:推荐 String s2 = String.valueOf(num); System.out.println("s2:"+s2); //方式三: //int --> Integer --> String String s3 = new Integer(num).toString(); System.out.println("s3:"+s3); //方式四: String s4 = Integer.toString(num); System.out.println("s4:"+s4); System.out.println("----------------"); //String --> int //方式一:String --> Integer --> int //public int intValue(); String s = "100"; Integer i = new Integer(s); int i1 = i.intValue(); System.out.println("i1:"+i1); //方式二:Integer.paraseInt(s):推荐 int i2 = Integer.parseInt(s); System.out.println("i2:"+i2); } }
public class IntegerDemo04 { public static void main(String[] args) { //十进制到二进制,八进制,十六进制 System.out.println(Integer.toBinaryString(100)); //1100100 System.out.println(Integer.toOctalString(100));//144 System.out.println(Integer.toHexString(100)); //64 //十进制到其他进制 System.out.println(Integer.toString(100, 10)); //100 System.out.println(Integer.toString(100,2)); //1100100 System.out.println(Integer.toString(100,36)); //2s System.out.println(Integer.toString(100, 37)); //100 //其他进制到十进制 System.out.println(Integer.parseInt("100",10)); //100 System.out.println(Integer.parseInt("100",2)); //4 //报错,因为二进制中没有2,3 //System.out.println(Integer.parseInt("123",2)); } }
public class IntegerDemo05 { public static void main(String[] args) { //定义了一个int类型的包装类类型变量i //Integer i1 = new Integer(100); Integer i2 = 200; i2 += 300; System.out.println("i2:"+i2); //通过反编译后的代码 //Integer i2 = Integer.valueOf(200); //自动装箱 //i2 = Integer.valueOf(i2.intValue() + 200); //System.out.println((new StringBuilder("i2:")).append(i2).toString()); } }
public class IntegerDemo06 { public static void main(String[] args) { Integer i1 = new Integer(127); Integer i2 = new Integer(127); System.out.println(i1 == i2); //false System.out.println(i1.equals(i2)); //true System.out.println("---------------"); Integer i3 = new Integer(128); Integer i4 = new Integer(128); System.out.println(i3 == i4); //false System.out.println(i3.equals(i4)); //true System.out.println("---------------"); Integer i5 = 128; Integer i6 = 128; System.out.println(i5 == i6); //false System.out.println(i5.equals(i6)); //true System.out.println("---------------"); Integer i7 = 127; Integer i8 = 127; System.out.println(i7 == i8); //true System.out.println(i7.equals(i8)); //true Integer i = Integer.valueOf(127); //通过查看源码,针对-128到127之间的数据做了一个数据缓冲池, //如果一个数据是在该范围内的,每次都并不创建新的空间, //如果不在这个范围里面的就会创建新的对象 // public static Integer valueOf(int i) { // assert IntegerCache.high >= 127; // if (i >= IntegerCache.low && i <= IntegerCache.high) // return IntegerCache.cache[i + (-IntegerCache.low)]; // return new Integer(i); } }
标签:
原文地址:http://www.cnblogs.com/yangyquin/p/4949487.html