Use of Java Class Libraries

  Many functions of Java are implemented through class libraries, and the use of class libraries is the key to writing Java programs. Learn the basics of using class libraries by studying wrapper classes in java. In java, basic types are not used as objects, and java also provides convenient methods to wrap basic classes into objects. As shown

The wrapper class provides constructors, constants and conversion methods for handling different types. All these wrapper classes are stored in the java.lang package. The following figure shows their inheritance relationship.

 The Number class is the abstract parent class of the Double, Float, Long, Integer, Short, and Byte classes. About constants

The constant can be directly referenced through the class name: 1.MAX_VALUE: The maximum value corresponding to the basic type. 2.MIN_VALUE: For Byte, Short, Integer and Long, MIN_VALUE represents the minimum value; for Float and Double classes, MIN_VALUE represents the minimum positive value. 3:TYPE: The keyword of the basic data type corresponding to the class can be directly referenced through the class name. 4. TUSE and FALSE: Boolean constants

   The constructor has 1. Floating point constructor: public Double(double num); public Double(String str)

throws NumberFormatException;

  In addition to the characters f and d at the end of the character, it can only contain negative signs, dots (.) or numeric characters, otherwise a NumberFormatException will be thrown. Columns such as:

 Double d1 = new Double(3.1415); //Create a double object of 3.1415

 Double d2 = new Double("3.1415e-3"); //Create an object of 3.1415

2. Float constructor

public Float(double num );

public Float(float num);

public Float(String str)throws NumberFormatException;

  Float objects can be created from values ​​of type float or double, and they can also be created from string expressions of floating point numbers.

3. Integer constructor

public Byte(byte num);public Byte(String str)thorws NumberFormatException;

public Short(short num);public Short(String str)throws NumberDormatException;

public Integer(int num);public Integer(String str)throws NumberDormatException;

public Long(long num);public Long(String str)throws NumberDormatException;

4. Character constructor

public Character(char ch);//ch specifies the character wrapped by the created character (Character) object.

5. Boolean constructor

public Boolean(Boolean boolValue);//The value of boolean is true or false

public Boolean(String boolString);

  For Boolean(String boolString), the new Boolean object will be true if the string "true" (either uppercase or lowercase) is contained in the boolString, and false otherwise.

 The following are commonly used methods:
    1.public double doubleValue(); Returns the double value of the Double object.

Similar methods are floatValue(), byteValue(), intValue(), longValue(), shortValue() and

CharValue() etc.;

  2.public static double parseDouble(String s)throws NumberFormatException;

Converting a number string into a double number may throw NumberFormatException. Similar methods are parseByte(), parseShort(), parseInt(), parseLong(), and parseFlaot().

3.public static double valueOf(String s)thorws NumberFormatException;

Creates a new double object and initializes it to the value represented by the specified string, possibly throwing NumberFormatException. Columns such as:

Double doubleObject = Double.valueOf(“13.14”);

4.public String toString(); Convert the Double object to a string. The code below illustrates the parseInt() method. The program completes the sum of a series of integers entered by the user. In the program, use the readLine() method to read integers, and use the parseInt() method to convert these strings into corresponding integer (int) values.

  How can the above program print sum as 0 instead of inputting an integer, and it needs to input an integer to execute correctly. Otherwise, the output in the exception will be printed.

Guess you like

Origin blog.csdn.net/weixin_44591600/article/details/123805250
Recommended