为什么基本数据类型还要包装类?
因为不够用,如下有需求
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); } }
|
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); Integer y = new Integer("123"); System.out.println(y);
} }
|
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);
Integer x = 123; Integer y = 123; System.out.println(x == y);
} }
|
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);
int i = Integer.parseInt("123"); System.out.println(i + 100);
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) { String s1 = 123 + ""; String s2 = String.valueOf(123);
int i1 = Integer.parseInt("123");
Integer integer = Integer.valueOf(100); int i2 = integer.intValue();
Integer integer1 = Integer.valueOf("123"); String s = String.valueOf(new Integer(123));
} }
|