1.包装类
byte Byte short Short int Integer long Long char Character float Float double Double boolean Boolean 基本数据和其包装类:作为成员变量时的默认初始值的差异: int -- Integer 0 null
Integer,Byte,Float,Double,Short,Long都是Number类的子类。Character和Boolean都是Object直接子类;8个类都是final修饰的(不可被继承)。
//包装类对象 把基本数据类型包装类除了 Character类之外,其他的7个都有两个构造方法 基本数据类型xx; xx对应包装类的构造方法: public xx的包装类(xx x){} public xx的包装类(String x){} 这里传递的x值表示该包装类型对象的值; Integer i = new Integer(17);//i = 17 i = new Integer("123");// i = 123 使用第二种构造方法可能会引发 类型转换异常; Integer:凡是涉及到整数的操作,Integer类里都有相应的方法来处理; Character类没有public Character(String c){}Boolean b = new Boolean("tRuE");//除了true之外(不区分大小写),其他的默认都是false /* static Boolean FALSE 对应基值 false 的 Boolean 对象。 static Boolean TRUE 对应基值 true 的 Boolean 对象。 */ b = Boolean.FALSE;//FALSE == new Boolean(false);
/* Integer: 有3个静态的方法可以完成: static String toBinaryString(int i) 以二进制(基数 2)无符号整数形式返回一个整数参数的字符串表示形式。 static String toHexString(int i) 以十六进制(基数 16)无符号整数形式返回一个整数参数的字符串表示形式。 static String toOctalString(int i) */
//byte byteValue() 以 byte 类型返回该 Integer 的值。 Integer i = new Integer(17); byte by = i.byteValue(); System.out.println(by); /* int compareTo(Integer anotherInteger)在数字上比较两个 Integer 对象。 */ System.out.println(i.compareTo(new Integer(12)));//1(-1,0,1)
/* int intValue() 以 int 类型返回该 Integer 的值。 包装类 -->基本类型: 调用xxxValue(); int ret = age.intValue(); 基本类型 -->其包装类 new 包装类(基本类型的值); new Integer(17); */ int age = i.intValue();
/* static int parseInt(String s) 将字符串参数作为有符号的十进制整数进行解析。 String --> 基本类型 */ age = Integer.parseInt("17");
/* static Integer valueOf(int i) 基本类型 -->包装类类型 和 new Integer(int i)一样; 返回一个表示指定的 int 值的 Integer 实例。 static Integer valueOf(String s) String --> Integer等价于 new Integer(String s); 返回保存指定的 String 的值的 Integer 对象。 */ //static boolean isDigit(char ch) 确定指定字符是否为数字。 //判断的是0 ~9之间的数字 System.out.println(Character.isDigit('A'));//false
System.out.println("----------------"); int i1 = 12; int i2 = 12; System.out.println(i1 == i2);//相等 Integer in1 = new Integer(12); Integer in2 = new Integer(12); System.out.println(in1 == in2);//不相等的,两个对象
in1 = 12; in2 = 12; System.out.println("--->"+(in1 == in2));//true /* 这里涉及到了一个享元模式: 只缓存[-128,127]区间的数字,Byte,Short,Integer,Long 都是一样的 */ in1 = 128; in2 = 128; System.out.println("--->"+(in1 == in2));//false
/* 装箱和拆箱: int Integer 装箱: 把基本数据类型 --> 它所对应的包装类类型 Integer i = new Integer(int value); 拆箱: 包装类类型 --> 它所对应的把基本数据类型 int i2 = i.intValue(); Java5开始提供了自动装箱和拆箱的功能; 自动装箱: 可以把一个基本数据类型的值直接赋给 其包装类对象 Integer age = 17; Object就可以接受一切类型了; Object o = false; o = 123; 自动拆箱: 可以把一个包装类对象直接赋给一个基本数据类型的变量 int age2 = age; */ Integer age2 = 17; int age3 = age2;
/* int 和 Integer 是不是一样的类型? */ /** public static void show(Integer a,Integer b){ System.out.println("Integer"); } **/ public static void show(int a,int b){ System.out.println("int"); } public static void main(String[] args) { Integer i1 = 1; Integer i2 = 2; //show(1,2);//int方法 show(i1,i2);//Integer /* String和基本数据类型之间的转换 String int String -->int : int xx (String s); static int parseInt(String s) 将字符串参数作为十进制整数进行解析。 int --> String : Integer类. static String toString(int i) String类. public static String valueOf(int i) */ //int i = Integer.parseInt("AAA");//Exception in thread "main" java.lang.NumberFormatException: For input string: "AAA"s int i = Integer.parseInt("007"); System.out.println(i); //String s = i +"";表示基本数据类型和String值相连接,组成一个新的String值,他们之间没有转换关系 String s = Integer.toString(123); s = String.valueOf(12345); //s = (String)i;//StringDemo.java:38: 错误: 不可转换的类型 System.out.println(s);
/* int 和 Integer 是不是一样的类型? */ /** public static void show(Integer a,Integer b){ System.out.println("Integer"); } **/ public static void show(int a,int b){ System.out.println("int"); } public static void main(String[] args) { Integer i1 = 1; Integer i2 = 2; //show(1,2);//int方法 show(i1,i2);//Integer /* String和基本数据类型之间的转换 String int String -->int : int xx (String s); static int parseInt(String s) 将字符串参数作为十进制整数进行解析。 int --> String : Integer类. static String toString(int i) String类. public static String valueOf(int i) */ //int i = Integer.parseInt("AAA");//Exception in thread "main" java.lang.NumberFormatException: For input string: "AAA"s int i = Integer.parseInt("007"); System.out.println(i); //String s = i +"";表示基本数据类型和String值相连接,组成一个新的String值,他们之间没有转换关系 String s = Integer.toString(123); s = String.valueOf(12345); //s = (String)i;//StringDemo.java:38: 错误: 不可转换的类型 System.out.println(s);