First acquaintance with Java (Java wrapper class - Integer and Boolean)

1. Integer

    The Integer class, Long class and Short class in the java.lang package respectively encapsulate the basic types int, long and short into one class. Since these classes are all subclasses of Number, the difference is that they encapsulate different data types, and their methods are basically the same. Take the Integer class as an example to introduce the integer wrapper class.

    The Integer class wraps a value of the primitive type int in an object. An object of this class contains a field of type int. In addition, the class provides several methods to convert between int types and Stirng types, as well as other constants and methods that are very useful when dealing with int types.

    1.1 Constructor

    The Integer class has the following two constructors.

    (1) Integer (int number) This method takes an int variable as a parameter to obtain an Integer object.

    eg : Create an Integer object with an int variable as a parameter.

Integer number = new Integer(7);

    ( 2 ) Integer ( String str ) This method takes a String variable as a parameter to obtain an Integer object.

    eg : Create an Integer object with a String variable as a parameter.

Integer number = new Integer("45");

    Use a numeric String variable as a parameter, such as 123, otherwise a NumberFormatException will be thrown.

    1.2 Common methods

    Common methods of the Integer class are as follows:

Common methods of the Integer class
method return value Function description
byteValue() byte Returns the value of this Integer as a byte
compareTo(Integer anotherInteger) int

Numerically compares two Integer objects. Returns 0 if the two values ​​are equal;

If the value of the calling object is less than the value of anotherInteger, a negative value is returned;

Returns a positive value if the value of the calling object is greater than the value of anotherInteger.

equals(Object IntegerObj) boolean Compares this object to the specified object for equality
intValue() int returns this Integer object as an int
shortValue() short returns this Integer object as a short
toString() String Returns a String object representing the value of this Integer
valueOf(String str) Integer Returns an Integer object holding the specified String value
parseInt(String str) int Returns the integer equivalent of the number contained in the string specified by str

    The parseInt() method in the Integer class returns the integer (int) value corresponding to the numeric string of this method.

    eg : Create a class, define a String array in the main method, convert the elements in the String type array to int type, and add the elements

public class Summation { //Create a class
    public static void main(String[] args) { //Main method
        String str[]={"89","12","10","18","35"}; //Define String array
        int sum = 0 ; //define the int variable sum
        for(int i = 0 ; i < str.length ; i++) { //loop through the array
            int myint = Integer.parseInt(str[i]); //Convert each element in the array to itn type
            sum = sum + myint; //add the song elements in the array
        }
        System.out.println("The sum of the elements in the array is: " + sum); //Output the calculation result
    }
}

    The running result is:

The sum of the elements in the array is: 164

    The toString() method of the Integer class converts an Integer object to a decimal string representation. The toBinaryString() , toHexString() and toOctalString() methods convert values ​​to binary, hexadecimal, and octal strings, respectively.

    eg : Create a class, create a String variable in the main method, and output the character variable in binary, decimal, hexadecimal and octal.

 
 
public class Charac { //Create class
    public static void main(String[] args) { //Main method
        String str1 = Integer.toString(456); //Get the decimal representation of the number
        String str2 = Integer.toBinaryString(456); //Get the binary representation of the number
        String str3 = Integer.toHexString(456); //Get the hexadecimal representation of the number
        String str4 = Integer.toOctalString(456); //Get the octal representation of the number
        System.out.println(" The decimal representation of '456' is: " + str1 );
        System.out.println(" The binary representation of '456' is: " + str2 );
        System.out.println(" The hexadecimal representation of '456' is: " + str3 );
        System.out.println(" The octal representation of '456' is: " + str4 );
    }
}

    The running result is:

The decimal representation of '456' is: 456
 The binary representation of '456' is: 111001000
 The hexadecimal representation of '456' is: 1c8
 The octal representation of '456' is: 710

    1.3 Constants

    The Integer class provides the following four constants.

    (1) MAX_VALUE: Indicates the maximum value that the int type can take, that is, 2³¹-1.

    (2) MIN_VALUE: Indicates the minimum value that the int type can take, ie -2³¹.

    (3) SIZE: Used to represent the number of bits in the int value in two's complement form.

    (4) TYPE: Represents a Class instance of the basic type int.

    eg : Create a class and output the constant value of the Integer class in the main method.

 
 
public class GetCon { //Create class
    public static void main(String[] args) { //Main method
        int maxint = Integer.MAX_VALUE; //Get the constant value of the Integer class
        int minint = Integer.MIN_VALUE;
        int intsize = Integer.SIZE;
        System.out.println(" The maximum value of the int type is: " + maxint); //Output the constant
        System.out.println(" The minimum value of int type is: " + minint);
        System.out.println("The number of bits of type int is: " + intsize);
    }
}
    The running result is:
The maximum value that the int type can take is: 2147483647
 The minimum acceptable value for the int type is: -2147483648
 The number of bits of type int is: 32

Two, Boolean

    The Boolean class wraps the value of the primitive type boolean in an object. An object value of type Boolean contains a field of type boolean. In addition, this class provides a number of methods for converting boolean and String to and from each other, as well as several other constants and methods that are useful when dealing with booleans.

    2.1 Constructor

    ( 1 ) Boolean ( boolean value )

    This method creates a Boolean object representing the value parameter.

    eg : Creates a Boolean object representing the value parameter.

Boolean b = new Boolean(true);
    ( 2 ) Boolean ( String str )

    This method creates a Boolean object with a String variable as a parameter. If the String parameter is not null and equals true ignoring case, assigns a Boolean object representing the true value, otherwise gets a false Boolean object.

     eg : Create a Boolean object with a String variable as a parameter.

Boolean bool = new Boolean("ok");

    2.2 Common methods

    Common methods of the Boolean class are as follows:

Common methods of the Boolean class
method return value Function description
booleanValue() boolean Return the value of the Boolean object as the corresponding boolean value
equals(Object obj) boolean

Determines whether the object on which the method is called is equal to obj. if and only if the parameter is not null,

And it only returns true when it represents a Boolean object with the same boolean value as the object on which the method was called.

parseBoolean (String s) boolean Parse string argument to boolean value
toString() String Returns a String object representing the boolean value
valueOf(String s) boolean Returns a boolean value representing the value of the specified string

    eg : Create a class, create a Boolean object with different construction methods in the main method, and call the booleanValue() method to re-convert the created object to boolean data output.

 
 
public class GerBoolean { //Create class
    public static void main(String[] args) { //Main method
        Boolean b1 = new Boolean(true); //Create a Boolean object
        Boolean b2 = new Boolean("ok"); //Create a Boolean object
        System.out.println("b1 :" + b1.booleanValue());
        System.out.println("b2 :" + b2.booleanValue());
    }
}

    The running result is:

b1 :true
b2 :false

    2.3 Constants

    Boolean provides the following 3 constants:

    ( 1 ) TRUE : The Boolean object corresponding to the base value true.

    (2) FALSE: The Boolean object corresponding to the base value false.

    (3) TYPE: The Class object of the basic type boolean.




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325631648&siteId=291194637