[Java basic learning] variable, constant type

variable

A java variable is the most basic storage unit in a program, and its elements include variable name, variable type and scope.

type varName [=value][{
    
    ,varName[=value]}];
//数据类型  变量名 =值;可以用逗号隔开来声明多个同类型变量
  int a,b,c;
        a=1;b=2;c=3;

Precautions:

Every variable has a type, and the type can be a basic type or a reference type.

The variable name must be a legal identifier.

A variable declaration is a complete statement, so every declaration must end with a semicolon.

Variable scope

public class PS {
    
    
    static int allClicks=0;   //类变量,前面有关键词“static”
    String str="hello world"; //实例变量,前面没有关键词
    
public void method(){
    
    
    int i=0;   //局部变量,只能在这个大括号内,不能出界
}

Local variable

In a single method, the method includes the main method, etc. The following int t variables cannot exceed the scope of public static void main(String[] args) {}

//main 方法
public class Demo06 {
    
    
    public static void main(String[] args) {
    
    
    //局部变量:必须声明和初始化值
    int i=10;
    System.out.println(i);//输出为10
}
     //其他方法
    public void add(){
    
    
        System.out.println(i);//不可输出,
    }
}

Instance variable

There are fewer keywords than the front of the class variable, which is defined outside the method

public class Demo06 {
    
    
    //属性:变量
    /*实例变量:不在方法内,从属于对象:
    如果不自行初始化,这个类型变为默认值,所有数值型初始默认值为 0 或者 0.0(浮点型);
    布尔值:默认是false
    除了基本类型,其余的默认值都是null;char为空格
      */
    String name;
    int age;      //name和age都是实例变量,在方法的括号之外,但可以在方法内输出

    //main 方法
    public static void main(String[] args) {
    
    

        //变量类型  变量名字 =new Demo08();
        Demo06 demo06 =new Demo06();
        System.out.println(demo06.age);//输出为0
        System.out.println(demo06.name);//输出为null
    }

Class variable

Preceded by keywords such as: static, etc.

public class Demo06 {
    
    

    //类变量 static
    static double salary=2500;
    //

    //main 方法
    public static void main(String[] args) {
    
    
        //局部变量:必须声明和初始化值
        int i=10;
        System.out.println(i);

        //类变量 static
        System.out.println(salary);//可输出为2500.0,上面如果去掉static就不可输出

    }
    //其他方法
    public void add(){
    
    
    }
}

constant

It can be understood as a special variable. After its value is set, it is not allowed to be changed during the running of the program.

Constant names generally use uppercase characters, and final must be added in front of them

final 常量名=final double PI=3.14;
public class Demo09 {
    
    

    //修饰符,不存在先后顺序,final static double PI=3.14也可以;
    static final double PI=3.14;
    //常量用final定义;

    public static void main(String[] args) {
    
    
        System.out.println(PI);//输出为3.14
    }
}

Variable naming convention

All variables, methods, and class names: see the name uprising, try to set the English corresponding to Chinese

Class member variables: lowercase first letter and camel case principle: such as yearSalary, except for the first word, the first letter of the following words is capitalized

Local variables: lowercase initials and camel case

Constant: uppercase letters and underscore: MAX_VALUE

Class name: initial capitalization and camel case principle: such as GoodMan, Demo01

Method name: lowercase initials and camel case principle: run(), main(), runRun()

Guess you like

Origin blog.csdn.net/weixin_44302662/article/details/113846860