variable

  • While the program is running, some temporary data may be generated at any time. The application stores this data in a number of memory cells, each of which is identified by an identifier. These memory units are called variables, the defined identifier is the variable name, and the data stored in the memory unit is the value of the variable.

  • Java is a strongly typed language, and every variable must be typed

  • Java variable is the most basic storage unit in the program, its elements include variable name, variable type and scope

// Define variable data type variable name;
int age;
// It is not recommended to declare multiple variables of the same type, separated by commas
int year,month,day;

// Reference type String definition, and initialize
string name = "The evil foam white.";
Copy the code

Notes:

  • Each variable has a type, which can be either a basic type or a reference type
  • The variable name must be a valid identifier.
  • A variable declaration is a complete statement, so each declaration must end with a semicolon

Variable types

  • Class variable: a variable that is independent of a method, usingstaticModification.
  • Instance variable: a variable that is independent of a method, but nostaticModification.
  • Local variables: variables in a method of a class.
public class Variable{
    static int allClicks = 0;    / / class variables
 
    String str = "hello world";  // Instance variables
 
    public void method(a){
        int i = 0;  // Local variables}}Copy the code

Java local variables

  • Local variables are declared in methods, constructors, or statement blocks.
  • Local variables are created when a method, constructor, or statement block is executed, and are destroyed when they are completed.
  • Access modifiers cannot be used with local variables;
  • A local variable is visible only in the method, constructor, or block in which it is declared.
  • Local variables are allocated on the stack.
  • Local variables do not have default values, so they must be initialized before they can be used.
public class Demo{
    / / the main method
    public static void main(String[] args){
        // Declare a local variable I
        int i;
       	// The local variable cannot be used directly. It must be initialized before it can be used. Otherwise, an error will be reported
        System.out.println(i);
        
        // Declare and initialize the value
        int j = 10; System.out.println(j); }}Copy the code

The instance variables

  • Instance variables are declared in a class, but outside of methods, constructors, and statement blocks.
  • When an object is instantiated, the value of each instance variable is determined;
  • Instance variables are created when the object is created and destroyed when the object is destroyed.
  • The value of an instance variable should be referenced by at least one method, constructor, or statement block so that information about the instance variable can be obtained externally in these ways;
  • Instance variables can be declared before or after use;
  • Access modifiers can modify instance variables;
  • Instance variables are visible to methods, constructors, or statement blocks in a class. In general, you should set instance variables to private. Instance variables can be made visible to subclasses by using access modifiers;
  • Instance variables have default values. The default value for a numeric variable is0, the default value for a Boolean variable isfalseThe default value for a reference type variable isnull. The value of a variable can be specified at declaration time or in the constructor;
  • Instance variables can be accessed directly by the variable name. But in static methods and other classes, you should use fully qualified names:ObejectReference.VariableName.
public class Dog{
    /* The instance variable name, which belongs to an object that is not initialized, will have a default value of this type */
    String name;
    
    /* The instance variable age, which belongs to an object that is not initialized, will have a default value of this type */
    int age;
  
   public static void main(String[] args){
       /* Create a dog object. Dog is a reference type, and dog is a local variable
       Dog dog = new Dog();
       
       // The name attribute of dog is an instance variable
       // Output: 0
       System.out.println(dog.age);
       
       // The name attribute of dog is an instance variable
       // Output: nullSystem.out.println(dog.name); }}Copy the code

Class variables (static variables)

  • Class variables are also called static variables and are used in classesstaticKeyword declaration, but must be outside the method.
  • No matter how many objects a class creates, a class only owns one copy of a class variable.
  • Static variables are rarely used except when they are declared as constants. Static variables are declared aspublic/private.finalstaticType. Static variables cannot be changed after initialization.
  • Static variables are stored in static storage. Often declared as a constant and rarely used alonestaticDeclare variables.
  • Static variables are created the first time they are accessed and destroyed at the end of the program.
  • Has similar visibility to instance variables. But in order to be visible to users of the class, most static variables are declared aspublicType.
  • Default values are similar to instance variables. The default value for a numeric variable is0, the default value for a Boolean isfalse, the default reference type isnull. The value of a variable can be specified at declaration time or in the constructor. In addition, static variables can be initialized in static statement blocks.
  • Static variables can pass: *ClassName.VariableName*.
  • Class variables are declared aspublic static finalType, it is recommended to use uppercase letters for class variable names. If the static variable is notpublicfinalType, which is named in the same way as instance variables and local variables.
public class Employee {
    Salary is a static private variable
    private static double salary;
    // DEPARTMENT is a constant
    public static final String DEPARTMENT = "Developer";

    public static void main(String[] args) {
        // We can define it first and then use it after initialization
        salary = 10000;
        // Average developer salary :10000.0
        System.out.println(DEPARTMENT + "Average salary :"+ salary); }}Copy the code

constant

  • Constant: a value that cannot be changed after initialization! A value that cannot be changed.
  • All constants can be thought of as special variables whose values, once set, cannot be changed during the course of the program.
  • Although constant names can also be in lower case, constants are usually represented in uppercase letters for ease of identification.

In Java, we use the final keyword to modify constants ina similar way to variables:

final double PI = 3.1415927;
Copy the code

Naming conventions for variables

  • All variable, method, class names: see the name
  • Class member variables: lowercase and hump principles:monthSalary
  • Local variables: start with a lowercase letter and the hump principle
  • Constants: Uppercase letters and underscores:MAX_VALUE
  • Class names: Uppercase and hump principles:Man,GoodMan
  • Method names: Lowercase and hump principles:run(),GetMaxValue()

The Hump rule: Capitalize all subsequent words except the first