Java programming

Keywords: Java

4, Object oriented (I)

1. What is the difference between object and object reference? Please give an example

public class FighterPlane{
    public FighterPlane{

    }
} 
FighterPlane fp = new FighterPlane();

1.new creates an Example object in heap space
2. () refers to calling the default constructor
3. Fighter plane FP creates class reference variables and stores them in stack space
4. = make the object reference point to the object just created
A reference can point to multiple objects, and an object can be referred to by multiple references


2. What are the characteristics of passing objects as parameters? Please give an example

The basic data type is passed as a parameter by value, while the object is passed by reference. When the object is passed as a parameter, the address of the object is passed.

//pass by value
public class ValuePass {
  private static int a;
  public static void main(String [] args) {
      modify(a); 
      System.out.println(a);
  }
  public static void modify(int a) {
       a++; 
  }
}//Result: 0

//Reference passing
class IntClass{
	public int value=10;
}
public class test {
	public static int a;
	public static void modify(IntClass Int) {
		Int.value=8;
	}
	public static void main(String[] args) {
		IntClass Int=new IntClass();
		System.out.println(Int.value);//10
		modify(Int);
		System.out.println(Int.value);//8
	}
}

3. What is the object initialization order? Please give an example

1. The system will initialize data members by default
2. Execute the initialization statement at the data member definition
3. Call the constructor to specify the initial value for the data member

class IntClass{
	int value;//Automatic initialization, the default value is 0
	//int value=5;// Specify initial value when defining
	
    public IntClass() {}
	
    //Define constructor initialization
	public IntClass(int val) {
		value=val;
	}
}
public class test {
    public static IntClass getInt(){
        //Call constructor
        IntClass s = new IntClass(8);
        return s;
    }
	public static void main(String[] args) {
		IntClass a =new IntClass();
		System.out.println(a.value);//0
        a=getInt();
        System.out.println(a.value);//8
	}
}

4. What is the difference between static and non static fields of a class? What scenarios should use the static modifier? Please give an example

Data members decorated with the static modifier are concrete objects that do not belong to any class, but static data members that belong to a class. It is stored in the common storage unit of the memory area of the class, rather than in the memory area of an object. Therefore, when any object of a class accesses it, it accesses the same value. The access method is to add a dot operator to the class name or an object reference.

Static data members are initialized only when the class is loaded and only once.  // 5 2

Static method is a class method, but it can be accessed by all objects. When referencing this method, you can use object name as prefix or class name as prefix. The code inside the static method can only access the static property or method in the class, and cannot directly access the non static property or method in the class (because it is an object method), but the non static method (object method) can access the static data member. The main method is a special static method and the entry point of the Application program. It must be written in the form of public static void main(String [] args).

 

  When a static field or static method of a class is accessed for the first time, the class (. class) is loaded and the static initialization statement is executed// 1 5 3

5. What are the functions of fina| modifiers in Java? Please give an example

Modifier attribute, the attribute is a constant. Example:

final int NUM = 20; 
NUM = 30; // Compilation error

Modify the method, the method cannot be overridden in subclasses (see the polymorphism section), which can prevent any inherited class from modifying this method and ensure the safety and correctness of the program. Example:

class Parent {
     public final void method1(){}//This method cannot be overridden by subclasses    
} 
class Child extends Parent { 
     public final void method1(){...}//Compilation error
}

Modify a class, the class cannot be inherited. Example:

class Parent {
}            
final class Person extends Parent{
} //You can inherit the Parent class        
class Child extends Person{
} //Compilation error, unable to inherit the Person class


6. Is the float[10] arr statement in Java correct? Why?

Wrong.

One dimensional array declaration form:

● type identifier array name []; (legal but not recommended)
● type identifier [] array name;
For example:
int[] arr;
String [] example;
myClass [] mc ;
Note that the array size cannot be specified in square brackets when declaring,
Such as float   [10] arr; It's wrong

7. What is the difference when the element type of Java array is basic type and reference type? Please give an example

Basic type

int[] arr = new int[10];  //Allocate 10 integer element spaces for array objects on the heap, and initialize each element to the default value of 0

reference type

String[] example = new String[10];  //The object reference array generates an array object with 10 cells. Each cell stores a reference to a String object. The initial value of all cells is null by default. At this time, no specific String object is created.

Posted by jonasr on Wed, 27 Oct 2021 07:12:03 -0700