-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicType.java
More file actions
78 lines (61 loc) · 1.69 KB
/
Copy pathBasicType.java
File metadata and controls
78 lines (61 loc) · 1.69 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package basic.hello;
class FreshJuice {
enum FreshJuiceSize{ SMALL, MEDIUM , LARGE }
FreshJuiceSize size;
}
public class BasicType {
static void BasicTypePrint(){
//short,int,long,float,double
int num=1;
num+=2;
System.out.println("int num="+num);
//byte=[-128,127]或ASCII表示的字符
byte bb=127;
bb='A';
System.out.println("byte bb="+bb);
//char
char chr='z';
chr='b';
System.out.println("char chr="+chr);
//boolean
boolean isTrue=true;
isTrue=false;
System.out.println("boolean isTrue="+isTrue);
}
static void PrintEnumeration() {
//枚举
FreshJuice juice = new FreshJuice();
juice.size = FreshJuice.FreshJuiceSize.MEDIUM;
System.out.println("枚举 juice="+juice.size);
}
static void PrintConst() {
//在 Java 中使用 final 关键字来修饰常量,声明方式和变量类似
final double PI = 3.1415927;
System.out.println("常量final double PI = "+PI);
}
static void PrintChange() {
//自动类型转换方向
//byte,short,char—> int —> long—> float —> double
/* 数据类型转换必须满足如下规则:
* 1. 不能对boolean类型进行类型转换。
* 2. 不能把对象类型转换成不相关类的对象。
* 3. 在把容量大的类型转换为容量小的类型时必须使用强制类型转换。
* 4. 转换过程中可能导致溢出或损失精度
* 5. 浮点数到整数的转换是通过舍弃小数得到,而不是四舍五入
* */
int i1 = 123;
byte b = (byte)i1;//强制类型转换为byte
System.out.println("int强制类型转换为byte后的值等于"+b);
}
static void PrintVar() {
var type=1;
System.out.println("jdk10新加入的自动推测类型,var type="+type);
}
public static void main(String[] args) {
BasicTypePrint();
PrintEnumeration();
PrintConst();
PrintChange();
PrintVar();
}
}