The difficulty
primary
Learning time
Ten minutes
Suits the crowd
Zero basis
Development of language
Java
The development environment
- JDK v11
- IntelliJ IDEA v2018.3
1. What are the variables?
A variable is an abstract concept in computer language that can store the result of a calculation or represent a value.
Variables can be accessed by variable names.
We can get two things:
- Storing calculation results
- Access a variable by its name
Storing calculation results
Demo 1 + 1:
From the figure we can see that the result of 1+1 is stored in the sum variable.
Access a variable by its name
How do I access the result after 1+1?
We can access the value of sum, which is the value of 1+1.
2. Declare a variable
How do I declare a variable?
Declaring a simple variable requires only the data type and variable name:
Data types are covered in the next chapter. We need only know the int in this chapter. The variable name is sum.
A variable name is the name of the variable.
Initialize the
After declaring a variable, assigning a value to the variable is the initialization action.
Let’s assign sum:
Display output:
Running results:
The console prints 1.
3. Define a variable
So we just say a variable, and then we assign it, and all of these initialization operations can be done in one step, which is to define a variable.
Sum = sum; sum = sum; sum = sum; sum = sum
Then print sum:
Running results:
4. Declare variables or define variables?
In real development, do I use declared variables or defined variables?
The answer is both.
Recommend declaring variable practices for unknown;
Use the practice of defining variables for known recommendations.
5. Variables must be initialized before they can be used
The variable defined we know is declared + initialized. So there is no problem with it.
However, declared variables cannot be used directly. What does it mean to use? Use is to access the value inside a variable. Such as:
Run:
As can be seen, the runtime error, error cause:
Error:(9, 28) Java: the variable sum may not have been initialized
So remember to declare variables and then initialize them.
That’s all for now, but stay tuned for more.
Answering questions
If you have questions or want to learn more about cutting-edge technology, please leave them in the comments below, and I’ll answer them for you.
The previous chapter
Full Stack 2019 Java Chapter 11: Identifiers
The next chapter
Full Stack 2019 Java Chapter 13: Data Types
A study group
Join a synchronous learning group for mutual communication and progress.
- Method 1: Follow the headline number gorhaf, private message “Java study Group”.
- Method 2: follow the public account gorhaf, reply “Java learning group”.
Full stack engineer learning program
Follow us and join the “Full stack Engineer Learning Program”.
Copyright statement
Original is not easy, shall not be reproduced without permission!