基本包装类
- 概述:为了对基本数据类型更方便的进行操作,Java为每种基本数据类型都做了对应的包装类。
- 基本类和对应的包装类
byte ----------------- Byte
short ---------------- Short
int -------------------- Integer
long ----------------- Long
float ----------------- Float
double -------------- Double
char ----------------- Character
boolean ------------- Boolean
常用包装类 Integer
- 概述: Integer 类在对象中包装了一个基本类型 int 的值,
该类提供了多个方法,能在 int 类型和 String 类型之间互相转换,
还提供了处理 int 类型时非常有用的其他一些常量和方法. - 构造方法:
public Integer(int value)
public Integer(String s)
public class Test1 {
public static void main(String[] args) {
int num1=1;
String str1="99";
Integer integer1 = new Integer(num1);
Integer integer2 = new Integer(str1);
System.out.println(integer1);
System.out.println(integer2);
}
}
- 例:
将一个数字转换为二进制
public class Test {
public static void main(String[] args) {
int num=100;
Integer integer = new Integer(num);
System.out.println(integer);
String s = Integer.toBinaryString(num); //静态方法直接用类名调用
System.out.println(s);
}
}
得到int类型的最大最小值
public class Test2 {
public static void main(String[] args) {
int maxValue = Integer.MAX_VALUE;
int minValue = Integer.MIN_VALUE;
System.out.println("最大值:"+maxValue);
System.out.println("最小值:"+minValue);
}
}
- int和String类型的相互转换:
int-------->String
a. int 类型与“”(空串)拼接
b.public static String valueOf(int i)
c.public static String toString(int i)
public class Test3 {
public static void main(String[] args) {
int num=99;
//方法一
String str1 = num + "";
//方法二
String str2 = String.valueOf(num);
//方法三
Integer integer=new Integer(num);
String str3 = integer.toString();
System.out.println(str1);
System.out.println(str2);
System.out.println(str3);
}
}
String-------->int
public class Test4 {
public static void main(String[] args) {
String str="233";
Integer integer = new Integer(str);
//方法一
int num1 = integer.intValue();
//方法二
int num2 = Integer.parseInt(str);
System.out.println(num1);
System.out.println(num2);
}
}
- 自动拆箱和自动装箱:
自动装箱:将一个基本类型,直接包装成他的包装类型
自动拆箱:将一个包装类型,转化成他所对应的基本类型
public class Test5 {
public static void main(String[] args) {
Integer integer=100;//自动装箱
int i=integer;//自动拆箱
}
}
- 手动拆箱和手动装箱:
public class Test6 {
public static void main(String[] args) {
Integer integer1=Integer.valueOf(20); //手动装箱
int i = integer1.intValue(); //手动拆箱
}
}
- PS:
- int 和Integer在进行比较的时候,Integer会进行拆箱,转为int值与int进行比较。
- Integer与Integer比较的时候:
public class Test7 {
public static void main(String[] args) {
//new Integer对象 并初始化值127
Integer integer1 = new Integer(127);
Integer integer2 = new Integer(127);
boolean b1=integer1==integer2;
boolean b2=integer1.equals(integer2);
System.out.println(b1);//false
System.out.println(b2);//true
//new Integer对象 并初始化值128
Integer integer3 = new Integer(128);
Integer integer4 = new Integer(128);
boolean b3=integer3==integer4;
boolean b4=integer3.equals(integer4);
System.out.println(b3);//false
System.out.println(b4);//true
//因为new对象后在堆内存中都开辟了新的空间 ==判断时会判断其地址值是否相等
//equals方法被Integer重写 判断的是对象的值是否相等
System.out.println("---------");
//自动装箱127
Integer integer5=127;
Integer integer6=127;
boolean b5=integer5==integer6;
boolean b6=integer5.equals(integer6);
System.out.println(b5);//true
System.out.println(b6);//true
//自动装箱128
Integer integer7=128;
Integer integer8=128;
boolean b7=integer7==integer8;
boolean b8=integer7.equals(integer8);
System.out.println(b7);//false
System.out.println(b8);//true
//自动装箱 当其值为-128——127之间的时候 integer对象会从其缓存IntegerCache中获取不用新创建对象
//而当其值超过这个范围的时候 Integer就会在堆内存中开辟新空间创建对象
}
}