java.lang

Type Wrapper Class

In java.long we have classes Boolean, Byte, Character, Double, Float, Integer, Long, Short. They correspond to the primitive classes boolean, byte, char, double, float, int, long, short respectively.

Why the type-wrapper classes are necessary?

Example : Double

We will take class Double as an example.
It extends Number class which has the following methods
Member Description
byte byteValue(), short shortValue(), int intValue(), long longValue(), float floatValue(), double doubleValue() Convert the value of this number object to the primitive date types of byte, short, int, long, float and double

Other useful methods

Constructors or members Description
static double MAX_VALUE,
static double MIN_VALUE
max/min value
static double NaN Not a number
static double NEGATIVE_INFINITY,
static double POSITIVE_INFINITY
.
public Double(double value),
public Double(String s) throws NumberFormatException
meaning is clear
String toString(),
static String toString(double d)
Returns a String representation.
static double parseDouble(String s) throws NumberFormatException,
static Double valueOf(String s) throws NumberFormatException
Returns a new double(or Double) initialized to the value represented by the specified String.
boolean equals(Object) determine if the object is equal to the Double
int compareTo(Double anotherDouble) Compares two Doubles numerically. The value is bigger then 0 if the Double is bigger than anotherDouble etc.

Other type-wrapper classes

Refer to the java document. They are similar to the previous classes. You are required to memorize the common members for the final exam.

Short questions

  1. What is the difference between valueOf and parseDouble?
  2. Use a short code to find the max possible value of long.
  3. What is the advantage of using wrapper-type classes?
    1. How can you change the double d = 3.15 to a Double?
    2. How can you change it to a float?
    3. Can you change it to int and how?
    4. How can you change it to a String ?

Answer

  1. valueOf returns the class Double.
    parseDouble returns the primitive type double.
  2. System.out.println(Long.MAX_VALUE);
  3. Refer to the beginning of the notes.
    1. use Double d2 = new Double(3.15) to change it to a Double.
    2. Use (float)3.15 (casting) or d2.floatValue().
    3. Yes. Use (int)3.15 (casting) or d2.intValue().
    4. d2.toString() or Double.toString(d)