把基本数据类型转化成包装类(装箱)
public class Demo01{
public static void main(String[] args){
//手动装箱
int num1 = 1;
Integer integer = new Integer(num1);
System.out.println(integer);
//int类型装箱 Integer.valueOf()
Integer integer1 = Integer.valueOf(num1);
System.out.println(integer1);
//String类型的装箱
String str = "100";
int num2 = Integer.parseInt(str);
System.out.println(num2);
}
}
把包装类转换成基本数据类型(拆箱)
public class Demo02{
public static void main(String[] args){
//int类型拆箱 integer.intValue();
int num1 = integer.intValue();
System.out.println(num1);
}
}
JDK1.5以后自动装拆箱
public class Demo03{
public static void main(String[] args){
//自动装箱
int num1 = 1;
Integer integer = num1;
System.out.println(integer);
//自动拆箱
int num2 = integer;
System.out.println(num2);
}
}
通过自动装箱获取 integer 对象时,如果数值在 -128~127 之间会把对象存放到缓冲区中,相同的数值直接返回缓冲区的对象
public class Demo04{
public static void main(String[] args){
Integer integer1 = 127;
Integer integer2 = 127;
Integer integer3 = -128;
Integer integer4 = -128;
//返回boolean类型
System.out.println(integer1 == integer2);//true
System.out.println(integer3 == integer4);//true
}
}
根据Ascii码表值判断,字符char是数字还是字母,由于数字,大小写字母均分别为连续存储,所以只需要与对应的最大最小值比较即可确定字符类型
public class Demo02 {
public static void main(String[] args) {
//判断是否为数字
char ch = '7';
System.out.println(Character.isDigit(ch));
//判断是否为字母
char ch1 = 'b';
System.out.println(Character.isLetter(ch1));
//判断是否为大写字母
char ch2 = 'B';
System.out.println(Character.isUpperCase(ch2));
//判断是否为小写字母
char ch3 = 'b';
System.out.println(Character.isLowerCase(ch3));
//判断是否为数字或者字母
char ch4 = 'A';
System.out.println(Character.isLetterOrDigit(ch4));
}
}