【447-459】基本类型对应的8个包装类
2022-01-24 14:27:00 # JavaSE

为什么基本数据类型还要包装类?

因为不够用,如下有需求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class _447_包装类存在的意义 {
public static void main(String[] args) {
MyInt myInt = new MyInt(3);
doSome(myInt);
}

public static void doSome(Object o){
System.out.println(o);
}
}

class MyInt{
int value;

public MyInt() {
}

public MyInt(int value) {
this.value = value;
}

@Override
public String toString() {
return String.valueOf(this.value);
}
}

八种包装类都是什么

基本数据类型 包装类型
byte java.lang.Byte(父类 Number)
short java.lang.Short(父类 Number)
int java.lang.Interger(父类 Number)
long java.lang.Long(父类 Number)
float java.lang.Float(父类 Number)
double java.lang.Double(父类 Number)
boolean java.lang.Boolean(父类 Object)
char java.lang.Character(父类 Object)

装箱与拆箱

1
2
3
4
5
6
7
8
9
10
11
12
public class _449_装箱和拆箱的概念 {
public static void main(String[] args) {
// 基本数据类型 -> 引用数据类型
// 装箱
Integer i = new Integer(123);
// 引用数据类型 -> 基本数据类型
// 拆箱
float j = i.floatValue();
System.out.println(j); // 123.0
}
}

Integer的构造方法

1
2
3
4
5
6
7
8
9
10
11
12
13
public class _450_Integer的构造方法 {
public static void main(String[] args) {
//
Integer x = new Integer(123);
System.out.println(x); // 123
//
Integer y = new Integer("123");
System.out.println(y); // 123

}
}


Double的构造方法

1
2
3
4
5
6
7
8
9
public class _451_Double的构造方法 {
public static void main(String[] args) {
//
Double z = new Double("3.14");
System.out.println(z);
}
}


通过常量获取最大值和最小值

1
2
3
4
5
6
7
8
9
public class _452_通过常量获取最大值和最小值 {
public static void main(String[] args) {
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
System.out.println(Byte.MAX_VALUE);
System.out.println(Byte.MIN_VALUE);
}
}

自动装箱和自动拆箱

自动装箱:基本数据类型自动转换成包装类。
自动拆箱:包装类自动转换成基本数据类型。

1
2
3
4
5
6
7
8
9
public class _453_自动装箱和自动拆箱 {
public static void main(String[] args) {
// 自动装箱
Integer x = 100;
// 自动拆箱
int y = x;
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class _455_自动装箱和自动拆箱 {
public static void main(String[] args) {
Integer a = 1000;
Integer b = 1000;
// "==" 不会自动拆箱
System.out.println(a == b); // false 比较的是内存地址

// java中为了提高程序的执行效率,将[-128,127]之间所有的包装对象提前创建好
// 如果数字在[-128,127]之间,数字会放到方法区常量池中
// 如下 x,y 内存地址相同
Integer x = 123;
Integer y = 123;
System.out.println(x == y); // true

}
}

Integer常用方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class _456_Integer常用方法 {
public static void main(String[] args) {
Integer x = new Integer(100);
// 手动拆箱
int z = x.intValue();
System.out.println(z);

// 这里运行异常 NumberFormatException
// Integer y = new Integer("中文");
// System.out.println(y);

// 转换为 int
int i = Integer.parseInt("123");
System.out.println(i + 100);

// 转化为 double
double v = Double.parseDouble("3.14");
System.out.println(v);

// 十进制转二进制
String s = Integer.toBinaryString(7);
System.out.println(s);

// 十进制转十六进制
String s1 = Integer.toHexString(15);
System.out.println(s1);

// 十进制转八进制
String s2 = Integer.toOctalString(9);
System.out.println(s2);

}
}

String_int_Integer类型互换

这儿因该得有个图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class _459_String_int_Integer类型互换 {
public static void main(String[] args) {
// int -> String
String s1 = 123 + "";
String s2 = String.valueOf(123);

// String -> int
int i1 = Integer.parseInt("123");

// int <-> Integer
// 自动装箱 拆箱
Integer integer = Integer.valueOf(100);
int i2 = integer.intValue();

// String <-> Integer
Integer integer1 = Integer.valueOf("123");
String s = String.valueOf(new Integer(123));

}
}

Prev
2022-01-24 14:27:00 # JavaSE
Next