Number Class in Java

Introduction

There is a special class known as the Number class. It is used when we want objects in place of data types.

Number Class in Java

In Java, we need to deal in objects. So we have the Number class in Java. The Number class contains all the wrapper classes in Java. So when we want objects in place of data types in Java, we use the Number class. The java.lang package contains the Number class in Java.

number class

Methods of Number Class

The Number class in Java has many methods. Some of them are explained below.

  • compareTo( )

    The Number class object is compared with the argument by this method. We can compare Byte, Integer and so on. The Number and argument should be of the same data type for comparison.

    Syntax
     

    publicint compareTo( NumberSubClass referenceName )

    It will return the value zero if the argument is equal to the Integer.

    It will return the value minus one if the argument is greater than the Integer.

    It will return the value one if the argument is smaller than the Integer.

    Example
     

    package demo;

    publicclass Demo

    {

       public staticvoid main(String args[])

        {

            Integer a = 7;

            System.out.println(a.compareTo(2));

            System.out.println(a.compareTo(7));

            System.out.println(a.compareTo(9));           

        }

    }


    Output

    compareTo( )

     

  • equals( )

    It is checked by this method that the object of the Number class and the argument are equal.

    Syntax

    public
    boolean equals(Object o)

    It will return the value true if they are equal.

    It will return the value false if they are unequal.

    Example
     

    package demo;

    publicclass Demo

    {

       public staticvoid main(String args[])

        {

            Integer a = 6;

            Integer b = 9;

            Integer c =6;

            Short x = 7;

            System.out.println(a.equals(b)); 

            System.out.println(a.equals(c));

            System.out.println(a.equals(x));

        }

    }

    Output

    equals( )

     

  • abs( )

    The Absolute value is returned.

    Syntax
     

    double abs(double d)

    float abs(float f)

    int abs(int i)

    long abs(long lng)

    Example

     

    package demo;

    publicclass Demo

    {

       public staticvoid 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));   

       }

    }


    Output

    abs( )

     

  • ceil( )

    It will provide an Integer as output. It will be equal to or greater than the argument.

    Syntax
     

    double ceil(double d)

    double ceil(float f)

    Example
     

    package demo;

    publicclass Demo

    {

       public staticvoid main(String args[])

        {

           double x = -10.777;

            System.out.println(Math.ceil(x));

        }

    }


    Output

    ceil( )

     

  • floor( )

    It will also gives an Integer as the output. It will be equal to or smaller than the argument.

    Syntax
     

    double floor(double d)

    double floor(float f)

    Example

     

    package demo;

    publicclass Demo

    {

       public staticvoid main(String args[])

        {

           double x = -10.777;

            System.out.println(Math.floor(x));

        }

    }


    Output

    floor( )

     

  • rint( )

    It will provide an Integer that is nearby to the argument.

    Syntax
     

    double rint(double d)

    Example

     

    package demo;

    publicclass Demo

    {

       public staticvoid main(String args[])

        {

           double x = 10.7;

           double y = 10.5;

           double z = 10.1;

            System.out.println(Math.rint(x));

            System.out.println(Math.rint(y));

            System.out.println(Math.rint(z));

        }

    }


    Output

    rint( )

     

  • random( )

    It will provide a random number. The range will be between 0.0 and 1.0.

    Syntax
     

    staticdouble random()

    Example

     

    package demo;

    publicclass Demo

    {

       public staticvoid main(String args[])

        {

            System.out.println( Math.random() );

            System.out.println( Math.random() );

            System.out.println( Math.random() );

        }

    }

    Output

    random( )


Summary

This article has explained the Number class in Java.

Up Next
    Ebook Download
    View all
    Learn
    View all