Appearance
Variables And Data Types
Variables
What are variables?
Variables are the concept of algebra in junior middle school mathematics. For example, in a simple equation, x and y are variables:
y = x +1
In Java, variables are divided into two types: basic type variables and reference type variables.
Let's discuss basic types of variables first.
In Java, variables must be defined before use. When defining a variable, you can give it an initial value. For example:
java
int x = 1;
The above statement defines a variable of type int
with the name x
and an initial value of 1
.
Not writing an initial value is equivalent to assigning a default value to it. The default value is always 0
.
Let’s look at a complete example of defining a variable and then printing the variable value:
java
public class Main {
public static void main(String[] args) {
int x = 100;
System.out.println(x);
}
}
Reassign Variables
An important feature of variables is that they can be reassigned. For example, for variable x
, first assign a value of 100
, then assign a value of 200
, and observe the results printed twice:
java
public class Main {
public static void main(String[] args) {
int x = 100; // first assign a value of `100`
System.out.println(x); // output the value
x = 200; // then assign a value of `200`
System.out.println(x); // output: 200
}
}
Notice that when you define variable x
for the first time, you need to specify the variable type int
, so use the statement int x = 100;
When reassigning for the second time, the variable x
already exists and cannot be redefined. Therefore, the variable type int
cannot be specified and the statement x = 200;
must be used.
Not only can variables be reassigned, they can also be assigned to other variables. Let's look at an example:
java
public class Main {
public static void main(String[] args) {
int n = 100;
System.out.println("n = " + n); //output: n = 100
n = 200;
System.out.println("n = " + n); //output: n = 200
int x = n;
System.out.println("x = " + x); //output: x = 200
x = x + 100; // x = 300
System.out.println("x = " + x); //output: x = 300
System.out.println("n = " + n); //output: n = 200
}
}
We analyze the code execution flow line by line:
Execute int n = 100;
, this statement defines variable n
and assigns a value of 100
Therefore, the JVM allocates a "storage unit" in memory for variable n
and fills in the value of 100
:
n
│
▼
┌───┬───┬───┬───┬───┬───┬───┐
│ │100│ │ │ │ │ │
└───┴───┴───┴───┴───┴───┴───┘
When n = 200;
is executed, the JVM writes 200
into the storage unit of variable n
. Therefore, the original value is overwritten, and the value of n
is now 200
:
n
│
▼
┌───┬───┬───┬───┬───┬───┬───┐
│ │200│ │ │ │ │ │
└───┴───┴───┴───┴───┴───┴───┘
When executing int x = n;
a new variable x
is defined and a value is assigned to x
. Therefore, the JVM needs to allocate a new storage unit to the variable x
and write the same value as the variable n
. The result is the value of the variable x
Also becomes 200
:
n x
│ │
▼ ▼
┌───┬───┬───┬───┬───┬───┬───┐
│ │200│ │ │200│ │ │
└───┴───┴───┴───┴───┴───┴───┘
When executing x = x + 100;
the JVM first calculates the value x + 100
on the right side of the equation, and the result is 300
(because the value of x
is 200
at this moment). Then, the result 300
is written into the storage unit of x
. Therefore, the variable x
The final value becomes 300
:
n x
│ │
▼ ▼
┌───┬───┬───┬───┬───┬───┬───┐
│ │200│ │ │300│ │ │
└───┴───┴───┴───┴───┴───┴───┘
It can be seen that variables can be assigned values repeatedly. Note that the equal sign =
is an assignment statement, not equal in the mathematical sense, otherwise x = x + 100
cannot be explained.
Basic Data Types
Basic data types are types that the CPU can directly perform operations on. Java defines the following basic data types:
- Integer types: byte, short, int, long
- Floating point type: float, double
- Character type: char
- Boolean type: boolean
What are the differences between these basic data types defined by Java? To understand these differences, we must briefly understand the basic structure of computer memory.
The smallest storage unit of computer memory is a byte. A byte is an 8-bit binary number, that is, 8 bits. Its binary representation range is from 00000000 ~ 11111111
, converted to decimal is 0 ~ 255
, converted to hexadecimal is 00 ~ ff
.
Memory cells are numbered starting from 0 and are called memory addresses. Each memory unit can be regarded as a room, and the memory address is the house number.
0 1 2 3 4 5 6 ...
┌───┬───┬───┬───┬───┬───┬───┐
│ │ │ │ │ │ │ │...
└───┴───┴───┴───┴───┴───┴───┘
One byte is 1byte, 1024 bytes is 1K, 1024K is 1M, 1024M is 1G, and 1024G is 1T. The number of bytes of a computer with 4T of memory is:
4T = 4 x 1024G
= 4 x 1024 x 1024M
= 4 x 1024 x 1024 x 1024K
= 4 x 1024 x 1024 x 1024 x 1024
= 4398046511104
Different data types occupy different numbers of bytes. Let's take a look at the number of bytes occupied by Java's basic data types:
┌───┐
byte │ │
└───┘
┌───┬───┐
short │ │ │
└───┴───┘
┌───┬───┬───┬───┐
int │ │ │ │ │
└───┴───┴───┴───┘
┌───┬───┬───┬───┬───┬───┬───┬───┐
long │ │ │ │ │ │ │ │ │
└───┴───┴───┴───┴───┴───┴───┴───┘
┌───┬───┬───┬───┐
float │ │ │ │ │
└───┴───┴───┴───┘
┌───┬───┬───┬───┬───┬───┬───┬───┐
double │ │ │ │ │ │ │ │ │
└───┴───┴───┴───┴───┴───┴───┴───┘
┌───┬───┐
char │ │ │
└───┴───┘
byte
is exactly one byte, while long
and double
require 8 bytes.
Integer
For integer types, Java only defines signed integers, so the highest bit represents the sign bit (0 represents a positive number, 1 represents a negative number). The maximum range that various integer types can represent is as follows:
- byte: -128 ~ 127
- short: -32768 ~ 32767
- int: -2147483648 ~ 2147483647
- long: -9223372036854775808 ~ 9223372036854775807
Let's look at an example of defining an integer type:
java
public class Main {
public static void main(String[] args) {
int i = 2147483647;
int i2 = -2147483648;
int i3 = 2_000_000_000; // Underlining makes it easier to identify
int i4 = 0xff0000; // 16711680 in hexadecimal
int i5 = 0b1000000000; // Binary representation of 512
long n1 = 9000000000000000000L; // Long type needs to add L at the end
long n2 = 900; // No L is added, here 900 is int, but the int type can be assigned to long
int i6 = 900L; // Error: cannot assign type long to int
}
}
Special note: The representation of the same number in different bases is exactly the same, for example, 15
= 0xf
= 0b1111
.
Floating Point
Floating-point numbers are decimals, because when decimals are expressed in scientific notation, the decimal point can "float". For example, 1234.5 can be expressed as 12.345x10^2 or 1.2345x10^3 , so it is called a floating-point number.
Here is an example of defining a floating point number:
java
float f1 = 3.14f;
float f2 = 3.14e38f; // 3.14x10^38 in scientific notation
float f3 = 1.0; // Error: The type without f at the end is double and cannot be assigned to float.
double d = 1.79e308;
double d2 = -1.79e308;
double d3 = 4.9e-324; // 4.9x10^-324 in scientific notation
For float
types, you need to add the f
suffix.
The range that floating point numbers can represent is very large. The float
type can represent a maximum of 3.4x10^38 , while the double
type can represent a maximum of 1.79x10^308 .
Boolean Type
The Boolean type boolean
has only two values: true
and false
. The Boolean type is always the calculation result of a relational operation:
java
boolean b1 = true;
boolean b2 = false;
boolean isGreater = 5 > 3; // true
int age = 12;
boolean isAdult = age >= 18; // false
The Java language does not specify the storage of Boolean types, because theoretically only 1 bit is needed to store Boolean types, but usually the JVM internally represents boolean
as a 4-byte integer.
Character Type
The character type char
represents a character. In addition to standard ASCII, Java's char
type can also represent a Unicode character:
java
public class Main {
public static void main(String[] args) {
char a = 'A';
char zh = '中';
System.out.println(a);
System.out.println(zh);
}
}
Note that the char
type uses single quotes '
and has only one character, which should be distinguished from the string type with double quotes "
.
Reference Type
Except for the variables of the above basic types, the rest are reference types. For example, the most commonly used reference type is String
:
java
String s = "hello";
Reference type variables are similar to pointers in C language. They store an "address" internally, pointing to the location of an object in memory. We will discuss this in detail later when we introduce the concept of classes.
Constant
When defining a variable, if you add final
modifier, the variable becomes a constant:
java
final double PI = 3.14; // PI is a constant
double r = 5.0;
double area = PI * r * r;
PI = 300; // compile error!
Constants cannot be assigned again after initialization when they are defined. Assigning again will cause a compilation error.
The purpose of constants is to use meaningful variable names to avoid magic numbers.
For example, instead of writing 3.14
everywhere in your code, define a constant. If we need to improve the calculation accuracy in the future, we only need to modify the definition of the constant, for example, to 3.1416
, without having to replace 3.14
everywhere.
To distinguish them from variables, constant names are usually all capitalized according to convention.
Var Keyword
Sometimes, the type name is too long, making it difficult to write. For example:
java
StringBuilder sb = new StringBuilder();
At this time, if you want to omit the variable type, you can use the var
keyword:
java
var sb = new StringBuilder();
The compiler will automatically infer that the type of variable sb
is StringBuilder
based on the assignment statement. To the compiler, the statement:
java
var sb = new StringBuilder();
In fact, it will automatically become:
java
StringBuilder sb = new StringBuilder();
Therefore, using var
to define variables only means less writing of the variable type.
Variable Scope
In Java, multi-line statements are enclosed with { ... }
. Many control statements, such as conditionals and loops, use { ... }
as their own scope, for example:
java
if (...) { // if start
...
while (...) { // while start
...
if (...) { // if start
...
} // if end
...
} // while end
...
} // if end
As long as these { ... }
are nested correctly, the compiler will recognize the beginning and end of the statement block. A variable defined in a statement block has a scope, starting from the point of definition to the end of the statement block. If these variables are referenced beyond the scope, the compiler will report an error. For example:
java
{
...
int i = 0; // Variable i is defined from here
...
{
...
int x = 1; // The variable x is defined from here
...
{
...
String s = "hello"; // The variable s is defined from here
...
} // The scope of variable s ends here
...
// Note that this is a new variable s, which has the same name as the variable above
// But because of the different scopes, they are two different variables:
String s = "hi";
...
} // The scope of variables x and s ends here
...
} // The scope of variable i ends here
When defining variables, follow the principle of scope minimization, try to define variables in the smallest possible scope, and do not reuse variable names.
Summary
Java provides two types of variables: basic types and reference types
Basic types include integers, floating point types, Boolean types, and character types.
Variables can be reassigned, and the equal sign is an assignment statement, not an equal sign in the mathematical sense.
Constants cannot be reassigned after initialization. Using constants facilitates understanding of program intent.