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 ;                                                 //定义 int 型变量 sum 
        for(int i = 0 ; i < str.length ; i++) {                       //循环遍历数组
            int myint = Integer.parseInt(str[i]);                     //将数组中的每个元素都转换为 itn 型
            sum = sum + myint;                                        //将数组中的歌元素相加
        }
        System.out.println("数组中各元素相加之和是:" + sum);           //将计算结果输出
    }
}

    运行结果为 :

数组中各元素相加之和是:164

    Integer 类的 toString() 方法,可将 Integer 对象转换为十进制字符串表示。 toBinaryString() 、toHexString() 和 toOctalString() 方法分别将值转换成二进制、十六进制和八进制字符串。

    eg :创建类,在主方法中创建 String 变量,实现将字符变量以二进制、十进制、十六进制和八进制输出。

 
 
public class Charac {                                                 //创建类
    public static void main(String[] args) {                          //主方法
        String str1 = Integer.toString(456);                          //获取数字的十进制表示
        String str2 = Integer.toBinaryString(456);                    //获取数字的二进制表示
        String str3 = Integer.toHexString(456);                       //获取数字的十六进制表示
        String str4 = Integer.toOctalString(456);                     //获取数字的八进制表示
        System.out.println(" '456' 的十进制表示为:" + str1 );
        System.out.println(" '456' 的二进制表示为:" + str2 );
        System.out.println(" '456' 的十六进制表示为:" + str3 );
        System.out.println(" '456' 的八进制表示为:" + str4 );
    }
}

    运行结果为 :

 '456' 的十进制表示为:456
 '456' 的二进制表示为:111001000
 '456' 的十六进制表示为:1c8
 '456' 的八进制表示为:710

    1.3  常量

    Integer 类提供了一下 4 个常量。

    ( 1 )MAX_VALUE :表示 int 类型可取的最大值,即 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 between booleans and Strings, 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 返回一个用指定的字符串表示值的 boolean 值

    eg :创建类,在主方法中以不同的构造方法创建 Boolean 对象,并调用 booleanValue() 方法将创建的对象重新转换为 boolean 数据输出。

 
 
public class GerBoolean {                                           //创建类
    public static void main(String[] args) {                        //主方法
        Boolean b1 = new Boolean(true);                             //创建 Boolean 对象 
        Boolean b2 = new Boolean("ok");                             //创建 Boolean 对象
        System.out.println("b1 :" + b1.booleanValue());
        System.out.println("b2 :" + b2.booleanValue());
    }
}

    运行结果为 :

b1 :true
b2 :false

    2.3  常量

    Boolean 提供了一下 3 个常量:

    ( 1 ) TRUE :对应基值 true 的 Boolean 对象 。

    ( 2 ) FALSE:对应基值 false 的 Boolean 对象 。

    ( 3 ) TYPE :基本类型 boolean 的 Class 对象 。




Guess you like

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