Summary of common Java classes

Common Java classes

Packaging

Because the basic types in Java language are not object-oriented and do not have the nature of objects, it is inconvenient to use them in practice. Java in Java Lang package provides wrapper classes corresponding to eight basic types, which can be easily converted into objects for processing, and some methods can be called. The correspondence between basic types and wrapper classes in Java is shown in the following table:

Basic data type namePacking class name
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

From jdk1 Since version 5, the concepts of automatic packing and automatic unpacking have been introduced. Packing refers to converting the value of basic type into packing class object, and unpacking refers to converting the packing class object into the value of basic type. Automatic boxing and unpacking are very common in Java, and can occur during assignment or method call

The basic operation codes of packing and unpacking are as follows:

package com.Cuc.demo01;

/**
 * Boxing and UnBoxing 
 * @author ZpF
 */

public class BoxingAndUnBoxing {
    public static void main(String[] args) {
        Integer x=new Integer(10);//Manual packing
        Integer y=11;//Automatic packing
        int m=x.intValue();//Manual unpacking
        int n=y;//Automatic unpacking
        System.out.println(m);
        System.out.println(n);
    }
}

Although automatic packing and unpacking can make the code concise, there are also some problems, which are explained by the following code.

package com.Cuc.demo01;

/**
 * A trap in auto boxing
 * @author ZpF
 */

public class BoxingAndUnBoxing {
    public static void main(String[] args) {
        /**
         * Java The value of - 128-127 will be cached, and this object will be reused within the range (the memory allocation is the same address)
         */
        Integer i1=10;
        Integer i2=10;
        System.out.println(i1==i2);//true i1 and i2 both point to the same address in memory, so they are the same object
        System.out.println(i1.equals(i2));//true

        /**
         * Java The value of - 128-127 will be cached. Beyond this range, an object will be re created
         */
        Integer i3=210;
        Integer i4=210;
        System.out.println(i3==i4);//false 
        System.out.println(i3.equals(i4));//true
    }
}

Constants in wrapper classes

Each wrapper class provides some constants. Typical constants are as follows:

nameexplain
MAX_VALUEMaximum
MIN_VALUEminimum value
SIZEThe number of bits occupied in the form of binary complement
BYTESThe number of bytes occupied in the form of binary complement
TYPE**class object of type

Each different packaging class provides different constants. The above is only an example, and some of them will be explained. When they are really used, they will need to be analyzed in detail.

Construction method of packaging class

Integer class, Double class and Boolean class provide two construction methods.

  1. Create an object with the value of the basic data type as the input parameter.
  2. Create an object with a String as an input parameter.

The example code is as follows:

package com.Cuc.demo01;

/**
 * Construction method of packaging class
 */
public class Consructor {
    public static void main(String[] args) {
        Integer i1=new Integer(10);
        Integer i2=new Integer("10");
        Double i3=new Double(20.1);
        Double i4=new Double("20.1");
        Boolean i5=new Boolean(true);
        Boolean i6=new Boolean("false");
    }
}

The Character class only provides a construction method to create a Character class object based on the Character.

package com.Cuc.demo01;

/**
 * Character Construction method of packaging class
 */
public class Consructor {
    public static void main(String[] args) {
        Character i7=new Character('Handsome');
    }
}

Common methods in packaging

Each packaging class provides many common methods. Consult relevant materials and select appropriate methods when necessary.

Math class

Math class is provided in Java to deal with complex mathematical operations, such as square root, logarithm, trigonometric function, exponent, etc. Math class also provides some common mathematical constants, such as PI, E, etc. The methods in the math class are defined as static static methods, which directly use math Method format call (method stands for method name). Math class has no construction method, that is, math class cannot instantiate objects, because mathematics itself is abstract and there is no concrete instantiation object. The math class is Java Lang base package.

enumeration

Enumeration types are very common in programming languages. It is in jdk1 The feature introduced in version 5 is a special data type. Enumeration types combine a series of predefined constants with the same meaning, each of which has a defined value of the same type.

Enumeration Definition

The definition format of enumeration types in Java is as follows:

public enum [Enumeration class name]{
[Enumeration value code block]
}

Because enumerations, like Class classes, need to be accessed externally, the permission modifiers are public; enum is the keyword of enumeration type; The name of the enumeration Class is the same as that of the Class definition. Generally, the first letter is capitalized. Because it represents a constant, the fields of the enumeration type are generally all capitalized. The name and content of each enumeration value are defined in "[enumeration code block]".

The following is an example of the definition of color enumeration:

package com.Cuc.demo01;

/**
 * Enumeration value definition
 * @author ZpF
 */
public enum ColorEnum {
    RED,GREEN,YELLOW,BLUE;
}

The enumeration value can be accessed directly through "enumeration class name. Enumeration value name".

Common methods of enumerating

Objects of enumeration type inherit Java Lang. enum class. Common methods in enumeration types are as follows:

methodFunction description
values()Returns the enumeration type member property as an array
valueOf()Convert normal characters to enumerated objects
compareTo()Compare the order of two enumeration objects at the time of definition
ordinal()Gets the index location of the enumeration member

Code example:

package com.Cuc.demo01;

/**
 * enum use
 * @author ZpF
 */
public class UseNum {
    public static void main(String[] args) {
        ColorEnum colorArray[]=ColorEnum.values();
        for (int i = 0; i < colorArray.length; i++) {
            System.out.println("Print enumeration type members in turn"+colorArray[i]);

        }
        System.out.println("RED and GREEN The comparison results are:"+ColorEnum.RED.compareTo(ColorEnum.GREEN));
        for (int i = 0; i < colorArray.length; i++) {
            System.out.println("Get the index locations of enumeration type members in turn:"+colorArray[i].ordinal());
        }
    }
}

Operation results:

You can also add a custom constructor to an enumeration type, but the constructor must be private and modified by the keyword private.

package com.Cuc.demo01;

/**
 * Enumeration value definition
 * @author ZpF
 */
public enum ColorEnum {
    RED("gules"),GREEN("green"),YELLOW("yellow"),BLUE("blue");
    public String color;

    private  ColorEnum() {
    }

    private  ColorEnum(String color) {
        this.color = color;
    }
}

package com.Cuc.demo01;

/**
 * enum use
 * @author ZpF
 */
public class UseNum {
    public static void main(String[] args) {
        ColorEnum colorArray[]=ColorEnum.values();
        for (int i = 0; i < colorArray.length; i++) {
            System.out.println("Print enumeration type members in turn:"+colorArray[i].color);

        }

    }
}

Operation results:

Keywords: Java JavaSE intellij-idea

Added by mcrbids on Tue, 11 Jan 2022 00:24:52 +0200