练习-装箱拆箱

1. 对byte,short,float,double进行自动拆箱和自动装箱

2. byte和Integer之间能否进行自动拆箱和自动装箱

3. 通过Byte获取byte的最大值

代码:

修改前

//Number类

//package digit;

//

//public class TestNumber {

//

// public static void main(String[] args) {

//

// Integer i = 5;

//

// Integer it = new Integer(i);// Integer是Number的子类,所以打印ture,这里除了int

// // 还可以是Byte,Short inreger,Long,Float Double。

//

// System.out.println(it instanceof Number);

//

// }

//}

//基本类型装换成封装类型

//package digit;

//

//public class TestNumber {

//

// public static void main(String[] args) {

//

// int i = 5;// 基本类型转换成封装类型

//

// Integer it = new Integer(i);

//

// }

//

//}

//package digit;

//

//public class TestNumber {

//

// public static void main(String[] args) {

//

// int i = 5;// 基本类型转换成封装类型

//

// Integer it = new Integer(i);

//

// // 封装类型转换成基本类型

//

// int i2 = it.intValue();

//

// }

//}

//基本类型转换成封装类型,封装类型再转换成基本类型

//自动装箱F

//package digit;

//

//public class TestNumber {

//

// public static void main(String[] args) {

// }

//

// int i = 5;

//

// Integer it = new Integer(i);// 基本类型转换成封装类型

//

// Integer it2 = i;// 自动转换就叫装箱

//

//}

//自动拆箱

//package digit;

//

//public class TestNumber {

// public static void main(String[] args) {

// int i = 5;

// Integer it = new Integer(i);

// int i2 = it;

// }

//

////}

//Int的最大值

//package digit;

//public class TestNumber{

// public static void main(String[] args){

// //int的最大值

// System.out.println("int的最小值为"+Integer.MAX_VALUE);

// //int的最小值

// System.out.println("int的最小值为"+Integer.MIN_VALUE);

//

// }

//

//}

package digit;

public class TestNumber {

public static void main(String[] args) {

byte bb = 2;

short ss=3;

float ff=4;

double dd=5.1;

// 装箱

Integer itb = new Integer(bb);

System.out.println("我在装箱byte" + itb);

Integer its = new Integer(ss);

System.out.println("我在装箱short" + its);

Integer itf = new Integer(ff);

System.out.println("我在装箱float" + itf);

Integer itd = new Integer(dd);

System.out.println("我在装箱double" + itd);

// 拆箱

Integer bb2 = itb;

System.out.println("我在拆箱byte" + bb2);

Integer ss2= its;

System.out.println("我在拆箱short" + ss2);

Integer ff2= itf;

System.out.println("我在拆箱short" + ff2);

Integer dd2= itd;

System.out.println("我在拆箱short" + dd2);

//最大值

System.out.println("byte的最大值" + Byte.MAX_VALUE);

}

}

代码:

修改后(需要强制装换float 和double为int)

//Number类

//package digit;

//

//public class TestNumber {

//

// public static void main(String[] args) {

//

// Integer i = 5;

//

// Integer it = new Integer(i);// Integer是Number的子类,所以打印ture,这里除了int

// // 还可以是Byte,Short inreger,Long,Float Double。

//

// System.out.println(it instanceof Number);

//

// }

//}

//基本类型装换成封装类型

//package digit;

//

//public class TestNumber {

//

// public static void main(String[] args) {

//

// int i = 5;// 基本类型转换成封装类型

//

// Integer it = new Integer(i);

//

// }

//

//}

//package digit;

//

//public class TestNumber {

//

// public static void main(String[] args) {

//

// int i = 5;// 基本类型转换成封装类型

//

// Integer it = new Integer(i);

//

// // 封装类型转换成基本类型

//

// int i2 = it.intValue();

//

// }

//}

//基本类型转换成封装类型,封装类型再转换成基本类型

//自动装箱F

//package digit;

//

//public class TestNumber {

//

// public static void main(String[] args) {

// }

//

// int i = 5;

//

// Integer it = new Integer(i);// 基本类型转换成封装类型

//

// Integer it2 = i;// 自动转换就叫装箱

//

//}

//自动拆箱

//package digit;

//

//public class TestNumber {

// public static void main(String[] args) {

// int i = 5;

// Integer it = new Integer(i);

// int i2 = it;

// }

//

////}

//Int的最大值

//package digit;

//public class TestNumber{

// public static void main(String[] args){

// //int的最大值

// System.out.println("int的最小值为"+Integer.MAX_VALUE);

// //int的最小值

// System.out.println("int的最小值为"+Integer.MIN_VALUE);

//

// }

//

//}

package digit;

public class TestNumber {

public static void main(String[] args) {

byte bb = 2;

short ss=3;

float ff=4;

double dd=5.1;

// 装箱

Integer itb = new Integer(bb);

System.out.println("我在装箱byte" + itb);

Integer its = new Integer(ss);

System.out.println("我在装箱short" + its);

Integer itf = new Integer(ff);

System.out.println("我在装箱float" + itf);

Integer itd = new Integer(dd);

System.out.println("我在装箱double" + itd);

// 拆箱

Integer bb2 = itb;

System.out.println("我在拆箱byte" + bb2);

Integer ss2= its;

System.out.println("我在拆箱short" + ss2);

Integer ff2= itf;

System.out.println("我在拆箱short" + ff2);

Integer dd2= itd;

System.out.println("我在拆箱short" + dd2);

//最大值

System.out.println("byte的最大值" + Byte.MAX_VALUE);

}

}


bytehe 和integer之间能进行自动拆箱和自动装箱

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。