int a,b,c; //声明三个整型a b cint a=10,b=12;初始化byte B = 22 初始化一个字节类型Bdouble pi =3.1415 //声明和分配值给pichar a = "a" //初始化char类型值为a
There are three types of variables in Java
- Local variables (local variables)
- Instance variable
- class/Static variable
Local variables
- A local variable is declared inside a method, constructor, or block.
- Local variables are created when the method, constructor, block is accessed, disappears at the time of completion
- Access modifiers cannot be used in local variables
- Local variables are visible only in methods, constructors, blocks
- Local variables are internally implemented at the level of the stack
- Local variable has no default value
- Local variables must be declared and initialized before they are used!!! Instance variable
- Instance variables are declared in a class, but in methods, constructors, or blocks
- When you allocate space for objects in the heap, an address is created for each instance variable value
- Instance variables are created when an object is created with the New keyword and destroyed when the object is destroyed
- Instance objects can be declared at the class level before or after use
- Access modifiers can be used in instance variables
- Instance variables can be accessed by all methods, constructors, blocks, and generally, making these variables privatized
- The instance variable has a default value, the number type is 0,boolean type is false, the object reference type is null, the value can be assigned at the time of declaration or in the constructor
- Instance variables can directly access variable names directly in the class, but in static methods they should use the full name Objectreference.variablename. class/Static variable
- A class variable (also known as a static variable) is declared in a class by the static keyword, but outside of a method, constructor, or block.
- In addition to declaring constants, static variables are seldom used. Constants are variables declared as public/private,final and static. Constant volume never changes the initial value.
- Class variables are created at the beginning of the program and destroyed when the program is destroyed
- Visibility is the same as instance variables
- The default value is the same as the instance variable
- Class variables can be accessed by the class name Classname.variablename.
- When declaring a class variable as public static final, the variable name (constant) is all uppercase. If the static variable is not public and final, the naming syntax is the same as the instance and local variables.
Java Variable type