Java数据类型

数据类型

基本数据类型

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)

![](E:\cjy1998.github.io\source\images\数据类型2 (1).png)

基本数据类型拓展

整数拓展

1
2
3
4
5
6
7
8
9
10
11
12
13
public class Demo03 {
public static void main(String[] args) {
//整数拓展: 进制 二进制(0b开头) 十进制 八进制(0) 十六进制(0x)
int i = 10;
int i2 = 010;//八进制(0)
int i3 = 0x10;//十六进制(0x) 0~9 A~F 16
int i4 = 0x11;
System.out.println(i); //输出10
System.out.println(i2);//输出8
System.out.println(i3);//输出16
System.out.println(i4);//输出17
}
}

浮点数拓展

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) {
//浮点数拓展 银行业务钱的表示?-BigDecimal 数学工具类
//float 有限 离散 舍入误差 大约 接近但不等于
//double
//最好完全使用浮点数进行比较
//最好完全使用浮点数进行比较//最好完全使用浮点数进行比较
float f = 0.1f;//0.1
double d = 1.0/10;//0.1
System.out.println(f==d);//false
System.out.println(f);//0.1
System.out.println(d);//0.1
float d1 = 271627163762372881f;
float d2 = d1 + 1;
System.out.println(d1 == d2);//true
}
}

字符拓展

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);//强制转换
//所有的字符本质还是数字
//编码 Unicode 表:97=a 65=A 2字节 0-65536 Excel 2的16次方=65536
//U0000 UFFFF
char c3 = '\u0061';
System.out.println(c3);//a
//转义字符
// \t 制表符
// \n 换行
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);//false
String sc = "hello world";
String sd = "hello world";
System.out.println(sc==sd);//true
//对象 从内存分析
//布尔值扩展
boolean flag = true;
/* if(flag==true){} 等于 if(flag){}*/
//Less is More! 代码要精简易读
}
}

类型转换

低 —————————————————————-> 高

byte , short ,char —>int—>long—>float—>double

运算中,不同类型的数据先转化为同一类型,然后再进行运算!

强制转换: (类型)变量名 高—>低

自动转换: 低—>高

注意点:

  1. 不能对布尔值进行转换
  2. 不能把对象类型转换成不相干的类型
  3. 在把高容量类型转换到低容量的时候,需要强制转换
  4. 转换的时候可能存在内存溢出,或者精度问题!(强制转换和自动转换均存在)
1
2
3
4
5
6
7
8
9
10
11
public class example01 {
public static void main(String[] args) {//在intellij IDEA开发者工具中 Java中main方法快捷方式 psvm + 回车
//操作比较大的数的时候,注意溢出问题
int money = 10_0000_0000;//JDk7以后新特性,数字之间可以用下划线分割
int year = 20;
int total = money * year;//-1474836480 , 计算的时候溢出了
long total2 = money * year;//-1474836480 ,money * year计算结果默认是int类型,然后计算完之后再转换成long已经晚了,转换之前就已经存在了问题!
long total3 = money * ((long)year);//20000000000 , 先把一个数转换为long类型
System.out.println(total3);//在intellij IDEA开发者工具中 输出方法快捷键 sout + 回车
}
}

引用数据类型

1.类

2.接口

3.数组


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!