Java basic data types

1. What are the basic data types of Java which occupy several bytes each?

Remember according to the formula:

● Data type: byte short int long float double boolean char

● Number of bytes occupied: 1284812 (byte corresponds to 1, short corresponds to 2, and so on)

2. Is String a basic data type?

You can see from the JDK source code that Stirng is a class, a reference type, and the bottom layer is implemented with a char array.

3. Is short s1 = 1; s1 = s1 + 1; wrong? Short s1 = 1; s1 += 1 is wrong?

The former is incorrect and the latter is correct.

For short s1 = 1; s1 = s1 + 1; Since 1 is an int type, the result of the s1+1 operation is also an int type, and the type needs to be cast to assign a value to the short type.

And short s1 = 1; s1 += 1; can compile correctly, because s1+= 1; is equivalent to s1 = (short)(s1 + 1); there is an implicit type conversion.

4. What is the difference between int and Integer?

Java is a fully object-oriented programming language, but for the convenience of programming, basic data types are introduced. In order to be able to treat these basic data types as objects, Java introduces a corresponding wrapper class for each basic data type. , The packaging class of int is Integer. Since Java 5, an automatic boxing/unboxing mechanism has been introduced to make the two convertible.

Java provides packaging types for each primitive type:

● Basic data types: boolean, char, byte, short, int, long, float, double

● Packaging type: Boolean, Character, Byte, Short, Integer, Long, Float, Double

5. What is the result of the numerical comparison output of the following Integer type?

public class Test{
public static void main(String[] args) {
Integer f1 = 100, f2 = 100, f3 = 150, f4 = 150;
System.out.println(f1 == f2);
System.out.println(f3 == f4);
}
}

If you don't understand the principle, it is easy to think that both outputs are either true or false. The first thing to note is that the four variables f1, f2, f3, and f4 are all Integer object references, so the following == operation compares not values ​​but references. What is the essence of boxing? When we assign an int value to an Integer object, we will call the static method valueOf of the Integer class. If we look at the source code of valueOf, we will know what happened.


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

IntegerCache is an internal class of Integer, and its code is as follows:


private static class IntegerCache {
    
    
    static final int low = -128;
    static final int high;
    static final Integer cache[];

    static {
    
    
        // high value may be configured by property
        int h = 127;
        String integerCacheHighPropValue =
            sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
        if (integerCacheHighPropValue != null) {
    
    
            try {
    
    
                int i = parseInt(integerCacheHighPropValue);
                i = Math.max(I, 127);
                // Maximum array size is Integer.MAX_VALUE
                h = Math.min(I, Integer.MAX_VALUE - (-low) -1);
            } catch( NumberFormatException nfe) {
    
    
                // If the property cannot be parsed into an int, ignore it.
            }
        }
        high = h;

        cache = new Integer[(high - low) + 1];
        int j = low;
        for(int k = 0; k < cache.length; k++)
            cache[k] = new Integer(j++);

        // range [-128, 127] must be interned (JLS7 5.1.7)
        assert IntegerCache.high >= 127;
    }

    private IntegerCache() {
    
    }
}
 

Simply put, if the value of the integer literal is between -128 and 127, then the new Integer object will not be new, but the Integer object in the constant pool will be directly referenced, so f1 in the above interview questionThe result of f2 is true, and f3The result of f4 is false.

Guess you like

Origin blog.csdn.net/weixin_38019299/article/details/108095905