Java foundation I (data type summary)

1, System description of basic data type
1. The boolean value is only true or false and cannot be replaced by 0. It is a reference type.
2. Byte memory 8-bit unsigned bit maximum storage 255 tables range - 128 ~ 127 packing class - byte
3. The Short memory is 16 bits. When there is no sign bit, the maximum storage range is 65536 -- 32768 ~ 32767. Packaging – Short
4. 32 bits in int memory. When there is no sign, the maximum storage is the 32nd power of 2 minus 1. The range of tables: from the 31st power of negative 2 to the 31st power of positive 2 minus 1. Packing class – Integer.
5. Long memory has 64 bits. When there is no sign bit, the maximum storage is the 64th power of 2 minus 1. The range of tables: from the 63rd power of negative 2 to the 63rd power of positive 2 minus 1. Packaging – long.
6. Float memory has 32 bits and the data range is 3.4e-45~1.4e38. F or F must be added after the number in direct assignment. Packaging – float.
7. Double memory has 64 bits, and the data range is 4.9e-324~1.8e308. D or D can be added or not added during assignment. Packing class – double.
Use quotation marks, char: 8-bit storage set. Can participate in addition, subtraction, multiplication and division, can also compare the size!! Packaging class – Character.
Summary: generally, it can be divided into two categories: Boolean type and numerical type. Numerical type can be divided into integer type and floating point type, in which character type can be treated separately. So Java only includes 8 basic data types

2, Packaging class understanding of data types
Why does each basic data type provide a corresponding wrapper class? The purpose is to abstract everything in Java into objects for easy use. Is the essential meaning of object-oriented.
1. Understanding of Boolean packaging class

//Serializable interface is a final modified class
public final class Boolean implements java.io.Serializable,
                                      Comparable<Boolean>{
     //Look at these two corresponding original objects. With the use of meta mode, multiple objects use a share of memory. As for what is "enjoy yuan" and the difference between it and single example, I won't say much here.
	 public static final Boolean TRUE = new Boolean(true);
	 public static final Boolean FALSE = new Boolean(false);
	 private final boolean value;
	 //Two constructors, it can be seen that it can be null. If you use the Boolean class
	 public Boolean(boolean value) {
        this.value = value;
    }
     public Boolean(String s) {
        this(parseBoolean(s));
    }
     public static boolean parseBoolean(String s) {
        return ((s != null) && s.equalsIgnoreCase("true"));
    }
    //jdk documents suggest using valueOf instead of new to create Boolean class objects. The Boolean object created by new is to continuously create a new instance object, while valueOf is to return the static member variable in the Boolean class, that is, the object using the meta mode.
    public static Boolean valueOf(String s) {
        return parseBoolean(s) ? TRUE : FALSE;
    }

    //1. Quality is used because if you want to insert the Boolean pointer into hashtable, if it is not quality, it may easily cause hash conflict. When calculating the hashcode of the matching object, the hashcode of each attribute is usually added and then hashed. If the quality is relatively small, it is easy to cause uneven hash distribution.
    //2. Maps can wrap Boolean objects. If a map contains other objects besides Boolean objects, it is easy to have conflicts if it is not handled properly
	 public static int hashCode(boolean value) {
        return value ? 1231 : 1237;
    }

}

Overall summary
1. Boolean is the basic data type, and Boolean is a class
2. Boolean objects generally exist in stack space, while Boolean objects exist in heap space
3. Boolean has two values: true and false. Boolean has null in addition to true and false

package com.jj;

public class Main {

    public static void main(String[] args) {

        // Here, valueof is used to create objects. The Boolean object created by new is to continuously create a new instance object
        //valueof returns the static member variables in the Boolean class
        Boolean a = Boolean.valueOf(true);
        //This and the previous sentences are case insensitive when using String variables as parameters for validation
        Boolean b = Boolean.valueOf("True");
        Boolean c = Boolean.valueOf("ASS");
        boolean x1 = a.booleanValue();
        boolean x2 = b.booleanValue();
        System.out.println("a  " + x1);
        System.out.println("b  " + x2);
        System.out.println("c = " + c);
        // Verify that the meta mode uses the same object
        boolean a1 = a.equals(b);
        // Definitely not the same object
        boolean b1 = a.equals(c);
        System.out.println("b1 = " + b1);
        System.out.println("a1 = " + a1);
        // Convert boolean to string
        String str1 = Boolean.toString(a);
        System.out.println("str1 = " + str1);
        // The source code is directly judged and then compared with true, so the printing is false
        boolean x5 = Boolean.parseBoolean("ASS");
        System.out.println("x5 = " + x5);


    }
}

