Number Class in Java

Introduction

 
There is a special class known as the Number class. It is used when we want objects in place of data types.
 

Number Class in Java

 
In Java, we need to deal with objects. So we have the Number class in Java. The Number class contains all the wrapper classes in Java. So when we want objects in place of data types in Java, we use the Number class. The java.lang package contains the Number class in Java.
 
number class

Methods of Number Class

 
The Number class in Java has many methods. Some of them are explained below.
 

compareTo( )

 
The Number class object is compared with the argument by this method. We can compare Byte, Integer and so on. The Number and argument should be of the same data type for comparison.
 
Syntax
 
public int compareTo( NumberSubClass referenceName )
 
It will return the value zero if the argument is equal to the Integer.
It will return the value minus one if the argument is greater than the Integer.
It will return the value one if the argument is smaller than the Integer.
 
Example
  1. package demo;  
  2. public class Demo {  
  3.  public staticvoid main(String args[]) {  
  4.   Integer a = 7;  
  5.   System.out.println(a.compareTo(2));  
  6.   System.out.println(a.compareTo(7));  
  7.   System.out.println(a.compareTo(9));  
  8.  }  
  9. }  
Output
compareTo( )
 

equals( )

 
It is checked by this method that the object of the Number class and the argument are equal.
 
Syntax
 
public boolean equals(Object o) 
 
It will return the value true if they are equal.
It will return the value false if they are unequal.
 
Example
  1. package demo;  
  2. publicclass Demo {  
  3.  public staticvoid main(String args[]) {  
  4.   Integer a = 6;  
  5.   Integer b = 9;  
  6.   Integer c = 6;  
  7.   Short x = 7;  
  8.   System.out.println(a.equals(b));  
  9.   System.out.println(a.equals(c));  
  10.   System.out.println(a.equals(x));  
  11.  }  
  12. }  
Output
equals( )
 

abs( )

 
The Absolute value is returned.
 
Syntax
 
double abs(double d)
float abs(float f)
int abs(int i)
long abs(long lng)
 
Example
  1. package demo;  
  2. public class Demo {  
  3.  public staticvoid main(String args[]) {  
  4.   Integer a = -8;  
  5.   double d = -100;  
  6.   float f = -90;  
  7.   System.out.println(Math.abs(a));  
  8.   System.out.println(Math.abs(d));  
  9.   System.out.println(Math.abs(f));  
  10.  }  
  11. }  
Output
abs( )
 

ceil( )

 
It will provide an Integer as output. It will be equal to or greater than the argument.
 
Syntax
 
double ceil(double d)
double ceil(float f)
 
Example
  1. package demo;  
  2. public class Demo {  
  3.  public staticvoid main(String args[]) {  
  4.   double x = -10.777;  
  5.   System.out.println(Math.ceil(x));  
  6.  }  
  7. }  
Output
 
ceil( )
 

floor( )

 
It will also give an Integer as the output. It will be equal to or smaller than the argument.
 
Syntax
 
double floor(double d)
double floor(float f)
 
Example
  1. package demo;  
  2. public class Demo {  
  3.  public staticvoid main(String args[]) {  
  4.   double x = -10.777;  
  5.   System.out.println(Math.floor(x));  
  6.  }  
  7. }   
Output
floor( )
 

rint( )

 
It will provide an Integer that is nearby to the argument.
 
Syntax
  
double rint(double d)
 
Example
  1. package demo;  
  2. public class Demo {  
  3.  public staticvoid main(String args[]) {  
  4.   double x = 10.7;  
  5.   double y = 10.5;  
  6.   double z = 10.1;  
  7.   System.out.println(Math.rint(x));  
  8.   System.out.println(Math.rint(y));  
  9.   System.out.println(Math.rint(z));  
  10.  }  
  11. }  
Output
 
rint( )
 

random( )

 
It will provide a random number. The range will be between 0.0 and 1.0.
 
Syntax
 
static double random()
 
Example
  1. package demo;  
  2. public class Demo {  
  3.  public staticvoid main(String args[]) {  
  4.   System.out.println(Math.random());  
  5.   System.out.println(Math.random());  
  6.   System.out.println(Math.random());  
  7.  }  
  8. }   
Output
 
random( )
 

Summary

 
This article has explained the Number class in Java.