Inbuilt Classes in Java
Built-In Classes of Primitive Types
To provide an enhanced support for primitive types, a class was created for each of them.
To support characters, a class named Character was derived from the Object class. The Character class is defined in the java.lang package.
To support Boolean values, a class named Boolean was derived from the Object class.
Java has an extensive support for numbers. To make it possible, a class named Number was derived from Object. Based on this Number class, the following classes were derived:
|Primitive||Equivalent java.lang Class|
Each of these classes is based on the Number class that is derived from the Object class. All these classes are part of the java.lang package.
Each Number-based class provides a method that can be used to convert its value to the primitive equivalent. These methods are:
|Primitive||java.lang Class||Conversion to the Primitive Type||Returns|
Each Number-based classes is equipped with two constants named MIN_VALUE and MAX_VALUE. The MAX_VALUE constant represents the highest value that a variable declared from that class can bear. The MIN_VALUE constant represents the lowest value that a variable declared from that class can bear.
Each Number-based class overrides the toString() and the equals() methods of their ancestor the Object class. Te overridden toString() method makes it possible to use a variable declared from their class as a string. The equals() method makes it possible to compare two variables declared and initialized from the same class.
Parsing a String
So far in previous lessons, to retrieve a value from a primitive type, we were using the Scanner class. Each Number-based class is equipped with a method that can be used to retrieve a value from the user and convert it to the value based on the class. These methods are:
|Primitive||java.lang Class||Method Parser||Returns|