Numbers
Example :
int i = 5000;
float gpa = 13.65f;
double mask = 125;
However, in development, we come across situations where we need to use objects instead of primitive data types. In order to achieve this, Java provides wrapper classes.
All the wrapper classes (Integer, Long, Byte, Double, Float, Short>
are subclasses of the abstract class Number.

The object of the wrapper class contains or wraps its respective primitive data type. Converting primitive data types into object is called boxing, and this is taken care by the compiler. Therefore, while using a wrapper class you just need to pass the value of the primitive data type to the constructor of the Wrapper class.
And the Wrapper object will be converted back to a primitive data type, and this process is called unboxing. The Number class is part of the java.lang package.
Following is an example of boxing and unboxing −
Example :
public class Test {
public static void main(String args[]>
{
Integer x = 5; // boxes int to an Integer object
x = x + 10; // unboxes the Integer to a int
System.out.println(x>
;
}
}
Output :

When x is assigned an integer value, the compiler boxes the integer because x is integer object. Later, x is unboxed so that they can be added as an integer.
Number Methods :
- xxxValue(>
: This method converts the value of the Number Object that invokes the method to the primitive data type that is returned from the method. - This method returns the primitive data type that is given in the signature.
- compareTo(>
: The method compares the Number object that invoked the method to the argument. It is possible to compare Byte, Long, Integer, etc. However, two different types cannot be compared, both the argument and the Number object invoking the method should be of the same type. - referenceName − This could be a Byte, Double, Integer, Float, Long, or Short.
- If the Integer is greater than the argument then 1 is returned.
- If the Integer is equal to the argument then 0 is returned.
- If the Integer is less than the argument then -1 is returned.
- equals(>
: The method determines whether the Number object that invokes the method is equal to the object that is passed as an argument. - The method returns True if the argument is not null and is an object of the same type and with the same numeric value. There are some extra requirements for Double and Float objects that are described in the Java API documentation.
- toString(>
: The method is used to get a String object representing the value of the Number Object. If the method takes a primitive data type as an argument, then the String object representing the primitive data type value is returned.
If the method takes two arguments, then a String representation of the first argument in the radix specified by the second argument will be returned. - i − An int for which string representation would be returned.
- toString(>
− This returns a String object representing the value of this Integer. - toString(int i>
− This returns a String object representing the specified integer. - abs(>
: The method gives the absolute value of the argument. The argument can be int, float, long, double, short, byte. - Any primitive data type.
- This method Returns the absolute value of the argument.
- ceil(>
:The method ceil gives the smallest integer that is greater than or equal to the argument. - A double or float primitive data type.
- This method returns the smallest integer that is greater than or equal to the argument. Returned as a double.
- floor(>
:The method ceil gives the smallest integer that is greater than or equal to the argument. - A double or float primitive data type.
- This method returns the largest integer that is less than or equal to the argument. Returned as a double.
- round(>
:The method round returns the closest long or int, as given by the methods return type. - d − A double or float primitive data type.
- f − A float primitive data type.
- This method returns the closest long or int, as indicated by the method's return type, to the argument.
Example :
public class Test { public static void main(String args[]>
{ double d = 100.675; double e = 100.500; float f = 100; float g = 90f; System.out.println(Math.round(d>
>
; System.out.println(Math.round(e>
>
; System.out.println(Math.round(f>
>
; System.out.println(Math.round(g>
>
; } }
Try it HereOutput :
Syntax :
byte byteValue(>
short shortValue(>
int intValue(>
long longValue(>
float floatValue(>
double doubleValue(>
Return Value :
Example :
public class Test {
public static void main(String args[]>
{
Integer x = 5;
// Returns byte primitive data type
System.out.println( x.byteValue(>
>
;
// Returns double primitive data type
System.out.println(x.doubleValue(>
>
;
// Returns long primitive data type
System.out.println( x.longValue(>
>
;
}
}
Try it Here
Output :

Syntax :
public int compareTo( NumberSubClass referenceName >
Parameters :
Return Value :
Example :
public class Test {
public static void main(String args[]>
{
Integer x = 5;
//Integer value is greater than the argument (5>3>
so, output is 1
System.out.println(x.compareTo(3>
>
;
//Integer value is equal to the argument so, output is 0
System.out.println(x.compareTo(5>
>
;
//Integer value is less than the argument (5<8>
so, output is −1
System.out.println(x.compareTo(8>
>
;
}
}
Output :

Syntax :
public boolean equals(Object o>
Return Value :
Example :
public class Test {
public static void main(String args[]>
{
Integer x = 5;
Integer y = 10;
Integer z =5;
Short a = 5;
System.out.println(x.equals(y>
>
;
System.out.println(x.equals(z>
>
;
System.out.println(x.equals(a>
>
;
}
}
Try it Here
Output :

Syntax :
String toString(>
static String toString(int i>
Parameters :
Return Value :
Example :
public class Test {
public static void main(String args[]>
{
Integer x = 5;
System.out.println(x.toString(>
>
;
System.out.println(Integer.toString(12>
>
;
}
}
Try it Here
Output :

Syntax :
double abs(double d>
float abs(float f>
int abs(int i>
long abs(long lng>
Parameters :
Return Value :
Example :
public class Test {
public static void 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>
>
;
}
}
Try it Here
Output :

Syntax :
double ceil(double d>
double ceil(float f>
Parameters :
Return Value :
Example :
public class Test {
public static void main(String args[]>
{
double d = -100.675;
float f = -90;
System.out.println(Math.ceil(d>
>
;
System.out.println(Math.ceil(f>
>
;
System.out.println(Math.floor(d>
>
;
System.out.println(Math.floor(f>
>
;
}
}
Try it Here
Output :

Syntax :
double floor(double d>
double floor(float f>
Parameters :
Return Value :
Example :
public class Test {
public static void main(String args[]>
{
double d = -100.675;
float f = -90;
System.out.println(Math.floor(d>
>
;
System.out.println(Math.floor(f>
>
;
System.out.println(Math.ceil(d>
>
;
System.out.println(Math.ceil(f>
>
;
}
}
Try it Here
Output :

Syntax :
long round(double d>
int round(float f>