« Previous Page Next Page »
    

Introduction to Java Numbers Class.

POSTED

AUTHOR: VARUN SINGH

There are various types of numbers used in Java programming language. In Java language, we mostly work with a primitive data type such as byte, int, long, double, etc. But Java also provides a wrapper class under the abstract class numbers in java.lang package.

All the wrapper classes (Integer, Long, Byte, Double, Float, Short) are subclasses of the abstract class Number.


java number class

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 boxing, and when again transferred to an object it is called Unboxing.

Example of Java Number:

public class NumberDemo {
   public static void main(String args[]) {
      Integer x = 6; // boxes int to an Integer object
      x =  x + 20;   // unboxes the Integer to a int
      System.out.println(x);
   }
}

Output:

26

Number Methods in Java:

There are various types of Number methods in Java, let's discuss them one by one:


java number methods

1. Java xxxValue() Method:

The method converts the value of the Number Object that invokes the method to the primitive data type that is returned from the method.

Syntax:

Syntax : 
byte byteValue()
short shortValue()
int intValue()
long longValue()
float floatValue()
double doubleValue()
Parameters : 
----
Returns :
the numeric value represented by this object
after conversion to specified type

Example:

//Java program to demonstrate xxxValue() method
public class TestValue
{
    public static void main(String[] args)
    {
        // Creating a Double Class object with value "6.9685"
        Double d = new Double("5.5555");   
        // Converting this Double(Number) object to different primitive data types
        byte b = d.byteValue();
        short s = d.shortValue();
        int i = d.intValue();
        long l = d.longValue();
        float f = d.floatValue();
        double d1 = d.doubleValue();            
        System.out.println("value of d after converting it to byte : " + b);
        System.out.println("value of d after converting it to short : " + s);
        System.out.println("value of d after converting it to int : " + i);
        System.out.println("value of d after converting it to long : " + l);
        System.out.println("value of d after converting it to float : " + f);
        System.out.println("value of d after converting it to double : " + d1);
    }
    
}

Output:

value of d after converting it to byte : 5
value of d after converting it to short : 5
value of d after converting it to int : 5
value of d after converting it to long : 5
value of d after converting it to float : 5.5555
value of d after converting it to double : 5.5555




2. Java compareTo() Method:

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.

Syntax:

public int compareTo( NumberSubClass referenceName )

Return Value:

• If the Integer is equal to the argument then 0 is returned.

• If the Integer is less than the argument then -1 is returned.

• If the Integer is greater than the argument then 1 is returned.


Example:

//Java program to demonstrate compareTo() method
public class TestCompare
{
   

 public static void main(String args[]) 
 {
      Integer i = 6;
      
      System.out.println(i.compareTo(4));
      System.out.println(i.compareTo(6));
      System.out.println(i.compareTo(9));            
   }
}

Output:

1
0
-1

3. Java parseInt() Method:

This method is used to get the primitive data type of a certain String. parseXxx() is a static method and can have one argument or two. Radix is used to return decimal(10), octal(8), or hexadecimal(16) etc representation as output.

Syntax:

Following are the syntax of this method:

static int parseInt(String s)
static int parseInt(String s, int radix)

Parameters:

s : This is a string representation of decimal.

radix : This is used to convert String s into integer.

Return Value:

parseInt(String s) : This returns an integer (decimal only).

parseInt(int i) : This returns an integer.

Example:

public class TestParse { 

   public static void main(String args[]) {
      int a =Integer.parseInt("7");
      double d = Double.parseDouble("3");
      int b = Integer.parseInt("333",14);

      System.out.println(a);
      System.out.println(d);
      System.out.println(b);
   }
}

Output:

7
3.0
633




4. Java equals() Method:

This method determine whether this Number object is equal to the argument.

Syntax:

public boolean equals(Object o)

Example:

public class TestEquals { 

   public static void main(String args[]) {
      Integer x = 6;
      Integer y = 11;
      Integer z =6;
      Short a = 6;

      System.out.println(x.equals(y));  
      System.out.println(x.equals(z)); 
      System.out.println(x.equals(a));
   }
}

Output:

false
true
false




5. Java - toString() Method:

The toString() method is used to get the Java String representation of any number, there are three variants in this method:

• Integer.toBinaryString(int i)

• Integer.toHexString(int i)

• Integer.toOctalString(int i)

which will return binary, hexa-decimal, octal string representation of specified integer(i) respectively.

Syntax:

String toString()
String toString(int i)

Example:

//Java program to demonstrate Integer.toString() 
//and Integer.toString(int i) method
public class TestString
{
    public static void main(String[] args)
    {
        // demonstrating toString() method
        Integer x = 14;
         
        System.out.println(x.toString()); 
         
        // demonstrating toString(int i) method
        System.out.println(Integer.toString(14));
         
        System.out.println(Integer.toBinaryString(140));
        System.out.println(Integer.toHexString(140)); 
        System.out.println(Integer.toOctalString(140));
    }
}

Output:

14
14
10001100
8c
214




The End:

Thank you for reading this article, and if you have any problem, have a another better useful solution about this article, please write message in the comment section. We will be very happy to hear that.

If you like my tutorial, please follow us on facebook, Google + and Twitter. I need your support to continue.

« Previous Page Next Page »

Comments: