1基本类型包装类(记忆)
Java 是面向对象的语言,但并不是“纯面向对象”的,因为我们经常用到的基本数据类型就不是对象。但是我们在实际应用中经常需要将基本数据转化成对象,以便于操作。
比如:将基本数据类型存储到 Object[ ]数组或集合中的操作等等。
为了解决这个不足,Java 在设计类时为每个基本数据类型设计了一个对应的类进行代表,这样八个和基本数据类型对应的类统称为包装类(Wrapper Class)。
针对八种基本数据类型定义相应的引用类型—包装类(封装类)
有了类的特点,就可以调用类中的方法, Java才是真正的面向对象
常用的操作之一:用于基本数据类型与字符串之间的转换
Integer
public static void main(String[] args) {
// 如果在使用过程中,调用方法的时候,如果方法上画了删除线 则表示该方法不推荐使用,但是 依然可以使用
Integer integer = new Integer("10");
//使用Integer来获取最大的整数
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
//推荐使用的 获取Integer对象的方法
// 实现了将基本类型的数据转换为他的包装类对象
Integer in = Integer.valueOf(100);
// 将基本类型的String类型转换为基本 类型对应的包装类
Integer in2 = Integer.valueOf("bac");//字符串中的内容只能为数字 否则就会出现NumberFormatException
}
自动装箱和自动拆箱
Integer in3 = 120;// 自动装箱 始于jdk1.5
int i = in;// 自动拆箱
int和String类型的相互转换
将一个int类型转化为String类型
方式一:将一个基本类型的数据和字符串拼接 就可以实现将基本类型转换为String
String s = 3 + "";
System.out.println(s + 1);//31
方式二:使用包装类提供的toString方法
package cn.lanqiao.wrapper;
public class IntegerDemo {
public static void main(String[] args) {
// 如果在使用过程中,调用方法的时候,如果方法上画了删除线 则表示该方法不推荐使用,但是 依然可以使用
Integer integer = new Integer("10");
//使用Integer来获取最大的整数
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
//推荐使用的 获取Integer对象的方法
// 实现了将基本类型的数据转换为他的包装类对象
Integer in = Integer.valueOf(100);
// 将基本类型的String类型转换为基本 类型对应的包装类
Integer in2 = Integer.valueOf("200");//字符串中的内容只能为数字 否则就会出现NumberFormatException
Integer in3 = 120;// 自动装箱 始于jdk1.5
int i = in;// 自动拆箱
String s = 3 + "";
System.out.println(s + 1);//31
System.out.println(in2.toString() + 100);//200100
}
}
方式三:使用String类提供的valueOf
System.out.println(String.valueOf(100) + 100);//100100
String 转换为 int
转换方式
方式一:先使用Integer.valueOf将字符串数字转成Integer,再调用intValue()方法转换为int
方式二:通过Integer静态方法parseInt()进行转换 public static int parseInt(String s)
//将String 类型转换为int类型
//方式一:先使用Integer.valueOf将字符串数字转成Integer,再调用intValue()方法转换为int
String str = "1000";
Integer ii = Integer.valueOf(str);
// 使用intValue方法
int ii1 = ii.intValue();
// 可以使用自动拆箱
int ii2= ii;
//方式二通过Integer静态方法parseInt()进行转换 public static int
int ii3 = Integer.parseInt(str);
}
案例:字符串中数据排序
需求:有一个字符串:“91 27 46 38 50”,请写程序实现最终输出结果是:“27 38 46 50 91”
思路:
1 将字符串按照空格分割成一个字符串数组
2 将其中的每一个元素转换为数字
3 将转换后的数字保存到一个整型数组中
4 使用Arrays提供的 排序方法或者使用我们自己写的排序方法对数组进行排序
public class StrChangInt {
public static void main(String[] args) {
String str = "91 27 46 38 50";
String[] strArr = str.split(" ");
Integer[] intArr = new Integer[strArr.length];
for(int i = 0 ; i < strArr.length;i++){
intArr[i] = Integer.parseInt(strArr[i]);
}
Arrays.sort(intArr);
for(Integer integer : intArr){
System.out.println(integer);
}
}
}
包装类的缓存问题
• 整型、char类型所对应的包装类,在自动装箱时,对于-128~127之间的值会进行缓存处理,其目的是提高效率。
• 缓存处理的原理为:如果数据在-128~127这个区间,那么在类加载时就已经为该区间的每个数值创建了对象,并将这256个对象存放到一个名为cache的数组中。每当自动装箱过程发生时(或者手动调用valueOf()时),就会先判断数据是否在该区间,如果在则直接获取数组中对应的包装类对象的引用,如果不在该区间,则会通过new调用包装类的构造方法来创建对象。下面我们以Integer类为例,看一看Java为我们提供的源码,加深对缓存技术的理解
public class StrChangInt {
public static void main(String[] args) {
Integer i = new Integer(1);
Integer j = new Integer(1);
System.out.println(i == j);//false
Integer m = 123;
Integer n = 123;
// 只要自动装箱的数据的范围在-128---127之间 都是 直接从缓存对象数组中去获取元素
System.out.println(m == n);//true
Integer x = 128;
Integer y = 128;
System.out.println(x == y);//false
}
}