#Variables are the basis of all code. In this article, let us learn about the creation and use of variables.
#1. Why use variables?
Variables are used to memorize data. It is a memory system
2. What is variable?
# A variable is a container, an area in memory used to store data.
For example: the memory is a hotel, the variable name is the room name, and the variable value is the person staying in the room
3. Variables three elements? How to declare a variable
Data type: The type of the variable, which determines the size of the variable space
Variable name: The variable name is easy for the computer to find This variable
The value of the variable: the stored data
For example: int number;
Data type | Description |
##char(Character type) | Used to store a single character For example: gender 'male', 'female' |
int (integer type) | is used to store integers |
double(double precision) | is used to store decimals |
String(string) | is used to store a string of characters such as "I like programming" |
4. How to use variables?
(1) Declare a variable int number;
(2) Assign number=3;# to the variable
##5. Variable naming rules and specifications?
Rules: The first letter is a letter, underscores '_', and '$' are consistent, and the rest is numbers, letters, underscores, and '$' symbolsSpecification: short and clearly indicating the function of the variable. Usually the first letter of the first word is lowercase, and the first letter of subsequent words is capitalized. For example: myName
#6. What should you pay attention to when using variables?
# After a variable is defined, it does not need to be assigned a value, but it cannot be used without assigning a value. It can only be used after it is assigned a value.Variables have a scope of use.
Variables cannot be redefined, such as: int i; and then another int i; cannot be defined.
.
public class Test1{ public static void main(String[] args){ int a=0; double b=0.00; char c='酷'; String d="我喜欢java"; System.out.println(a); System.out.println(b); System.out.println(c); System.out.println(d); } }Run the above example and the results are as follows:
0 0.00 酷 我喜欢java
This article has ended here. Please forgive me for any shortcomings. You can pay attention to more other exciting content. PHP Chinese website's
Java video tutorialThe above is the detailed content of Java Basics: Variables. For more information, please follow other related articles on the PHP Chinese website!