Inbuilt Classes in Java

Posted By on November 6, 2014


Download PDF
Basics of Java
Inheritance in Java

Built-In Classes of Primitive Types

 

Introduction

To provide an enhanced support for primitive types, a class was created for each of them.

Character Values

To support characters, a class named Character was derived from the Object class. The Character class is defined in the java.lang package.

Boolean Values

To support Boolean values, a class named Boolean was derived from the Object class.

Numeric Values

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
byte Byte
short Short
int Integer
long Long
float Float
double Double

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
byte Byte byteValue() byte
short Short shortValue() short
int Integer intValue() int
long Long longValue() long
float Float floatValue() float
double Double doubleValue() double

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
byte Byte parseByte(String str) byte
short Short parseShort(String str) short
int Integer parseInteger(String str) int
long Long parseLong(String str) long
float Float parseFloat(String str) float
double Double parseDouble(String str) double
Basics of Java
Inheritance in Java

Download PDF

Posted by Akash Kurup

Founder and C.E.O, World4Engineers Educationist and Entrepreneur by passion. Orator and blogger by hobby

Website: http://world4engineers.com