This article is for you to understand three questions
- What is a variable?
- How do I use variables?
- What are the specifications for variable naming?
Variable
The computer's memory is similar to the human brain, and the computer uses memory to store the data needed for computing.
Memory is like a hotel, different room types correspond to different data types, and memory stores data in a suitable space for the data as needed. This space for storing data is a variable.
Variable name |
memory Address |
value |
Name |
0x5c2a0c3f. |
Tom |
Age |
0XCD4D72A ... |
10 |
Variable names, memory, and stored values such as the table above ↑
You can see the memory address is long and inconvenient to remember, but the variable name can be like a nickname for a person, easy to remember
Steps to use a variable
Two dogs home after school, mother gave him a good meal, two dogs eat these meals need what steps?
- Need to prepare a bowl or plate first
- To put a dish or soup into a container.
- Pinch a dish or drink soup with a spoon
The steps for using variables are the same as for eating with two dogs:
- Declaring variables, requesting space in memory based on data type
- Assignment to store data in the corresponding memory space
- Use variables to remove data using
Use the following code to demonstrate the process of using variables:
public class DemoVariable{ public static void main(String[] args){ //1.声明变量 int a; //2.赋值 a=10; //3.使用变量(打印变量的值到控制台) System.out.println(a); }}
The above assignment statement looks much like the equation in math, but the assignment and the equals sign are completely different.
The function of the equals sign in programming is to put the value on the right side of the "=" into the variable on the left and remember the rule later.
You can also declare variables and assignments at the same time:
public class DemoVariable{ public static void main(String[] args){ //1.声明变量并赋值 int a=10; //2.使用变量(打印变量的值到控制台) System.out.println(a); }}
Summarize
Syntax for declaring variables:
Data type variable name;
Syntax for variable assignment:
Variable name = value;
- syntax to declare variables and variable assignments at the same time:
Data type variable name = value;
Naming rules for variables
1. In Java, the capitalization of variable names is strictly differentiated, like Java
and java
is two completely different variables
2. Allow characters of the following rules to be variable names
First character: 1. Letter 2. Underline 3.$ Symbol
The remainder: any number of 1. Number 2. Letter 3. Underline 4.$ Symbol
For example a
_
$_
a1
: $a
user_name
Variable names in 3.java cannot conflict with keywords
Life in the ancient people's name can not be casually up, if the names of conflicts with the emperor may be beheaded. The Java compiler is as overbearing as the ancient Emperors: "Don't follow my rules, hum, just wait for the error."
For example: public
static
void
class
These keywords and the type of data we're going to talk about later
4. To avoid meaningless variable names
For example: aa
bbb
z1
haha
Give it a try.
- Which of the following variable names is wrong? Why?
string
String
a+b
9variable
my name
- Can you run the following code in eclipse? Why?
public class Test1 { public static void main(String[] args) { String book; System.out.println(book); }}
- Can you run the following code in eclipse? Why?
public class Test2 { public static void main(String[] args) { int num=20; int num=15; }}
Encourage you to post your answers in your message, communicate with others, and search for related questions on Baidu
004 Variables in Java