【Java】基本数据类型

Java 数据类型

基本数据类型:

  • 整型:byte \ short \ int \ long
  • 浮点型:float \ double
  • 字符型:char
  • 布尔型:boolean

引用数据类型:

  • 类(class
  • 接口(interfac
  • 数组(array

容量

  • 整型:byte(1字节=8bit) \ short(2字节)\ int(4字节)\ long(8字节)
  • 浮点型:float(4字节) \ double(8字节)
  • 字符型:char(1字符=2字节)

注意:定义long型变量,必须以"l"或"L"结尾;定义float类型变量时,变量要以"f"或"F"结尾

整型常量默认为int类型,浮点类型常量默认为double类型。

自动类型提升

自动类型提升:当容量小的数据类型的变量与容量大的数据类型的变量做运算时,结果自动提升为容量大的数据类型。

byte 、char 、short --> int --> long --> float --> double

特别的:当byte、char、short三种类型的变量做运算时,结果为int型

floatdouble类型的变量相加时,结果为double类型。

强制类型转换

强制类型转换:从容量大的类型转换成容量小的类型。它是自动类型提升运算的逆运算。其中容量大小指的是,表示数的范围的大小。比如:float容量要大于long的容量

注意事项

  1. 情况1:编译不出错,因为右边的123456默认是int类型,转给long类型时为自动类型提升(小转大,不出错)
1
long num  = 123456;
  1. 情况2:编译出错——过大的整数。因为右边的数默认为int类型,但因为该数字过大超过了int类型的范围,所以会报错。此时需要再其后面加上"l""L"
1
2
3
4
long num = 12345678987654321;

// 正确:
long num = 12345678987654321L;
  1. 情况3:编译出错——不兼容的类型:从double转换到float可能会有损失。因为右边的12.3默认为double类型,不能直接转换,需要加上强制类型转换(float)12.3。此时需要再其后面加上"f""F"
1
2
3
4
5
float f1 = 12.3;

// 正确
float f1 = (float)12.3;
float f1 = 12.3F;
  1. 情况4:编译出错——不兼容的类型:从int转换到byte可能会有损失。因为此时的 1 默认是int类型,不能直接转换,需要加上强制类型转换。
1
2
byte b = 12;
byte b1 = b + 1;

进制转换

二进制转十进制细节:https://www.bilibili.com/video/BV1Kb411W75N?t=470&p=64

计算机底层使用补码的方式存储数据

基本数据类型使用示例

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class VariableTest {
public static void main(String[] args) {
//1. 整型:byte(1字节=8bit) \ short(2字节) \ int(4字节) \ long(8字节)
//① byte范围:-128 ~ 127
//
byte b1 = 12;
byte b2 = -128;
//b2 = 128;//编译不通过
System.out.println(b1);
System.out.println(b2);
// ② 声明long型变量,必须以"l"或"L"结尾
// ③ 通常,定义整型变量时,使用int型。
short s1 = 128;
int i1 = 1234;
long l1 = 3414234324L;
System.out.println(l1);

//2. 浮点型:float(4字节) \ double(8字节)
//① 浮点型,表示带小数点的数值
//② float表示数值的范围比long还大

double d1 = 123.3;
System.out.println(d1 + 1);
//③ 定义float类型变量时,变量要以"f"或"F"结尾
float f1 = 12.3F;
System.out.println(f1);
//④ 通常,定义浮点型变量时,使用double型。

//3. 字符型:char (1字符=2字节)
//① 定义char型变量,通常使用一对'',内部只能写一个字符
char c1 = 'a';
//编译不通过
//c1 = 'AB';
System.out.println(c1);

char c2 = '1';
char c3 = '中';
char c4 = 'ス';
System.out.println(c2);
System.out.println(c3);
System.out.println(c4);

//② 表示方式:1.声明一个字符 2.转义字符 3.直接使用 Unicode 值来表示字符型常量
char c5 = '\n';//换行符
c5 = '\t';//制表符
System.out.print("hello" + c5);
System.out.println("world");

char c6 = '\u0043';
System.out.println(c6);

//4.布尔型:boolean
//① 只能取两个值之一:true 、 false
//② 常常在条件判断、循环结构中使用
boolean bb1 = true;
System.out.println(bb1);
}
}

整数类型扩展

整数进制:

  • 二进制:0b10,对应对应十进制2
  • 十进制:10
  • 八进制:010,对应十进制8
  • 十六进制:0x10,对应十进制16

多个不同整数类型的变量相加时,若其中有一个long类型,则相加结果为long;否则都为int类型。

浮点数类型扩展

浮点数float类型占32字节,double类型占64字节,其位数有限、是离散的、有舍入误差的,因此容易出现数值溢出的情况。两个浮点数可能很接近但不严格相等。floatdouble类型的变量相加时,结果为double类型。

使用浮点数时应注意,银行业务中不能使用float和double类型,要使用BigDecimal类进行判断。

1
2
3
4
5
6
7
8
9
10
11
12
// 理论上f和d应该相等
float f = 0.1f;
double d = 1.0 / 10;

// 但是结果输出false
System.out.println(f == d);

float f1 = 2158452165841256123584f;
float f2 = f1 + 1;

// 结果输出true
System.out.println(f1 == f2);

字符类型

编码类型:Unicode 2字节,范围0-65536

转义字符:

  • \t 制表符
  • \n 换行