1引出基本类型包装类
问题1:在面向对象中,”一切皆对象”,现在问题来了,int age = 17;请问这age代码哪里有对象,基本类型变量,不是对象.此时有矛盾.问题2:现在给你一个复杂的十进制数据,请你迅速在程序给我转换为2进制,8进制,16进制,算法太麻烦了.
问题3:现在使用double类型来表示学生的考试成绩,double类型初始值为0,0:但是:你怎么表示一个人缺考和考试得0分.
-------------------------------------------------------------------------
上述的问题,进就是因为基本数据类型缺少对象,如果需要对象,必须先有类.
此时我们可以为每一个基本类型都编写一个对应的包装类,类中包含了该基本类型的一个值.
八大基本数据类型的包装类都使用final修饰,都是最终类,都不能被继承.
2.装箱和拆箱
装箱:把基本类型数据转成对应的包装类对象。
class TestDemo
{
public static void main(String[] args)
{
//装箱的两种方式
Integer num1 = new Integer(17);
Integer num2 = Integer.valueOf(17);
System.out.println(num1); //17
System.out.println(num2); //17
}
}
拆箱:把包装类对象转成对应的基本数据类型数据。
class TestDemo
{
public static void main(String[] args)
{
//装箱的两种方式
Integer num1 = new Integer(17);
Integer num2 = Integer.valueOf(17);
//拆箱
int num3 = num1.intValue();
System.out.println(num1); //17
System.out.println(num2); //17
System.out.println(num3); //17
}
}
Sun公司从Java5开始提供了的自动装箱(Autoboxing)和自动拆箱(AutoUnboxing)功能 :
自动装箱:可把一个基本类型变量直接赋给对应的包装类变量。
自动拆箱:允许把包装类对象直接赋给对应的基本数据类型变量。
class TestDemo
{
public static void main(String[] args)
{
//自动装箱
Integer num1 = 17;
//自动拆箱
int num2 = num1;
System.out.println(num1); //17
System.out.println(num2); //17
}
自动装箱和拆箱,也是一个语法糖/编译器级别新特性.
在底层依然是手动装箱和拆箱操作.
但是:装箱操作使用的是Integer.valueOf的方式,而不是直接new Integer.
------------------------------------------------------------
switch支持的数据类型:byte,short,char,int,也支持对应的包装类. 为何?其实在底层,switch中会对包装类做手动拆箱操作.可以使用反编译工具查看
Object数组:Object[]该数组可以装一切数据类型.
Object[] arr = {“A”,12,3.14,true};
3.包装类的常用操作方法
1:包装类中的常量MAX_VALUE/MIN_VALUE/SIZE(在内存中存储占多少位)/TYPE(对应的基本类型)
class TestDemo
{
public static void main(String[] args)
{
System.out.println(Integer.MAX_VALUE); // 2147483647
System.out.println(Integer.MIN_VALUE ); // -2147483648
System.out.println(Integer.SIZE ); // 32
System.out.println(Integer.TYPE); // int
}
}
2:包装类的构造器,xxx类型的包装类Xxx:(xxx表示8大基本数据类型).
Xxx(xxx value):接受自己的基本类型值,如Integer(int val)/Boolean(boolean val)
Xxx(String value): 但是,Character除外.
构造器的作用:创建包装类对象.
class TestDemo
{
public static void main(String[] args)
{
Integer num = new Integer(18);
Integer num2 = new Integer("18");
System.out.println(num); //18
System.out.println(num); //18
}
}
3:基本类型和包装类型的转换(装箱和拆箱)
装箱:
Integer i1 = new Integer(123);
Integer i2 = Integer.valueOf(123);//推荐,带有缓存.
包装类中的缓存设计(享元模式),本质就是缓存设计:Byte、Short、Integer、Long:缓存[-128,127]区间的数据;
Character:缓存[0,127]区间的数据;
class TestDemo
{
public static void main(String[] args)
{
Integer num = new Integer(123);
Integer num2 = new Integer(123);
System.out.println(num==num2); //false
Integer num3 =Integer.valueOf(123);
Integer num4 =Integer.valueOf(123);
System.out.println(num3==num4); //true,同一个对象 缓存设计:缓存[-128,127]区间的数据
}
}
源码:可以看到low=-128,high=127
拆箱:
int val = i1.intValue();
4:String和基本类型/包装类型之间的转换操作
String和int/Integer之间的转换操作:转换方法必须在String类中或Integer类中.
把String转换为包装类类型:
方式1: static Xxx valueOf(String str) :把String转换为包装类的对象
Integer i1 = Integer.valueOf(“123”);
方式2: new Xxx(String str):
Integer i2= new Integer(“123”);
把包装类对象转换为String.
String str = 任何对象.toString();
把基本数据类型转换为String:
String str = 17 + ””;
String str = String.valueOf(17);
把String转换为基本数据类型:
static xxx parseXxx(String s) :xxx表示8大基本数据类型
String input=”12345”;
int num = Integer.parseInt(input);
5:Boolean b = new Boolean("SB");//false
只认可true/TRUE,为true,其他都是false.
6.包装类型之间互相转换
4.Integer与int的区别(包装类型和基本数据类型的区别)
1):默认值int的默认值是0.
Integer的默认值为null.
推论:Integer既可以表示null,又可以表示0.
2):包装类中提供了该类型相关的很多算法操作方法.
static String toBinaryString(int i) :把十进制转换为二进制
static String toOctalString(int i) : :把十进制转换为八进制
static String toHexString(int i) : :把十进制转换为十六进制
3):在集合框架中,只能存储对象类型,不能存储基本数据类型值
4):请问:Integer和int是相同的数据类型吗?不是
5): 方法中的,基本类型变量存储在栈中,包装类型存放于堆中.
开发中,建议使用包装类型.