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.

Methods of Number Class
The Number class in Java has many methods. Some of them are explained below.
compareTo( )
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
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.
- package demo;
- public class Demo {
- public staticvoid main(String args[]) {
- Integer a = 7;
- System.out.println(a.compareTo(2));
- System.out.println(a.compareTo(7));
- System.out.println(a.compareTo(9));
- }
- }
Output

equals( )
It will return the value false if they are unequal.
- package demo;
- publicclass Demo {
- public staticvoid main(String args[]) {
- Integer a = 6;
- Integer b = 9;
- Integer c = 6;
- Short x = 7;
- System.out.println(a.equals(b));
- System.out.println(a.equals(c));
- System.out.println(a.equals(x));
- }
- }
Output

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

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

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


rint( )
It will provide an Integer that is nearby to the argument.
double rint(double d)
Example
- package demo;
- public class Demo {
- public staticvoid main(String args[]) {
- double x = 10.7;
- double y = 10.5;
- double z = 10.1;
- System.out.println(Math.rint(x));
- System.out.println(Math.rint(y));
- System.out.println(Math.rint(z));
- }
- }
Output

random( )
It will provide a random number. The range will be between 0.0 and 1.0.
static double random()
Example
- package demo;
- public class Demo {
- public staticvoid main(String args[]) {
- System.out.println(Math.random());
- System.out.println(Math.random());
- System.out.println(Math.random());
- }
- }
Output

Summary
This article has explained the Number class in Java.

Comments
Join the conversation! Your thoughts help the community grow.