9 Java initialization and retrieval of face questions

Source: Internet
Author: User

How is the overloaded method distinguished in 1.Java?

Differentiated by the parameter types and order of overloaded methods.

Note: If the parameter type and order are the same, the compiler will give an error regardless of the parameter name, and the prompt method is already defined. and cannot be differentiated according to the return value type, if it is differentiated by the return value, sometimes the program calls the method does not need to return a value, then the program cannot determine the call that overloaded method.

2. Read the following procedure to explain the errors.

public static void Testlong (long i) {

SYSTEM.OUT.PRINTLN ("Test Long");

}

public static void TestFloat (float i) {

System.out.println ("Test float");

}

public static void Main (string[] args) {

Testlong (50);

TestFloat (1.5);

}

Testlong no problem, because the passed parameter 50 is of type int, and the receiver parameter is long, the small range can be automatically transformed into a wide range of data types; TestFloat will not compile, Because the passed parameter 1.5 is of type double, and the receiver parameter is of type float, a wide transition to a small range of data types requires an explicit conversion, that is, TestFloat (1.5f).

3. Read the following procedure to explain the errors.

public static class A {

A (int i) {

System.out.println ("A (int i)");

}

}

public static void Main (string[] args) {

A = new A ();

}

After you have defined a custom constructor, to use the default constructor, you need to explicitly specify the default constructor, otherwise a = new A ();

4. Read the following procedure to explain the errors.

public static class A {

A () {

System.out.println ("A ()");

}

A (int i) {

System.out.println ("A (int i)");

}

A (int i, int j) {

A ();

A (i);

System.out.println ("A (int i, int j)");

}

}

When invoking other constructors in a constructor, calls are made using the This keyword, such as this (), only one other constructor can be called in one constructor, and the statement that calls the other constructor is placed in the first row of the caller (that is, the constructor that emits the call behavior) of the statement block.

5. Read the following procedure to write the results of the execution.

public static class A {

private int i;

Private String J;

int Geti () {

return i;

}

String Getj () {

Return J;

}

A (int i) {

i = i;

}

A (String j) {

THIS.J = j;

}

}

public static void Main (string[] args) {

System.out.println (New A (5). Geti ());

System.out.println (New A ("Hello"). GETJ ());

}

The result of the execution is:

0

Hello

For i = i; This statement does not change the value of the instance variable I, and the default value of I is 0, so the result is also 0, if you need to change the value of the instance variable I, it needs to be changed to this.i = i;

6. In a class, declaring a number of static and non-static methods, tell us if the static method of the declaration can access the declared non-static method for a reason?

The static method cannot access a non-static method because the static method is a method that belongs to the class itself and is determined during compilation, and not the static method is the method that belongs to the object of the class and needs to be accessed after instantiation. If the non-static method is accessed in the static method, it cannot be compiled.

Why can't 7.static keywords decorate local variables?

The static keyword-decorated variable or method belongs to the class, is determined at compile time, and the ordinary variable or method belongs to the object generated by the class and needs to be instantiated before it can be determined. Therefore, if the static keyword modifies the local variables of the method, the method needs to be instantiated before it can be determined, while the static modified variables need to be determined at compile time, which can lead to contradictions.

What is the purpose of 8.finalize ()? Under what circumstances do I need to call this function?

When you call Finalize () where you need to free up memory, the memory that is consumed is reclaimed in the next garbage collection, and you typically do not need to call this function explicitly.

The garbage collector can only reclaim memory that is used by objects created by the new keyword, and some of the memory that is used in this way (such as calling C + + local methods) is recycled. Then you need to use Finalize (). Because the free () function is required to release memory in C + +, the Java program calls the Finalize () method to free memory when calling C + +.

9. List and briefly explain several common garbage collection techniques.

reference count: Each object contains a reference counter, each referenced once, the counter is incremented by 1, the reference is set to null or destroyed, and the counter is reduced by 1. The garbage collector polls, and once the value of the counter is found to be less than 1, the memory occupied by the object is reclaimed.

Stop replication: when the garbage collection mechanism runs, the program needs to stop running, moving each active object from one heap to another, and the garbage that is left is recycled.

tag cleanup: start with a stack and a static storage area, find a live object to mark it, and after all the tagging process is complete, garbage is recycled.

9 Java initialization and retrieval of face questions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.