Java Number Class
In this article we are reading about “Java Number Class”.
Java Number class is an abstract class which is placed in java.lang package. It has four abstract methods and two concretemethods. The abstract class Number is the superclass of classes BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, and Short. This class contains a single consructor number().
In Java language, we mostly work with a primitive data type, but Java also provides a wrapper class under the abstract class numbers in java.lang package, there are six subclasses under the class ‘numbers’.
The primitive data types are ‘wrapped’ under these Java classes for their corresponding objects. This wrapping is usually done by the compiler. When an object is converted into primitive type than it is called Autoboxing, and then again transferred to an object it is called Unboxing.
The following figure shows the class hierarchy for the number classes provided by the Java platform.

Example :
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public class NumberDemo {
public static void main(String args[]) { Float floatOne = new Float(14.78f - 13.78f); Float floatTwo = Float.valueOf("1.0"); Double doubleOne = new Double(1.0); int difference = floatOne.compareTo(floatTwo); if (difference == 0) { System.out.println("floatOne is equal to floatTwo."); } else if (difference < 0) { System.out.println("floatOne is less than floatTwo."); } else if (difference > 0) { System.out.println("floatOne is greater than floatTwo."); } System.out.println("floatOne is " + ((floatOne.equals(doubleOne)) ? "equal" : "not equal") + " to doubleOne."); } } |
output :
1
2 |
floatOne is equal to oneAgain.
floatOne is not equal to doubleOne. |
So in this article we have read about “Java Number Class”.
Comments
Post a Comment