forked from Syske/learning-dome-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVarObject.java
More file actions
60 lines (53 loc) · 2.1 KB
/
VarObject.java
File metadata and controls
60 lines (53 loc) · 2.1 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
public class VarObject {
int globalVarInt; // 全局变量
char globalVarChar; // 全局
byte globalVarByte; // 全局
long globalVarLong; // 全局
short globalVarShort; // 全局
float globalVarFloat; // 全局
double globalVarDouble; // 全局
boolean globalVarBoolean; // 全局
/**
* 装箱
*/
private void function2() {
Character ch = 'c';
Integer i = 12;
Byte b = 12;
Short s = 123;
Long aLong = 123213L;
System.out.println("Character ch = " + ch);
System.out.println("Integer i = " + i);
System.out.println("Byte b = " + b);
System.out.println("Short s = " + s);
System.out.println("Long aLong = " + aLong);
// 手动装箱
Integer integer0 = new Integer(12);
System.out.println("Integer integer0 = " + integer0);
}
/**
* 拆箱
*/
private void function3() {
Integer i = new Integer(16);
int i2 = i;
int i3 = i.intValue(); // 不知道这个算不算手动拆箱
System.out.println("int i2 = " + i2);
System.out.println("int i3 = " + i3);
}
public void function() {
int localVarInt = 1; // 局部变量,必须初始化,否则会报编译错误
System.out.println(localVarInt);
System.out.println("全局变量globalVarInt的初始值:" + globalVarInt);
System.out.println("全局变量globalVarChar的初始值:" + globalVarChar);
System.out.println("全局变量globalVarByte的初始值:" + globalVarByte);
System.out.println("全局变量globalVarLong的初始值:" + globalVarLong);
System.out.println("全局变量globalVarShort的初始值:" + globalVarShort);
System.out.println("全局变量globalVarFloat的初始值:" + globalVarFloat);
System.out.println("全局变量globalVarDouble的初始值:" + globalVarDouble);
System.out.println("全局变量globalVarBoolean的初始值:" + globalVarBoolean);
}
public static void main(String[] args) {
new VarObject().function2();
}
}