[Java]Basic knowledge of interview

#[Java]Basic knowledge of interview
review it yourself

foreword

Looking for a job recently, discipline the basics of Java

1. Basic data types

1. Difference and conversion between int and Interger

the difference

1. Integer is a wrapper class for int, which is a basic data type of java
2. Integer variables must be instantiated before they can be used, while int variables do not need to be
3. Integer is actually a reference to an object. When a new Integer is created, a pointer is actually generated to point to the object; while int directly stores data values
4. The default value of Integer is null, and the default value of int is 0

Compare
1. Since an Integer variable is actually a reference to an Integer object, two Integer variables generated by new are never equal (because new generates two objects with different memory addresses).

   Integer a=new Integer(1);
   Integer b=new Integer(1);
   System.out.println(a==b);//false

2. When comparing an Integer variable with an int variable, as long as the values ​​of the two variables are equal, the result is true (because when the wrapper class Integer is compared with the basic data type int, java will automatically unpack it as int, and then compare, In fact, it becomes a comparison of two int variables)

  Integer a=new Integer(1);
//        Integer b=new Integer(1);
  int b=1;
  System.out.println(a==b);//true

3. When comparing an Integer variable not generated by new and a variable generated by new Integer(), the result is false. (Because the Integer variable generated by non-new points to the object in the java constant pool, and the variable generated by new Integer() points to the newly created object in the **heap**, the addresses of the two in memory are different)

Integer a=new Integer(1);
Integer b=1;
System.out.println(a==b);//false

4. For two non-new generated Integer objects, when comparing, if the value of the two variables is between -128 and 127, the comparison result is true, and if the value of the two variables is not in this interval, the comparison result false

      	Integer a=127;
        Integer b=127;
        System.out.println(a==b);//true
        Integer c=128;
        Integer d=128;
        System.out.println(c==d);//false


When java compiles Integer i = 100;, it will be translated into Integer i = Integer.valueOf(100);, and the definition of valueOf of Integer type in the java API is as follows:

 public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }

java will cache the number between -128 and 127. When Integer i = 127, it will cache 127. The next time you write Integer j = 127, it will be taken directly from the cache, and there will be no new

2.String class

Strings created by String are stored in the common pool, while String objects created by new are on the heap:

        String s1 = "data";              // String is created directly
        String s2 = "data";              // String is created directly
        String s3 = s1;                    // same reference
        String s4 = new String("data");   // String object creation
        String s5 = new String("data");   // String object creation

        System.out.println(s1==s3);

1.string.equal method

Let's see the result

     String s1 = "data";              // String is created directly
        String s2 = "data";              // String is created directly
        String s3 = s1;                    // same reference
        String s4 = new String("data");   // String object creation
        String s5 = new String("data");   // String object creation
        System.out.println(s1==s4);//false
        System.out.println(s1.equals(s4));//true

The == comparison is the memory address of the comparison, and the string equal function is called because it overrides equal.
String equal first compares whether it is the same object address, and if so, returns true, and then converts it to String type to compare the array of single characters.

 public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        }
        if (anObject instanceof String) {
            String anotherString = (String)anObject;
            int n = value.length;
            if (n == anotherString.value.length) {
                char v1[] = value;
                char v2[] = anotherString.value;
                int i = 0;
                while (n-- != 0) {
                    if (v1[i] != v2[i])
                        return false;
                    i++;
                }
                return true;
            }
        }
        return false;
    }

Summarize

Hint: Here's a summary of the article:

Tags: Java jvm Interview

Posted by tengkie on Sun, 04 Sep 2022 12:38:21 +0300