1、包装类的概念

2、装箱与拆箱
装箱:基本类型-->包装类对象
拆箱:包装类对象-->基本类型


package cn.day12.demo03;
/**
* created by apple on 2020/6/14
* 装箱:把基本类型数据 包到包装类中
* 1、构造方法
* Integer(int value) 构造一个新分配的Integer对象,表示指定的int值
* Integer(String s) 构造一个新分配的Integer对象,表示String参数
指示的int值
注意: 第二个传递的字符串,是基本类型的字符串,否则会抛出异常。。100 ok a异常
2、静态方法
static Integer valueOf(int i)
static Integer valueOf(String s)
拆箱:在包装类中取出基本类型的数据
成员方法:
int intValue()
*/
public class DemoInteger {
public static void main(String[] args) {
//装箱:
//构造方法
Integer in1 = new Integer(1); //此方法过时了
System.out.println(in1); //1 重写了toString
Integer in2 = new Integer("1");
System.out.println(in2); //1
//静态方法
Integer in3 = Integer.valueOf(1);
System.out.println(in3); //1
Integer in4 = Integer.valueOf("1");
System.out.println(in4); //1
//拆箱
int in5 = in4.intValue();
System.out.println(in5);
}
}
1
1
1
1
1
3、自动装箱与拆箱

4、基本类型与字符串之间的转换


package cn.day12.demo03;
public class Demo02Integer {
/*
J基本数据类型和字符串之间的转换
*/
public static void main(String[] args) {
//基本类型 -》字符串
int i1 =10;
String s1 = i1 +"";
System.out.println(s1+200); //10200
String s = Integer.toString(100);
System.out.println(s+200); // 100200
String s2 = String.valueOf(100);
System.out.println(s2 +200); //100200
//字符串-》基本类型
int i = Integer.parseInt(s1);
System.out.println(i+100); //110
}
}
网友评论