2. Basic type of byte

//You can also see that it is a final modified class, which can only be used and cannot be inherited
public final class Byte extends Number implements Comparable<Byte>{
	public static final int SIZE = 8;   //Can only be one byte
	//Two constructors
	public Byte(byte value) {
        this.value = value;   //The value passed in to be of type Byte
    }
    public Byte(String s) throws NumberFormatException {
        this.value = parseByte(s, 10);  //The incoming request is a string that can be converted to Byte
    }
    //This Byte is cached
    private static class ByteCache {
        private ByteCache(){}

        static final Byte cache[] = new Byte[-(-128) + 127 + 1];//The length of the declared cache array is 256

        static {
            for(int i = 0; i < cache.length; i++)
                cache[i] = new Byte((byte)(i - 128));//Then - 128 ~ 127 are cached
        }
    }
    //Two methods of parsing strings
    public static byte parseByte(String s, int radix)
        throws NumberFormatException {
        //radix is the cardinality when parsing a string. Under this method, it has the meaning of parsing cardinality.
        int i = Integer.parseInt(s, radix);//Parse the string and return it, so s must be - 128 ~ 127 characters. Why use the wrapper class method of int to parse it
        if (i < MIN_VALUE || i > MAX_VALUE)
            throw new NumberFormatException(
                "Value out of range. Value:\"" + s + "\" Radix:" + radix);
        return (byte)i;
    }
    //It is also a decoding and transcoding method, converting String to Byte
    public static Byte decode(String nm) throws NumberFormatException {
        int i = Integer.decode(nm);
        if (i < MIN_VALUE || i > MAX_VALUE)
            throw new NumberFormatException(
                    "Value " + i + " out of range from input " + nm);
        return valueOf((byte)i);
    }
}

Explain the role of radix
b[0] = Byte.parseByte("11",2) =3
Indicates that the string 11 is represented as a hexadecimal number based on 2, and the byte value is 3. Here, 11 represents a binary number
Key int and Integer

public final class Integer extends Number implements Comparable<Integer> {
	
