数据类型
基本数据类型
1、整型
byte(-2的7次方到2的7次方-1) 、short(-2的15次方到2的15次方-1) 、int(-2的31次方到2的31次方-1) 、long(-2的63次方到2的63次方-1)
2、浮点型
float(单精度浮点型) 、 double(双精度浮点型)
3、字符型
char
4、布尔型
boolean(true 、false)
.png)

基本数据类型拓展
整数拓展
1 2 3 4 5 6 7 8 9 10 11 12 13
| public class Demo03 { public static void main(String[] args) { int i = 10; int i2 = 010; int i3 = 0x10; int i4 = 0x11; System.out.println(i); System.out.println(i2); System.out.println(i3); System.out.println(i4); } }
|
浮点数拓展
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public class Demo03 { public static void main(String[] args) { float f = 0.1f; double d = 1.0/10; System.out.println(f==d); System.out.println(f); System.out.println(d); float d1 = 271627163762372881f; float d2 = d1 + 1; System.out.println(d1 == d2); } }
|
字符拓展
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
| public class Demo03 { public static void main(String[] args) { char c1 = 'a'; char c2 = '中'; System.out.println(c1); System.out.println((int)c1); System.out.println(c2); System.out.println((int)c2); char c3 = '\u0061'; System.out.println(c3); System.out.println("Hello\nWorld"); System.out.println("========================================"); String sa = new String("hello world"); String sb = new String("hello world"); System.out.println(sa==sb); String sc = "hello world"; String sd = "hello world"; System.out.println(sc==sd); boolean flag = true; } }
|
类型转换
低 —————————————————————-> 高
byte , short ,char —>int—>long—>float—>double
运算中,不同类型的数据先转化为同一类型,然后再进行运算!
强制转换: (类型)变量名 高—>低
自动转换: 低—>高
注意点:
- 不能对布尔值进行转换
- 不能把对象类型转换成不相干的类型
- 在把高容量类型转换到低容量的时候,需要强制转换
- 转换的时候可能存在内存溢出,或者精度问题!(强制转换和自动转换均存在)
1 2 3 4 5 6 7 8 9 10 11
| public class example01 { public static void main(String[] args) { int money = 10_0000_0000; int year = 20; int total = money * year; long total2 = money * year; long total3 = money * ((long)year); System.out.println(total3); } }
|
引用数据类型
1.类
2.接口
3.数组