(Java)常用API(四) ---- 基本类型包装类

【基本类型包装类】

 字节型短整型整型长整型字符型布尔型浮点型浮点型
基本类型byteshortintlongcharbooleanfloatdouble
包装类ByteShortIntegerLongCharacterBooleanFloatDouble

基本数据类型对应包装类的特点:用于在基本数据类型和字符串之间进行转换。

【Integer的静态方法】-- 数据类型转换

/*
 * 基本类型包装类 Integer的静态方法
 */
public class IntegerDemo1 {
	public static void main(String[] args) {
		function_1();
		function_2();
		function_3();
	}
	
	//1.Integer类中静态方法parse(String s) 返回基本数据类型
	//  参数字符串 s 必须是数字格式
	public static void function_1(){
		int i = Integer.parseInt("1234");
		System.out.println(i/2);              // 617
	}
	
	//2.Integer类中静态方法parse(String s, int radix)  返回结果为十进制
	// 参数 radix基数 :进制      
	public static void function_2(){
		int i = Integer.parseInt("110", 2);    // 2为二进制
		System.out.println(i);                 // 6
	}
	
	//3.将基本类型,转换为String类型
	//  任何类型+""  变成 String类型
	public static void function_3(){
		//1.任何类型+""  变成 String类型
		int i = 4;
		String s1 = i + "";
		System.out.println(s1+1);                 // 41
		
		//2.Integer的静态方法toString()
		String s2 = Integer.toString(i);
		System.out.println(s2+2);                //  42
		
		//3.Integer的静态方法toString(,)
		String s3 = Integer.toString(5,2);       //  将 5 转为 2进制数
		System.out.println(s3);                  //  101
	}

}

【Integer的构造方法】

/*
 * Integer类的构造方法
 * 
 */
public class IntegerDemo2 {
	public static void main(String[] args) {
		function_1();
		function_2();
		function_3();
	}
	
	//1.构造方法Integer(String s)
	//  非静态方法intValue(),将构造方法中的字符串转成基本数据类型
	public static void function_1(){
		Integer s1 = new Integer("1221");
		int i1 = s1.intValue();
		System.out.println(--i1);         // 1220
	}
	
	//2.Integer类的静态成员变量
	//  MAX_VALUE     MIN_VALUE
	public static void function_2(){
		System.out.println(Long.MAX_VALUE);
		System.out.println(Long.MIN_VALUE);
	}
	
	//3.进制的转换 ,转成了字符串的格式
	public static void function_3(){
		System.out.println(Integer.toBinaryString(6));  // 转换成二进制     110
		System.out.println(Integer.toHexString(6));     // 转换成16进制    6
		System.out.println(Integer.toOctalString(6));   // 转换成八进制      6
	}

}

【自动装箱和拆箱】

/*
 * 1.自动装箱:基本数据类型,直接变成对象
 * 2.自动拆箱:对象中的数据,变回基本数据类型
 * 3.自动装箱和拆箱的好处    :基本类型和引用类型直接运算
 *             弊端      :出现空指针异常(null)
 */
public class IntegerDemo3 {
	public static void main(String[] args) {
		function_2();
		
		
	}
	//1.自动装箱、自动拆箱
	public static void function_1(){
		// Integer为引用类型,in为引用变量,指向一个对象   但 1 为int 基本类型
		// 1.自动装箱: 基本数据类型1,直接变成了对象
		Integer in =1;                  // Integer in = new Integer(1);
				
		// 2.自动拆箱
		//  in.intValue()+1 = 2
		//  in = 2   自动装箱
		in = in +1;
	}
	
	//2.练习
	public static void function_2(){
		Integer i1 = new Integer(1);
		Integer i2 = new Integer(1);
		System.out.println(i1 == i2);        // false  比较的对象地址
		System.out.println(i1.equals(i2));   // true   继承object重写equals,比较对象的数据
		
		System.out.println("===========128==========");
		Integer a = 128;
		Integer b = 128;
		System.out.println(a == b);          // false
		System.out.println(a.equals(b));     // ture
		
		// 注意:数据在byte范围内,JVM不会重新new对象(为了节约空间)
		System.out.println("===========127==========");
		Integer a1 = 127;
		Integer b1 = 127;
		System.out.println(a1 == b1);       // ture
		System.out.println(a1.equals(b1));  // ture
	}

}

【System类】

/*
 * System类
 * 
 */
public class SystemDemo1 {
	public static void main(String[] args) {
		//function_1();
		//function_2();
		function_3();
	}
	
	//1. 测试程序执行时间
	//   获取系统当前毫秒值        static long currentTimeMillis()
	public static void function_1(){
		long start = System.currentTimeMillis();
		for(int i=0;i<10000;i++){
			System.out.println(i);
		}
		long end = System.currentTimeMillis();
		System.out.println(end -start);
	}
	
	//2.退出虚拟机,所有程序全停止
	public static void function_2(){
		
		while(true){
			System.out.println("hehe");
			System.exit(0);             // 参数非0 是异常停止
		}
	}
	
	//3.收垃圾
	//  System.gc()
	
	//4.System类方法  复制数组
	//  arraycopy(object src, int srcPos, object dest, int destPos,int length)
	/*
	 * object src,    要复制的原数组
	 *  int srcPos,   数组源的起始索引
	 *  object dest,  复制后的目标数组
	 *  int destPos,  目标数组起始索引
	 *  int length    要复制的数组的数量
	 * 
	 */
	public static void function_3(){
		int[] src = {0,1,2,3,4,5};
		int[] dest = {6,7,8,9};
		System.arraycopy(src, 1, dest, 1, 2);
		for(int i=0;i<dest.length;i++){
			System.out.println(dest[i]);  // 6 1 2 9
		}
		
	}

}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值