Home >Java >javaTutorial >What should you pay attention to when using variables in java
Explanation
1. Each variable has a type. The type can be a basic type or a reference type.
2. The variable name must be a legal identifier.
3. A variable declaration is a complete statement, so each declaration must end with a semicolon.
Example
public class demo04 { //类变量 static double salary =2500; // 属性 变量 //实例变量:从属于对象;如果不进行初始化,这个类型的默认值为0,0.0。 //布尔值:默认值为false //除了基本类型,其余的默认值都是null; String name; int age; //main方法 public static void main(String[] args) { //局部变量:必须声明和初始化值; int i = 10; System.out.println(i); //使用实例变量 //变量类型 变量名字 = new Demo04(); demo04 demo04 = new demo04(); //alt+enter System.out.println(demo04.age); System.out.println(demo04.name); System.out.println(salary); } //其他方法 public void add(){ System.out.println(1); } }
The above is the detailed content of What should you pay attention to when using variables in java. For more information, please follow other related articles on the PHP Chinese website!