	 public static final Class<Integer>  TYPE = (Class<Integer>) Class.getPrimitiveClass("int");//Class instance of the original type int.
	 //All possible character sets that represent numbers as strings are cached.
	 final static char[] digits = {
        '0' , '1' , '2' , '3' , '4' , '5' ,
        '6' , '7' , '8' , '9' , 'a' , 'b' ,
        'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
        'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
        'o' , 'p' , 'q' , 'r' , 's' , 't' ,
        'u' , 'v' , 'w' , 'x' , 'y' , 'z'
    };
	//Two constructors
	 public Integer(int value) {
        this.value = value;
    }
    public Integer(String s) throws NumberFormatException {
        this.value = parseInt(s, 10);//It involves the conversion of String to int, which will be discussed in detail later.
    }
    //The method as explained in the Byte type above returns the string representation of the first parameter of the hexadecimal number specified by the second parameter. Handle various binary integers
    public static String toString(int i, int radix) {
        if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)             
        radix = 10;//The default is decimal 
        /* Use the faster version */         
        if (radix == 10) {             
            return toString(i);         
        }
        char buf[] = new char[33];         
        boolean negative = (i < 0);         
        int charPos = 32;
        //Turn it into a negative number for processing
        if (!negative) {             
            i = -i;         
        }
        while (i <= -radix) {             
            buf[charPos--] = digits[-(i % radix)];             
            i = i / radix;         
        }         
        buf[charPos] = digits[-i];
        if (negative) {             
            buf[--charPos] = '-';         
        }
        return new String(buf, charPos, (33 - charPos));     
    }
    //An example code will demonstrate this later. In fact, this is to wrap int type into Integer and then convert it into String string
     public static String toString(int i) {
        if (i == Integer.MIN_VALUE)
            return "-2147483648";
        int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
        char[] buf = new char[size];
        getChars(i, size, buf);
        return new String(buf, true);
    }
    //Combine with toString to form a method to convert it into a string
    static void getChars(int i, int index, char[] buf) {
        int q, r;
        int charPos = index;
        char sign = 0;

        if (i < 0) { //If i is a negative number, set the sign character of i to '-'.
            sign = '-';  //Determine positive and negative numbers
            i = -i;  //Convert negative numbers into positive numbers to improve efficiency 
        }

        // Generate two digits per iteration
        while (i >= 65536) { //If i is greater than 65536, the character corresponding to the number of ten bits and one bit is obtained each time. After judging the size of the value, take each number, and the larger number takes two digits at a time (large number operation consumes a lot)
            q = i / 100;
        // really: r = i - (q * 100);
            r = i - ((q << 6) + (q << 5) + (q << 2)); //Using bit operation, the last two digits of i are obtained each time, and the extraction processing is continuously cycled
            i = q;//Reassign and prepare for the next cycle 
            buf [--charPos] = DigitOnes[r]; //The corresponding character in the single digit set stored in r
            buf [--charPos] = DigitTens[r]; //The corresponding character in the ten digit set stored in r
        }

        // Fall thru to fast mode for smaller numbers
        // assert(i <= 65536, i);
        for (;;) { //In the case of I < 65536, the consumption of small number operation is small, so only one digit is taken at a time
            q = (i * 52429) >>> (16+3);//52429 / (2 * 19) is about 1. This design is to improve the accuracy
            r = i - ((q << 3) + (q << 1));  // r = i-(q*10) ... // Get the last two digits of I each time
            buf [--charPos] = digits [r];//Take the last digit  
            i = q;//Reassign and prepare for the next cycle  
            if (i == 0) break;
        }
        if (sign != 0) {
            buf [--charPos] = sign; //Set symbol
        }
    }
    //The following two are used to determine the length of the string.
    //sizeTable is defined to represent the maximum number of bits in an int, which is used to determine the length of an int.
    final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
                                      99999999, 999999999, Integer.MAX_VALUE };
    //Use the sizeTable definition above to determine the string representation length of the int number.           
    static int stringSize(int x) {
        for (int i=0; ; i++)
            if (x <= sizeTable[i])
                return i+1;
    }
    //Fried chicken is an important method!! ParseInt (string s, int radius) parses the string parameter into a signed integer using the cardinality specified by the second parameter. parseInt(String s) can only convert numeric strings to decimal numbers
     public static int parseInt(String s, int radix)
                throws NumberFormatException
    {
        /*
         * WARNING: This method may be invoked early during VM initialization
         * before IntegerCache is initialized. Care must be taken to not use
         * the valueOf method.
         */

        if (s == null) {//Parameter verification: check the correctness of parameters before calling the method.
            throw new NumberFormatException("null");
        }

        if (radix < Character.MIN_RADIX) {
            throw new NumberFormatException("radix " + radix +
                                            " less than Character.MIN_RADIX");
        }

        if (radix > Character.MAX_RADIX) {
            throw new NumberFormatException("radix " + radix +
                                            " greater than Character.MAX_RADIX");
        }

        int result = 0;
        boolean negative = false;
        int i = 0, len = s.length();//i represents the number of bits of s currently traversed
        int limit = -Integer.MAX_VALUE;//Sets the maximum value of an Integer whose minimum value is negative
        int multmin;
        int digit;

        if (len > 0) {//If the string length is greater than 0, the conversion is performed
            char firstChar = s.charAt(0);//Get first character
            if (firstChar < '0') { // Possible leading "+" or "-"
                if (firstChar == '-') {//Judge whether it is negative 
                    negative = true; 
                    limit = Integer.MIN_VALUE;//Convert the limit to the minimum value of Integer, which cannot be less than the minimum value of Integer  
                } else if (firstChar != '+')
                    throw NumberFormatException.forInputString(s);//If the first char is neither + nor -, an exception is thrown  

                if (len == 1) // Cannot have lone "+" or "-"
                    throw NumberFormatException.forInputString(s);//If there is only one symbol, an exception is thrown
                i++;
            }
            multmin = limit / radix;//Set the limit value under different base numbers  
            while (i < len) {//Carry out binary conversion
                // Accumulating negatively avoids surprises near MAX_VALUE
                digit = Character.digit(s.charAt(i++),radix);//Convert the numeric string into the required hexadecimal number, and use the tool class to operate and convert one character each time  
                if (digit < 0) {
                    throw NumberFormatException.forInputString(s);
                }
                if (result < multmin) {
                    throw NumberFormatException.forInputString(s);
                }
                result *= radix;
                if (result < limit + digit) {
                    throw NumberFormatException.forInputString(s);
                }
                result -= digit;
            }
        } else {
            throw NumberFormatException.forInputString(s);
        }
        return negative ? result : -result;//Returns a positive or negative number based on the sign
    }
    //See, the parseInt we often use is just a static method to help us formulate decimal rules
    public static int parseInt(String s) throws NumberFormatException {
        return parseInt(s,10);
    }
    //Powerful internal class caching mechanism? Internal character caching class
	private static class IntegerCache {
		//Lower bound of cache, - 128, immutable  
        static final int low = -128;
         //Cache upper bound, temporarily null
        static final int high;
        static final Integer cache[];//Using arrays to cache
        //Principle: initialize the array, put a certain range of integers into the cache array, and then judge the range when calling valueOf method, and then grab data from the cache array

        static {
            // high value may be configured by property
            // Cache last session, which can be configured through JVM properties
            int h = 127;
            String integerCacheHighPropValue =
                sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
            //Get, get the upper bound
            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;
			//Get all the data that can be saved in Integer and initialize the cache array
            cache = new Integer[(high - low) + 1];
            int j = low;
            //Cache all Integer data
            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() {}
    }
    //Also, we often use this method, and officials also recommend using this method to create objects
       public static Integer valueOf(int i) {
       //If i is in Integer cache, it will be fetched directly
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
       //Otherwise, create an instance directly
        return new Integer(i);
    }
}



