Java包装类简介

包装类:

  • 1、为什么有包装类

    • Java设计之初,保留了C语言的8种基本数据类型,但是Java从一开始就是面向对象的,而基本数据类型不具有面向对象的特征。
    • Java是面向对象的语言,很多API和新特性都是针对“对象”设计的,这个时候,我们程序中的8中基本数据类型的数据就无法与
    • 这些API对接,即无法享受这些新特性。
    • 例如:我们的集合和泛型等不支持基本数据类型,只支持对象。
    • 为了解决这个问题,Java给八中基本数据类型和void,设计了它们对应的包装类。
  • 2、包装类有哪些?

    • 基本数据类型     包装类
    • byte                    Byte
    • short                   Short
    • int                       Integer
    • long                    Long
    • float                    Float
    • double                Double
    • char                    Character
    • boolean              Boolean
    • void                    Void
  • 3、基本数据类型与包装类之间的转换

    • (1)装箱:把基本数据类型的数据 包装为 包装类的对象
    • (2)拆箱:把包装类对象中的数据 拆出来 变为基本数据类型
    • JDK1.5之前,装箱与拆箱需要手动实现,JDK1.5之后,可以实现自动装箱与自动拆箱
    • 注意区别:
      • (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);
	}
}

包装类:java.lang包

  • 包装类们的API:
    • 1、Integer

      • 和字符串之间类型转换用的方法:
        • (1)static int parseInt(String s)
        • (2)static Integer valueOf(String s)
      • 其他方法:
        • String toBinaryString(int i) //转为二进制 0b开头
        • String toOctalString(int i)//转为八进制 0开头
        • String toHexString(int i) //转为十六进制 0x开头
      • 两个常量:
        • Integer.MAX_VALUE //int的最大值
        • Integer.MIN_VALUE //int的最小值
    • 2、Character

      • Character.toUpperCase(字符)
        • 等价于 (char)(字符 - 32)
      • Character.toLowerCase(字符)
        • 等价于 (char)(字符 + 32)
    • 3、Double

      • 比较两个double类型的数据大小,除了使用>,<,=,还可以使用这个方法
      • static int compare(double d1, double d2) :结果有三种,正整数、负整数、0
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做了一个很好设计,把这些范围的值对应的包装类对象进行了共享,如何共享呢?使用常量对象

  • 自动装箱的缓存的常量对象

    • Byte、Short、Integer、Long:-128~127
    • Float和Double:不缓存
    • Character:0~127 最早ASCII码表的128个字符
    • Boolean:true/false
      我们可以通过下面这段代码来了解一下包装类使用常量和不使用常量的区别
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,然后自动类型转换
   }	
}

面试题:

  • 考查内容:
    • (1)方法的参数传递机制
      • 形参是基本数据类型:实a参给形参的数据值,形参的修改和实参无关,例如:本题的int i
      • 形参是引用数据类型:实参给形参的地址值,形参修改了属性等同与实参修改了属性,例如:本题 MyData my
    • (2)有些不可变对象
      • 形参是引用数据类型,但是,形参是特殊的引用数据类型,String、包装类等这些,
      • 它们的对象是不可变对象,一旦修改就会产生新对象。
      • 例如:本题的String s, Integer num都是不可变对象,无论形参怎么修改,都和实参无关。
      • 因为形参已经指向新对象了,和实参无关。
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;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值