public class IntegerTest01 {
public static void main(String[] args) {
//8中基本数据类型对象8种包装类
//基本数据类型转换为引用数据类型---->装箱
Integer integer = new Integer(1);
float f = integer.floatValue();
System.out.println(f);
//引用数据类型转换成基本数据类型---->拆箱
int retValue = integer.intValue();
System.out.println(retValue);
System.out.println("maximu = " + Integer.MAX_VALUE);
System.out.println("minimum = " + Integer.MIN_VALUE);
//自动装箱
//x保存的是对象的内存地址
Integer x = 70000;
Integer z = 70000;
System.out.println(x == z);
//自动拆箱
double y = x;
//java中为了提高程序运行效率,将-128到127之间所有的包装对象提前创建好,
//放到了方法区的整数型常量池中,不需要再new对象了。直接从常量池中取出来。
//在这里x存的地址和y存的地址一样,都是指向常量池中的1
x = 1;
z = 1;
System.out.println(x + 1);
System.out.println(y);
System.out.println(x == z);
//java.lang.NumberFormatException
//数字格式化异常
//不是一个数字不能包装成Integer类型
//Integer a = new Integer("中文");
//目前所遇到的异常
//NullPointerException 空指针异常
//ClassCastException 类型转换异常
//IndexOutOfBoundsException 数组下标越界异常
//NumberFormatException 数字格式化异常
//Integer类中常用方法
//1.手动拆装箱
//重点 public static int parseInt(String s) throws NumberFormatException
//其他包装类也有这个方法,照葫芦画瓢即可
int retValue1 = Integer.parseInt("456");//String--->int
//网页上的文本框输入的要求是字符串,而后台数据库要求存储数字
//此时需要将字符串转换成数字
//NumberFormatException
//int retValue2 = Integer.parseInt("中国");
System.out.println(retValue1);
//进制转换方法
String string = Integer.toBinaryString(3);//11
String string1 = Integer.toHexString(17);//11
String string2 = Integer.toOctalString(9);//11
System.out.println(string + string1 + string2); //111111
StringBuffer stringBuffer = new StringBuffer(6);
stringBuffer.append(11);
stringBuffer.append(11);
stringBuffer.append(11);
System.out.println(stringBuffer);//111111
//valueOf方法 了解即可
Integer i1 = Integer.valueOf(100);
System.out.println(i1);
Integer i2 = Integer.valueOf("100");
System.out.println(i2);
}
}
包装类Integer,以及parseInt()等常用方法
最新推荐文章于 2024-07-22 19:30:17 发布