Menu
Topics Index
...
`


Exploring java.lang > Primitive Type Wrappers >
Siva Nookala - 14 Apr 2016
The abstract class Number is the superclass of classes BigDecimal, BigInteger, Byte, Double, Float, Integer, Long and Short. Subclasses of Number must provide methods to convert the represented numeric value to byte, double, float, int, long and short.

Number Constructor :
ConstructorDescription
Number()Creates single Constructor.

Number Methods :
MethodDescription
byte byteValue() Returns the value of the specified number as a byte.
abstract double doubleValue() Returns the value of the specified number as a double.
abstract float floatValue()Returns the value of the specified number as a float.
abstract int intValue() Returns the value of the specified number as an int.
abstract long longValue() Returns the value of the specified number as a long.
short shortValue()Returns the value of the specified number as a short.

Number Test
class NumberTest
{
    public static void main(String arg[])
    {
        Integer i = new Integer(123456);
        Float f = new Float(789);
        Double d = new Double(3456);
        Long l = new Long(654321);
        
        System.out.println("Integer i value is converted into Byte : " + i.byteValue());
        System.out.println("Float f value is converted into Double : " + f.doubleValue()); // LINE A
        System.out.println("Long l value is converted into Float : " + l.floatValue()); // LINE B
        System.out.println("Integer i value is converted into Long : " + i.longValue()); // LINE C
        System.out.println("Float f value is converted into Short : " + f.shortValue());
    
    }
}
OUTPUT

Integer i value is converted into Byte : 64
Float f value is converted into Double : 789.0
Long l value is converted into Float : 654321.0
Integer i value is converted into Long : 123456
Float f value is converted into Short : 789

DESCRIPTION

In this program, we are converting an input required data type into specified data type.

THINGS TO TRY
  • In LINE A replace f by i and see the result which converts Integer into Double.
  • At LINE B replace l by i and see the result which converts Integer into Float.
  • In LINE C replace i by d and see the result which converts Double into Long.

Number instance methods :
MethodDescription
compareTo()Compares this Number object to the argument.
equals()Determines whether this number object is equal to the argument.
valueOf()Returns an Integer object holding the value of the specified primitive.
toString()Returns a String object representing the value of specified int or Integer.
parseInt()This method is used to get the primitive data type of a certain String.
abs()Returns the absolute value of the argument.
ceil()Returns the smallest integer that is greater than or equal to the argument. Returned as a double.
floor()Returns the largest integer that is less than or equal to the argument. Returned as a double.
rint()Returns the integer that is closest in value to the argument. Returned as a double.
round()Returns the closest long or int, as indicated by the method's return type, to the argument.
min()Returns the smaller of the two arguments.
max()Returns the larger of the two arguments.
exp()Returns the base of the natural logarithms, e, to the power of the argument.
log()Returns the natural logarithm of the argument.
pow()Returns the value of the first argument raised to the power of the second argument.
sqrt()Returns the square root of the argument.
sin()Returns the sine of the specified double value.
cos()Returns the cosine of the specified double value.
tan()Returns the tangent of the specified double value.
asin()Returns the arcsine of the specified double value.
acos()Returns the arccosine of the specified double value.
atan()Returns the arctangent of the specified double value.
atan2()Converts rectangular coordinates (x, y) to polar coordinate (r, theta) and returns theta.
toDegrees()Converts the argument to degrees.
toRadians()Converts the argument to radians.
toHexString()Returns a string holding the value of specified primitive.
toBinaryString()Returns a string that contains the binary equivalent of specified primitive.
toOctalString()Returns a string that contains the octal equivalent of specified primitive.
bitCount()Returns the number of set bits of specified primitive.
reverse()Reverse the order of the bits in specified primitive and returns the result.
rotateLeft(long num, int n)Returns the result of rotating in specified primitive numleft n positions.
rotateRight(long num, int n)Returns the result of rotating in specified primitive numright n positions.
signum()Returns -1 if specified primitive is negative, 0 if it is zero, and 1 if it is positive.

Number Method Test1
class NumberMethodTest1
{
    public static void main(String arg[])
    {
        Integer a = 5;
        Integer b = 6;
        String c = "8";    
        
        System.out.println("Compares two Intergers : " + a.compareTo(b));
        System.out.println("Checks for equal : " + a.equals(b));
        System.out.println("Converts to Float : " + Float.valueOf(b));
        System.out.println("Converts to String : " + Integer.toString(a));
        System.out.println("Converts String to Integer : " + Integer.parseInt(c));
    
    }
}
OUTPUT

Compares two Intergers : -1
Checks for equal : false
Converts to Float : 6.0
Converts to String : 5
Converts String to Integer : 8

DESCRIPTION

In this program, compareTo, equals, valueOf, toString and parseInt<cw> methods are used.

THINGS TO TRY
  • Give b value as 5 and check the output for compareTo and equals.

Subtract() method performs the subtraction of given two BigDecimal numbers.

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App