Java Number – Number Methods with Syntax and Examples

1. Objective

We closed our last chapter on Decision Making in Java. Now, we are going to talk about Java Number. Moreover, we will learn several types of Java Number methods with their syntax and examples.

So, let’s start the Java Number Methods tutorial.

Java Number Methods with Syntax and Examples

Java Number Methods with Syntax and Examples

2. What is Java 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.

Do you know What is Autoboxing and Unboxing in Java?

Example of Java Number

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);
   }
}

3. Number Methods in Java

There are various types of Java number methods, let’s discuss them one by one:

Java Number - Number Methods with Syntax and Examples

Java Number Tutorial – Java Number Methods

a. Java xxx xxxValue()

It represents the primitive datatypes byte, short, int, long, float, double. This datatype here is used to convert the Java number types into the mentioned types.
Syntax

byte byteValue()
short shortValue()
int intValue()
long longValue()
float floatValue()
double doubleValue()

Example of Java xxx xxxValue

//Java program to demonstrate xxxValue() method
public class Test
{
    public static void main(String[] args)
    {
        // Creating a Double Class object with value "6.9685"
        Double d = new Double("6.9685");   
        // 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 : 6
value of d after converting it to short : 6
value of d after converting it to int : 6
value of d after converting it to long : 6
value of d after converting it to float : 6.9685
value of d after converting it to double : 6.9685

Let’s discuss about Abstrct Class in Java

b. Java int compareTo(NumberSubClass referenceName)

This method is used to compare the specified argument and the number object, but two different types cannot be compared, so both the argument and the number should be of the same type.

The Reference could of the type byte, double, float, long, or short.

Syntax

public int compareTo( NumberSubClass referenceName )

Example of Java int compareTo

//Java program to demonstrate compareTo() method
public class Test
{
    public static void main(String[] args)
    {
        // creating an Integer Class object with value "10"
        Integer i = new Integer("10");  
        // comparing value of i
        System.out.println(i.compareTo(8));
        System.out.println(i.compareTo(10));
        System.out.println(i.compareTo(11));
    }
}

c. Java boolean equals(Object obj)

This Java Number method is used to determine whether the number object is equal to the argument.
Syntax 

public boolean equals(Object obj)

Example of Java Boolean Equal

//Java program to demonstrate equals() method
public class Test
{
    public static void main(String[] args)
    {
        // creating a Short Class object with value "15"
        Short s = new Short("15");
        // creating a Short Class object with value "10"
        Short x = 10;
        // creating an Integer Class object with value "15"
        Integer y = 15;
        // creating another Short Class object with value "15"
        Short z = 15;
        //comparing s with other objects
        System.out.println(s.equals(x));
        System.out.println(s.equals(y));
        System.out.println(s.equals(z));
    }
}

d. Java int parseInt(String s,int radix)

The radix in this Java Number method use to return decimal, octal, or hexadecimal representation etc. type as output, this method use for getting the primitive datatypes.
Syntax 

static int parseInt(String s, int radix)

Example of Java int parseInt(String s, int radix)

//Java program to demonstrate Integer.parseInt() method
public class Test
{
    public static void main(String[] args)
    {
      // parsing different strings
        int z = Integer.parseInt("654",8);
        int a = Integer.parseInt("-FF", 16);
        long l = Long.parseLong("2158611234",10);
        System.out.println(z);
        System.out.println(a);
        System.out.println(l);
      // run-time NumberFormatException will occur here "Geeks" is not a parsable string
        int x = Integer.parseInt("Geeks",8);
     // run-time NumberFormatException will occur here(for octal(8),allowed digits are [0-7])
        int y = Integer.parseInt("99",8);
    }
}

Follow this link to know about Java Method

e. Java int parseInt(String s)

This method use to get the primitive datatypes, the only difference being the radix uses decimal as default.
Syntax

static int parseInt(String s)

Example of Java int parseInt (String s)

//Java program to demonstrate Integer.parseInt() method
public class Test
{
    public static void main(String[] args)
    {
        // parsing different strings
        int z = Integer.parseInt("655");
        long l = Long.parseLong("2123211234");          
        System.out.println(z);
        System.out.println(l);           
        // run-time NumberFormatException will occur here "shadow" is not a parsable string
        int x = Integer.parseInt("shadow");           
        // run-time NumberFormatException will occur here (for decimal(10),allowed digits are [0-9])
        int a = Integer.parseInt("-FF");         
    }
}

f. Java String toString()

This method is used to get the Java String representation of any number, there are three variants in this method-

  • toBinaryString(int i)
  • toHexString(int i)
  • toOctalString(int i)

Syntax

String toString()
String toString(int i)

Example of Java String toString

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

g. java Integer valueOf()

This method returns the integer value of a primitive datatype.
Syntax

Integer valueOf(int i)
Integer valueOf(String s)
Integer valueOf(String s, int radix)

Example of Integer valueOf

// Java program to demonstrate valueOf() method
public class Test
{
    public static void main(String[] args)
    {
        // demonstrating valueOf(int i) method
        System.out.println("Demonstrating valueOf(int i) method");
        Integer i =Integer.valueOf(50);
        Double d = Double.valueOf(9.36);
        System.out.println(i);
        System.out.println(d);       
        // demonstrating valueOf(String s) method
        System.out.println("Demonstrating valueOf(String s) method");
        Integer n = Integer.valueOf("333");
        Integer m = Integer.valueOf("-255");
        System.out.println(n);
        System.out.println(m);
        // demonstrating valueOf(String s,int radix) method
        System.out.println("Demonstrating (String s,int radix) method");
        Integer y = Integer.valueOf("333",8);
        Integer x = Integer.valueOf("-255",16);
        Long l = Long.valueOf("51688245",16);
        System.out.println(y);
        System.out.println(x);
        System.out.println(l);
        // run-time NumberFormatException will occur in below cases
        Integer a = Integer.valueOf("shadow");
        Integer b = Integer.valueOf("shadow",16);
    }
}

So, this was all about Java Number Methods Tutorial. Hope you like our explanation.

4. Conclusion

Hence, in this tutorial for Java Number, we learned about the different types of numbers available for Java in its library, examples of Java number and various methods to declare with syntax and examples. This will help us understand the more complex programs better and code better. Still, have a query, feel free to share with us!

Why should you learn Java Language

For reference

Leave a Reply

Your email address will not be published. Required fields are marked *