Number Class (Cont.)


The following is the list of the instance methods that all the subclasses of the Number class implement:

Method Description
xxxValue( )

Converts the value of this Number object to the xxx data type and returns it.

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 a 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.

random( )

Returns a random number.


Differences between the method equals() and the operator == are vague and complicated. Simply put,
MyNumber.java (java.lang.Number class)

 public class MyNumber {
   public static void main( String[ ] args ) {
     int     a = 5;
     Integer b = 5;
     Short   c = 5;
     Double  d = 5.0;

     System.out.println( b.equals( 5 ) );
       //  true   false   error
     System.out.println( b.equals( a ) );
       //  true   false   error
     System.out.println( b.equals( c ) );
       //  true   false   error
     System.out.println( b.equals( d ) );
       //  true   false   error
     System.out.println( b == 5 );
       //  true   false   error
     System.out.println( b == a );
       //  true   false   error
     System.out.println( b == c );
       //  true   false   error
   }
 }
      Result: