1、为什么有包装类
2、包装类有哪些?
3、基本数据类型与包装类之间的转换
public class TestWrapper {
public void test01(){
//手动装箱
int a = 10;
Integer iObj = new Integer(a);
System.out.println(iObj);
//手动拆箱
Integer iObj1 = new Integer(10);
Integer iObj2 = new Integer(20);
int sum = iObj1.intValue() + iObj2.intValue();
System.out.println("和=" + sum);
//自动装箱
int a = 10;
Integer iObj = a;
//自动拆箱
Integer iObj1 = new Integer(10);
Integer iObj2 = new Integer(20);
int sum = iObj1 + iObj2;
System.out.println("sum = " + sum);
}
}
1、Integer
2、Character
3、Double
public class TestWrapperAPI {
public void test0(){
//String转int
public void test01(){
String str = "123";
//parseInt()方法返回的类型为int
int num = Integer.parseInt(str);
System.out.println(num);
//valueOf()方法返回的类型为Integer
Integer num2 = Integer.valueOf(str);
//自动拆箱
System.out.println(num2);
}
//进制转换
public void test02(){
System.out.println(Integer.toBinaryString(10));//1010 二进制
System.out.println(Integer.toOctalString(10));//12 八进制
System.out.println(Integer.toHexString(10));//a 十六进制
}
//字母小写转大小
public void test04(){
char c = 'a';
System.out.println(Character.toUpperCase(c));
}
//比较大小,d1>d2返回正数,d1<d2返回负数,d1=d2,返回0
public void test05(){
double d1 = 1.2;
double d2 = 1.3;
System.out.println(Double.compare(d1, d2));//-1
}
}
}
包装类是为了包装基本数据类型的值用的,那么我们开发中使用频率最高的数字范围是:-128~127
因此Java做了一个很好设计,把这些范围的值对应的包装类对象进行了共享,如何共享呢?使用常量对象。
自动装箱的缓存的常量对象
public class TestWrapperEquals {
@Test
public void test1(){
int a = 1;
int b = 1;
System.out.println(a == b);//true
}
@Test
public void test2(){
Integer a = 1;
Integer b = 1;
//使用了常量
System.out.println(a == b);//true
}
@Test
public void test3(){
int a = 200;
int b = 200;
System.out.println(a == b);//true
}
@Test
public void test4(){
Integer a = 200;
Integer b = 200;
//没有使用常量,a和b相当于是两个对象
System.out.println(a == b);//false
}
@Test
public void test5(){
Integer a = new Integer(1);
Integer b = new Integer(1);
System.out.println(a == b);//false
}
@Test
public void test6(){
Integer a = 2;
Byte b = 2;
//编译错误,不是同一种引用数据类型是无法用==比较的,而且他们也不是父子类
//System.out.println(a == b);
}
@Test
public void test7(){
int a = 1;
short b = 1;
System.out.println(a == b);//true 自动类型转换
}
@Test
public void test8(){
int a = 1;
Integer b = 1;
System.out.println(a == b);//true 自动拆箱
}
@Test
public void test9(){
//Byte b = 129;//超过范围
Short b = 129;
int a = 129;
System.out.println(a == b);//true 先自动拆箱为short,然后自动类型转换
}
}
public class TestMainShi {
public static void change(int i, MyData my, String s, Integer num){
i = 2;
my.b = 2;
s = "2";//等同于s = new String("2");因为String属于不可变类型
num = 2;//等同于num = new Intager(2);因为Intager属于不可变类型
}
public static void main(String[] args) {
int x = 1;
MyData m = new MyData();
m.b = 1;
String str = "1";
Integer in = new Integer(1);
change(x,m,str,in);
System.out.println(x);//1
System.out.println(m.b);//2
System.out.println(str);//1
System.out.println(in);//1
}
}
class MyData{
int b;
}
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- igbc.cn 版权所有 湘ICP备2023023988号-5
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务