variable
Variable is the concept of junior high school algebra mathematics, for example, a simple equation, x, y are variables.
In Java variables are divided into two types:
- The basic types of variables;
- Reference type variable;
The basic types of variables;
Java, variables must be defined before use, you can define an initial value to variables such as:
you x = 1 ; you y = 2 ; you z;
This statement defines an integer int
type of variable does not write his initial value is equivalent to specify a default value, the default value is always 0;
Here is a complete definition of variables, variables and print examples:
public class the Main { public static void main (String [] args) { int X = 100; // definition of the variable value 100; System.out.println (X); // print variable values; } }
An important feature of variable is reassigned.
public class the Main { public static void main (String [] args) { int X = 100; // define a variable of type int x, and given an initial value 100 System.out.println (X); // print the value of the variable, observe whether the 100 X = 200; // reassign 200 System.out.println (X); // print the value of the variable to see whether 200 } }
Noting the first time the definition of variables x
when you need to specify the variable type int
, so use statement int x = 100;
. And when the second re-assignment, the variable x
already exists, and can not be redefined, and therefore can not specify the variable type int
, you must use the statement x = 200;
.
Variables can not only re-assigned, can also be assigned to other variables.
public class the Main { public static void main (String [] args) { int n = 100; // definition of the variable n, assigned to the same time 100 System.out.println ( "n =" + n); // print the value of n n = 200; // variable n is assigned 200 System.out.println ( "n =" + n); // print the value of n int x = n; // variable x assigned to n (n is 200, Thus after the assignment of the value of x is 200) System.out.println ( "= x" + x); // print the value of x x = x + 100; // variable x is assigned the value of x + 100 (x 200 , the value of x after the assignment is 100 = 300 + 200 is) System.out.println ( "= x" + x);// print the value of x System.out.println ( "n =" + n); // print again the value of n, n should be 200 or 300? } }
To analyze the following code execution flow;
Execution int n = 100;
, the statement defines a variable n
, and assigned 100
, and therefore, the JVM variables in memory n
allocated a "memory cell", fill in the values 100
:
Execution n = 200;
time, the JVM to 200
write the variable n
storage unit, therefore, the original values are overwritten, and now n
the value 200
:
Performed int x = n;
when a new variable is defined x
, while x
the assignment, and therefore, the JVM requires a newly assigned to the variable storage unit x
, and write the variable, and n
the same value, the result is a variable x
value also becomes 200
:
Execution x = x + 100;
time, the JVM is first calculated value of the right equation x + 100
, the result is 300
(Cos x
value 200
), then the result 300
written to x
the memory cell, therefore, a variable x
final value becomes 300
:
Visible, variables can be assigned repeatedly. Note that the equal sign =
is the assignment statement is not equal in the mathematical sense, otherwise it can not be explainedx = x + 100
.
Basic data types
The basic data types are CPU type of the operation can be carried out directly;
Java data types defined in the base;
-
整数类型:byte,short,int,long
-
浮点数类型:float,double
-
字符类型:char
-
布尔类型:boolean
内存单元从0开始编号,称为内存地址。每个内存单元可以看作一间房间,内存地址就是门牌号。
一个字节是1byte,1024字节是1K,1024K是1M,1024M是1G,1024G是1T;
不同的数据类型占用的字节数不一样。我们看一下Java基本数据类型占用的字节数:
byte
恰好就是一个字节,而long
和double
需要8个字节。
整形
对于整型类型,Java只定义了带符号的整型,因此,最高位的bit表示符号位(0表示正数,1表示负数)。各种整型能表示的最大范围如下:
- byte:-128 ~ 127
- short: -32768 ~ 32767
- int: -2147483648 ~ 2147483647
- long: -9223372036854775808 ~ 9223372036854775807
// 定义整型 public class Main { public static void main(String[] args) { int i = 2147483647; int i2 = -2147483648; int i3 = 2_000_000_000; // 加下划线更容易识别 int i4 = 0xff0000; // 十六进制表示的16711680 int i5 = 0b1000000000; // 二进制表示的512 long l = 9000000000000000000L; // long型的结尾需要加L } }
特别注意:同一个数的不同进制的表示是完全相同的,例如15
=0xf
=0b1111
。
浮点型
浮点类型的数就是小数,因为小数用科学计数法表示的时候,小数点是可以“浮动”的,如1234.5可以表示成12.345x102,也可以表示成1.2345x103,所以称为浮点数。
float f1 = 3.14f; float f2 = 3.14e38f; // 科学计数法表示的3.14x10^38 double d = 1.79e308; double d2 = -1.79e308; double d3 = 4.9e-324; // 科学计数法表示的4.9x10^-324
对于float
类型,需要加上f
后缀。
浮点数可表示的范围非常大,float
类型可最大表示3.4x1038,而double
类型可最大表示1.79x10308
布尔类型
布尔类型boolean
只有true
和false
两个值,布尔类型总是关系运算的计算结果:
boolean b1 = true; boolean b2 = false; boolean isGreater = 5 > 3; // 计算结果为true int age = 12; boolean isAdult = age >= 18; // 计算结果为false
Java语言对布尔类型的存储并没有做规定,因为理论上存储布尔类型只需要1 bit,但是通常JVM内部会把boolean
表示为4字节整数。
字符类型
字符类型char
表示一个字符。Java的char
类型除了可表示标准的ASCII外,还可以表示一个Unicode字符:
// 字符类型 public class Main { public static void main(String[] args) { char a = 'A'; char zh = '中'; System.out.println(a); System.out.println(zh); } }
注意char
类型使用单引号'
,且仅有一个字符,要和双引号"
的字符串类型区分开。
常量
定义变量的时候,如果加上final
修饰符,这个变量就变成了常量:
final double PI = 3.14; // PI是一个常量 double r = 5.0; double area = PI * r * r; PI = 300; // compile error
常量在定义时进行初始化后就不可再次赋值,再次赋值会导致编译错误。
常量的作用是用有意义的变量名来避免魔术数字(Magic number),例如,不要在代码中到处写3.14
,而是定义一个常量。如果将来需要提高计算精度,我们只需要在常量的定义处修改,例如,改成3.1416
,而不必在所有地方替换3.14
。
根据习惯,常量名通常全部大写。
var关键字
有些时候,类型的名字太长,写起来比较麻烦。例如:
StringBuilder sb = new StringBuilder();
这个时候,如果想省略变量类型,可以使用var
关键字:
var sb = new StringBuilder();
编译器会根据赋值语句自动推断出变量sb
的类型是StringBuilder
。对编译器来说,语句:
var sb = new StringBuilder();
等价于
StringBuilder sb = new StringBuilder();
因此,使用var
定义变量,仅仅是少写了变量类型而已。
变量的作用范围
在Java中,多行语句用{ }括起来。很多控制语句,例如条件判断和循环,都以{ }作为它们自身的范围,例如:
if (...) { // if开始 ... while (...) { while 开始 ... if (...) { // if开始 ... } // if结束 ... } // while结束 ... } // if结束
只要正确地嵌套这些{ },编译器就能识别出语句块的开始和结束。而在语句块中定义的变量,它有一个作用域,就是从定义处开始,到语句块结束。超出了作用域引用这些变量,编译器会报错。举个例子:
{ ... int i = 0; // 变量i从这里开始定义 ... { ... int x = 1; // 变量x从这里开始定义 ... { ... String s = "hello"; // 变量s从这里开始定义 ... } // 变量s作用域到此结束 ... // 注意,这是一个新的变量s,它和上面的变量同名, // 但是因为作用域不同,它们是两个不同的变量: String s = "hi"; ... } // 变量x和s作用域到此结束 ... } // 变量i作用域到此结束
定义变量时,要遵循作用域最小化原则,尽量将变量定义在尽可能小的作用域,并且,不要重复使用变量名。