包装类(精讲)---最全

574 篇文章 4 订阅
272 篇文章 1 订阅

(一)
/**

  • 包装类的作用:
  •  使编程更加方便
    
  • @author Administrator

*/
public class Test01 {

public static void main(String[] args) {

	//调用方法, 只要是引用类型的数据就可以作为实参
	m1("abc"); 		//实际上是把"abc"在常量池中的引用传递给obj参数
	
	m1(123);		//不能把基本类型数据传递给obj参数
	/*
	 * 这儿没有语法错误, 是因为系统把基本类型123先转换为了对应的包装类对象, 再把包装类对象的引用传递给形参obj
	 */
}
//obj是Object类型的引用, obj参数保存的是对象的引用(地址)
public static void m1( Object  obj) {
		
}

}

(二)
public class Application {
public static void main(String[] args) {
// SpringApplication.run(Application.class, args);
System.out.println( Integer.SIZE); //32
System.out.println( Integer.BYTES); //4
System.out.println( Integer.MIN_VALUE); //-2147483648
System.out.println( Integer.MAX_VALUE); //2147483647
System.out.println( Integer.TYPE); //int

    System.out.println( Boolean.TYPE);
    System.out.println( Boolean.TRUE);
    System.out.println( Boolean.FALSE);

    System.out.println( Double.NaN);
    System.out.println( Double.POSITIVE_INFINITY);
    System.out.println( Double.NEGATIVE_INFINITY);

    System.out.println( 10 / 0.0 ); 		//Infinity, 无穷大
    System.out.println( -10 / 0.0 ); 		//-Infinity, 负无穷大
    System.out.println( 0 / 0.0 ); 		//NaN, Not a Number
}

}
测试结果:
32
4
-2147483648
2147483647
int
boolean
true
false
NaN
Infinity
-Infinity
Infinity
-Infinity
NaN
Process finished with exit code 0
注意: System.out.println( 10 / 0 ); 输出会报异常:Exception in thread “main” java.lang.ArithmeticException: / by zero

(三)
/**

  • 包装类的构造方法
  •  1) 根据基本类型创建包装类对象
    
  •  2) 根据字符串 创建包装类对象
    
  • @author Administrator

*/
public class Test02 {

public static void main(String[] args) {
	// 1) 根据基本类型创建包装类对象
	Integer i1 = new Integer(123);
	Double dd1 = new Double(45.78);
	Boolean bb1 = new Boolean(true);

	//2) 根据字符串 创建包装类对象
	Integer i2 = new Integer("456123");
	System.out.println( i2 );
	//注意, 根据字符串创建包装类对象时, 字符串要符合数字的格式

// i2 = new Integer(“adfasf”); //java.lang.NumberFormatException
// i2 = new Integer(“3.14”); // java.lang.NumberFormatException

	Double dd2 = new Double("45.78");

// dd2 = new Double(“adsf”); //java.lang.NumberFormatException:
dd2 = new Double(“456”);

	//布尔类型比较特殊, 如果是"true"字符串会转换为true, 其他的字符串都转换为false
	Boolean bb2 = new Boolean("true");
	System.out.println( bb2 );
	bb2 = new Boolean("trasdfaffdue");
	System.out.println( bb2 );
	bb2 = new Boolean( null );
	System.out.println( bb2 );
}

}
输出结果:
456123
true
false
false

Process finished with exit code 0

