Java Basic Interview (Knowledge Points)

Keywords: Programming Java

  • What are the differences between int and Integer? What are the basic types in Java and which reference types correspond to each other?

Answer: int is the basic type and Integer is the reference type. Integer is the encapsulation class of int.

int——Integer
float——Float
double——Double
byte——Byte
long——Long
char——Character
boolean——Boolean
short——Short

  • What is auto-disassembly? How to realize automatic disassembly and packing?

Answer: Boxing means that when the value of a basic type of data is assigned to the corresponding reference type, it automatically converts to the reference type without forcing it.

It's the same with unpacking.

//Boxing: Converting basic types to wrapper class objects
int i=10;
Integer x=new Integer(i);Manual boxing
Integer y=10;Automatic boxing
 
//Unpacking: Converting wrapped class objects to basic type values
Integer j=new Integer(8);
int m=j.intValue();//Manual dismantling
int n=j;//Auto-unboxing
  • What is the maximum range of Integer classes? How to calculate the number beyond this range?

Answer: The maximum range is 0x80000000, or 2147483647. Over this range can be used LONG, over the range of LONG can be used

BigInteger. 
  • Is Java value passing or reference passing?

Answer: First of all, we need to clarify what is value passing and what is reference passing.

Value passing, passing the value of the data (note that if you pass a reference type, the value here refers to the reference type itself).

Reference transfer, the address of the data that refers to the past.

package lang;

public class Test {


    public static void main(String[] args) {
        int i = 100;
        System.out.println("data i Initially for"+i);
        test(i);//For the base type, the value passed is not the i itself, so the i in the main method is unchanged.
        System.out.println("call test()After method i Value:"+i);
        System.out.println("----------------------------------");
        test1(i);//Although it's a reference type, it's also a passed value, because it's the wrapper type of the basic type, which is treated as the basic type int. So i in the main method hasn't changed either.
        System.out.println("call test1()After method i Value:"+i);
        System.out.println("----------------------------------");
        A a = new A();
        a.i = 10;
        System.out.println("A The initial data is:"+a);
        test2(a);
        System.out.println("call test2()After method i Value"+a);

    }

    public static void test(int i) {
        System.out.println("test()The data received by the method is"+i);
        i++;
        System.out.println("test()The data calculated by the method are as follows:"+i);
    }

    public static void test1(Integer i) {
        System.out.println("test1()The data received by the method is"+i);
        i++;
        System.out.println("test1()The data calculated by the method are as follows:"+i);
    }

    public static void test2(A a) {
        System.out.println("test2()The data received by the method is"+a);
        a.i++;
        System.out.println("test2()The data calculated by the method are as follows:"+a);

    }


}
class A{
    int i ;

    @Override
    public String toString() {
        return "A{" +
                "i=" + i +
                '}';
    }
}

The results are as follows:

So when the method parameter is the basic type, the value of the basic type is passed! When a method parameter is a reference type, it passes the value of the reference type itself!.

It doesn't matter whether it's a reference pass or a value pass, just figure out what's passed.

 

 

Posted by new_programmer on Thu, 24 Jan 2019 12:51:13 -0800