Java variable types, String correlation, printing multiplication table, class inheritance

1. The expected result of the following program

Compilation error:

str, i are local variables

  • Local variables are declared in methods, construction methods, or statement blocks;
  • Local variables are created when methods, constructors, or statement blocks are executed. When they are executed, the variables will be destroyed;
  • Access modifiers cannot be used for local variables;
  • Local variables are only visible in the method, construction method, or statement block that declares it;
  • Local variables are allocated on the stack.
  • Local variables have no default values, so after the local variables are declared, they must be initialized before they can be used.

Error: (8, 37) java: The variable str may not have been initialized

Error: (9, 35) java: The variable i may not have been initialized

public class StringTest {

    public static void main(String[] args) {
        String str;
        int i;
        System.out.println("str=" + str);
        System.out.println("i=" + i);
    }
}

Knowledge expansion, and JAVA also has instance variables and class variables (static variables):

【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 accordingly;
  • 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, construction method, or statement block, so that the external can obtain instance variable information through these methods;
  • 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 the class. In general, instance variables should be set as private. You can make instance variables visible to subclasses by using access modifiers;
  • Instance variables have default values. The default value of numeric variables is 0, the default value of Boolean variables is false, and the default value of reference variables is null. The value of the variable can be specified at the time of declaration or in the constructor;
  • Instance variables can be accessed directly through the variable name. But in static methods and other classes, you should use the fully qualified name: ObejectReference.VariableName.

[Class variable (static variable)]

  • Class variables are also called static variables, declared in the class with the static keyword, but must be outside the method construction method and statement block.
  • No matter how many objects a class creates, the class only has one copy of the class variables.
  • Static variables are rarely used except for being declared as constants. Constants refer to variables declared as publc/private, final and static types. The constant cannot be changed after initialization.
  • Static variables are stored in the static storage area. It is often declared as a constant and rarely uses static to declare variables alone.
  • Static variables are created at the beginning of the program and destroyed at the end of the program.
  • Similar visibility to instance variables. But in order to be visible to users of the class, most static variables are declared as public types.
  • The default values ​​are similar to instance variables. The default value of numeric variables is 0, the default value of booleans is false, and the default value of reference types is null. The value of the variable can be specified at the time of declaration or in the constructor. In addition, static variables can also be initialized in static statement blocks.
  • Static variables can be accessed through: ClassName.VariableName .
  • When a class variable is declared as a public static final type, the name of the class variable must use capital letters. If the static variable is not of public or final type, its naming method is consistent with the naming method of instance variables and local variables.

2. Print 9*9 multiplication table

    public void print9_9(){
        for(int i=1;i<=9;i++){
            for(int j=1;j<=i;j++)
                System.out.print(j+"x"+i+"="+(j*i)+" ");
            System.out.println();
        }
    }

3. Expected output result of class inheritance

[Explanation] After the subclass inherits the parent class, it obtains the properties and methods of the parent class. These properties and methods must be initialized before use. So you need to call the constructor of the parent class to initialize.

TellName in
Father printName
in Father tellName in
Son printName in Son

public class Father {
    public Father() {
        tellName();
        printName();
    }

    private void tellName(){
        System.out.println("Father中的tellName");
    }

    private void printName(){
        System.out.println("Father中的printName");
    }
}


public class Son extends Father {

    public Son() {
        tellName();
        printName();
    }

    private void tellName(){
        System.out.println("Son中的tellName");
    }

    private void printName(){
        System.out.println("Son中的printName");
    }

    public static void main(String[] args){
        Son s = new Son();
    }
}

 

Guess you like

Origin blog.csdn.net/sjmz30071360/article/details/88976174