(四)
public class Application {
/**
* 包装类的常用操作
* @author Administrator
*
*/

public static void main(String[] args) {
    //1)包装类对象
    Integer i1 = new Integer(456);
    Integer i2 = new Integer("123");

    //2) 从Number继承的方法, 可以把包装类对象转换为基本类型
    byte  bb = i2.byteValue();
    double dd = i1.doubleValue();
    System.out.println( bb );
    System.out.println( dd );
    bb = i1.byteValue();		//只把最后一个字节保存到bb变量中
    System.out.println( bb );   //-56

    //3)compareTo(Integer anotherInteger)  如果第一个对象大 返回正数, 参数对象大返回负数,相等返回0
    //Comparable<T>接口,作用是比较当前对象与参数对象的大小 , 一个类实现了该接口,需要重写compareTo()
    //Comparable<T>接口中的<T>是泛型(把数据类型当作参数), 指定参数对象的数据类型
    //Integer对象比较大小是根据value字段的值比较大小
    //每个包装类都有一个value字段, 保存对应的基本类型的值
    System.out.println( i1.compareTo(i2) );  	//1
    Boolean b1 = new Boolean(true);
    Boolean b2 = new Boolean("afda");
    System.out.println( b1.compareTo(b2));  	//1

    //4)equals(Object obj) ,判断两个对象的是否相等,根据基本类型的value值判断是否相等
    System.out.println( i1.equals(i2) );
    System.out.println( b1.equals(b2) );

    //5)parseInt(String s) 可以把字符串转换为基本类型的数据
    int xx = Integer.parseInt("7894");
    System.out.println( xx );

// xx = Integer.parseInt(“7.894”); // java.lang.NumberFormatException:
double dd3 = Double.parseDouble(“45.123”);
System.out.println(dd3);
boolean flag = Boolean.parseBoolean(“qerreqe”);
System.out.println( flag ); //false

    //6)把整数转换为二进制,八进制,十六进制
    System.out.println( Integer.toBinaryString(100));
    System.out.println( Integer.toOctalString(100));
    System.out.println( Integer.toHexString(100));

    //7)valueOf(int i) /valueOf(String s) 把基本类型/字符串 转换为包装类对象
    i1 = Integer.valueOf( 258);
    i2 = Integer.valueOf("369");
    System.out.println(i1);
    System.out.println(i2);
    dd3 = Double.valueOf(3.14);
    dd = Double.valueOf("456.789");

}
}

输出结果
123
456.0
-56
1
1
false
false
7894
45.123
false
1100100
144
64
258
369

Process finished with exit code 0
(五)
/**

  • int , String, Integer相互转换
  • @author Administrator

*/
public class Test05 {

public static void main(String[] args) {
	int num = 123;
	//1) int-->String
	String text = "" + num; 		//字符串与基本类型连接,先把基本类型转换为字符串再连接
									//			 "" + "123"
	text = String.valueOf(num);
	text = Integer.toString(num);
	
	//2) String --> int
	num = Integer.parseInt(text);

	//3) int --> Integer
	Integer i1 = new Integer(num);
	i1 = Integer.valueOf(num);
	i1 = num;						//Java可以自动的把基本类型转换为包装类对象
	
	//4) Integer--> int
	num = i1.intValue();
	num = i1;					//Java可以自动的把包装类对象转换为基本类型数据
	
	//5) Integer-->String
	text = i1.toString();
	 
	//6) String-->Integer
	i1 = new Integer(text);
	i1 = Integer.valueOf(text);
}

}
(六)
/**

  • 装箱
  •  把基本类型转换为包装类对象
    
  • 拆箱
  •  把包装类对象转换为基本类型
    
  • 在Java中,可以自动的装箱与拆箱
  • @author Administrator

*/
public class Test06 {

public static void main(String[] args) {
	Integer i1 = 456; 		//自动装箱 
	Integer i2 = 456;		//根据基本类型456创建一个包装类对象
	System.out.println( i1 == i2 ); 		//false 包装类是引用类型
	System.out.println( i1.equals(i2)); 	//true

	i1 = 123;		//根据基本类型123创建一个包装类对象
	i2 = 123;
	System.out.println( i1 == i2 ); 		//true 
	/*
	 * Java认为-128~127之间的整数是最常用的整数
	 * 在这个范围内的整数自动装箱后,生成的包装类对象存储方法区的共享池中
	 * 	i1 = 123;  会创建一个包装类对象保存到方法区的常量池中
	 * 	再执行i2 = 123; 时, 就直接把常量池中的包装对象的 引用赋值给i2
	 */

	Long gg1 = 99L;
	Long gg2 = 99L;
	System.out.println( gg1 == gg2 ); 		//true
}

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值