Using Integer instance code


Long type source code

       public final class Long extends Number implements Comparable<Long> {
	@Native public static final long MIN_VALUE = 0x8000000000000000L;//The minimum value is 2 to the 63rd power - 1 
	@Native public static final long MAX_VALUE = 0x7fffffffffffffffL;//The maximum value is the 63rd power of - 2 
	public static final Class<Long>     TYPE = (Class<Long>) Class.getPrimitiveClass("long");
	//The implementation principle of toString method is the same as that of toString in Integer.
	
	//Convert to the string representation of the corresponding hexadecimal
	public static String toUnsignedString(long i, int radix) {
        if (i >= 0)
            return toString(i, radix);
        else {
            switch (radix) {
            case 2:
                return toBinaryString(i);//Convert Long to binary  

            case 4:
                return toUnsignedString0(i, 2);//Convert Long to hexadecimal  

            case 8:
                return toOctalString(i);//Convert Long to octal  

            case 10:
                /*
                 * We can get the effect of an unsigned division by 10
                 * on a long value by first shifting right, yielding a
                 * positive value, and then dividing by 5.  This
                 * allows the last digit and preceding digits to be
                 * isolated more quickly than by an initial conversion
                 * to BigInteger.
                 */
                long quot = (i >>> 1) / 5;,//Decimal system
                long rem = i - quot * 10;
                return toString(quot) + rem;

            case 16:
                return toHexString(i);

            case 32:
                return toUnsignedString0(i, 5);

            default:
                return toUnsignedBigInteger(i).toString(radix);
            }
        }
    }
    //Returns an unsigned value with BigInteger equal to the parameter
     private static BigInteger toUnsignedBigInteger(long i) {
        if (i >= 0L)
            return BigInteger.valueOf(i);
        else {
            int upper = (int) (i >>> 32);
            int lower = (int) i;

            // return (upper << 32) + lower
            return (BigInteger.valueOf(Integer.toUnsignedLong(upper))).shiftLeft(32).
                add(BigInteger.valueOf(Integer.toUnsignedLong(lower)));
        }
    }
    public static String toHexString(long i) {//Convert Long to hexadecimal  
        return toUnsignedString(i, 4);  
    }  
    public static String toOctalString(long i) {//Convert Long to octal  
        return toUnsignedString(i, 3);  
    }  
    public static String toBinaryString(long i) {//Convert Long to binary  
        return toUnsignedString(i, 1);  
    static int stringSize(long x) {//Show the string length of Long  
        long p = 10;  
        for (int i=1; i<19; i++) {//Multiply by ten each time for comparison  
            if (x < p)  
                return i;  
            p = 10*p;  
        }  
        return 19;  
    }  
    //This is similar to the implementation of Integer. Resolves a string parameter to a signed Integer.
    public static long parseLong(String s, int radix)
              throws NumberFormatException
    {
        if (s == null) {//Parameter test
            throw new NumberFormatException("null");
        }

        if (radix < Character.MIN_RADIX) {
            throw new NumberFormatException("radix " + radix +
                                            " less than Character.MIN_RADIX");
        }
        if (radix > Character.MAX_RADIX) {
            throw new NumberFormatException("radix " + radix +
                                            " greater than Character.MAX_RADIX");
        }

        long result = 0;
        boolean negative = false;//It's basically the same as Integer
        int i = 0, len = s.length();
        long limit = -Long.MAX_VALUE;
        long multmin;
        int digit;

        if (len > 0) {
            char firstChar = s.charAt(0);
            if (firstChar < '0') { // Possible leading "+" or "-"
                if (firstChar == '-') {
                    negative = true;
                    limit = Long.MIN_VALUE;
                } else if (firstChar != '+')
                    throw NumberFormatException.forInputString(s);

                if (len == 1) // Cannot have lone "+" or "-"
                    throw NumberFormatException.forInputString(s);
                i++;
            }
            multmin = limit / radix;
            while (i < len) {
                // Accumulating negatively avoids surprises near MAX_VALUE
                digit = Character.digit(s.charAt(i++),radix);
                if (digit < 0) {
                    throw NumberFormatException.forInputString(s);
                }
                if (result < multmin) {
                    throw NumberFormatException.forInputString(s);//Convert to a negative number for calculation, so use the < sign  
                }
                result *= radix;
                if (result < limit + digit) {
                    throw NumberFormatException.forInputString(s);
                }
                result -= digit;
            }
        } else {
            throw NumberFormatException.forInputString(s);
        }
        return negative ? result : -result;
    }
    public static long parseLong(String s) throws NumberFormatException {//Default to decimal  
        return parseLong(s, 10);  
    }  
    //The officially recommended method for creating Long objects
    public static Long valueOf(String s, int radix) throws NumberFormatException {  
        return Long.valueOf(parseLong(s, radix));  
    }  
    public static Long valueOf(String s) throws NumberFormatException  
    {  
        return Long.valueOf(parseLong(s, 10));  
    }
    //The default cache of Long is - 128 ~ 127, and the cache module is the same as Integer  
     private static class LongCache {
        private LongCache(){}

        static final Long cache[] = new Long[-(-128) + 127 + 1];

        static {
            for(int i = 0; i < cache.length; i++)
                cache[i] = new Long(i - 128);
        }
    }
    public static Long valueOf(long l) {
        final int offset = 128;
        //In the cache range, it is used directly
        if (l >= -128 && l <= 127) { // will cache
            return LongCache.cache[(int)l + offset];
        }
        return new Long(l);
    }
    public int hashCode() {//Rewrite hashcode() method, shift 32 bits to the right without sign, and multiply value itself  
        return (int)(value ^ (value >>> 32));  
    }  
    public boolean equals(Object obj) {  
        if (obj instanceof Long) {  
            return value == ((Long)obj).longValue();//The address values of value are compared, so they are equal in the cache range and unequal outside the cache range (except that two objects point to the same Long)  
        }  
        return false;  
    }    
}

Automatic packing / unpacking greatly facilitates the use of basic type data and their packaging.
Automatic packaging: the basic type is automatically converted to packaging class, for example (int > > integer)
Automatic unpacking: packaging class is automatically converted to basic type. For example (integer > > int)

Original blog

Added by justinjkiss on Tue, 08 Feb 2022 13:28:03 +0200