In this article, you are going to learn Number Classes in Java Programming Language.
In general, we use primitive data types such as byte, int, long, short, double, float etc. to perform arithmetic and other mathematical operations on numbers.
But when it comes to develop an application, there exists need to use number objects rather than using those primitive data types.
Also Read: Character Classes in Java Programming Language
This need is fulfilled by the wrapper classes for numbers that come along with the built-in Java.lang package. These classes are called number classes.
The number classes convert or wrap up primitive data types into objects and vice versa. This conversion is however supplemented by the java compiler which gives rise to the two new concepts i.e, Autoboxing and Unboxing.
AUTOBOXING AND UNBOXING
The wrapping up of a primitive data type in an object by the java compiler is known as ‘Autoboxing‘. And, unwrapping the number object back to a primitive data type is known as ‘Unboxing‘.
Example: class W3TrainingSchool { public static void main(String [] args) {Float a = 150f ; //Autoboxing a = a/7 ; // Unboxing System.out.println(“Division =” + a) ; } } |
Output: Division = 21.428572 |
PRIMITIVE DATA TYPES AND NUMBER CLASSES
Following is a list of primitive data types with their respective number/ wrapper classes used in java programming language.
Pimitive Data Type | Number/ Wrapper Class |
byte | Byte |
int | Integer |
float | Float |
long | Long |
short | Short |
double | Double |
The Number Class is broadly categorised into the following wrapper subclasses.
(A) Wrapper Classes for General Calculation
- Byte
- Integer
- Float
- Long
- Short
- Double
(B) Wrapper Classes for High Precision Calculation
- BigDecimal
- BigInteger
(3) Wrapper Classes for Multithreaded Application
- AtomicInteger
- AtomicLong
A pictorial representation of all the wrapper classes being the subclasses of the abstract class ‘Number’ is shown below.
INSTANCE METHODS USED IN NUMBER CLASSES
[1] Methods for Primitive Data Types
Syntax:
[1] Methods for Primitive Data Types
Syntax:
byte byteValue( ) short shortValue( )
int intValue( ) long longValue( ) float floatValue( ) double doubleValue( ) |
All these number methods convert the value of number objects to their respective primitive data types. They do not contain any parameters.
Return Value:
The above methods return primitive data types.
Example: class NumberMethods { public static void main(String args[]) {Integer a = 99 ;System.out.println(a.byteValue( )) ; System.out.println(a.shortValue( )) ; System.out.println(a.intValue( )) ; System.out.println(a.longValue( )) ; System.out.println(a.floatValue( )) ; System.out.println(a.doubleValue( )) ; } } |
Output: 99 99 99 99 99.0 99.0 |
[2] compareTo( ) Method
This method is used to compare number object with the argument(parameter).
Syntax:
int compareTo(Byte refByte) int compareTo(Double refDouble)
int compareTo(Float refFloat) int compareTo(Integer refInteger) int compareTo(Long refLong) int compareTo(Short refShort) |
Return Value:
There will be three possible return values :
- The method will return ‘0’ if the value of integer is equal to the argument.
- The method will return ‘1’ if the value of integer is greater than the argument.
- The method will return ‘-1’ if the value of the integer is less than the argument.
Example: class NumberMethods { public static void main(String args[]) { Integer a = 10 ; System.out.println(a.compareTo(10)) ; System.out.println(a.compareTo(5)) ; System.out.println(a.compareTo(15)) ; } } |
Output: 0 1 -1 |
There are several other instance methods used in number classes. They are listed below.
|
|
Also Read : String Classes in Java Programming
We have provided you the description on the use of Number Classes in Java Programming Language. Hope you like this post. For more updates and related information, stay connected to blogs on Java Programming Language.