Numbers in Java

Numbers in Java


Posted in : Core Java Posted on : September 28, 2010 at 12:39 PM Comments : [ 0 ]

Numbers in Java

Numbers in Java

In java ,when we have need of a number, we use primitive data types such byte, int, long, double etc.

During coding of an application , situation occurs when we need to use object in place of primitive data types. To implement this Java have wrapper classes for each primitive data type.

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

The process of writing a primitive data type into an object is called wrapping an Object and it is handled  by compiler. The process is called boxing. Similarly the compiler unboxes the object to a primitive as well. The Number is part of the java.lang package.

Given below the example of boxing and unboxing :

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

Output :

C:\Program Files\Java\jdk1.6.0_18\bin>javac boxing.java

C:\Program Files\Java\jdk1.6.0_18\bin>java boxing
5

Given below table contains the methods for processing numbers :

    Methods             Description         
xxxValue() Converts the value of this Number object to the xxx data type and returned 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 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.
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.
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.
toDegrees() Converts the argument to degrees
toRadians() Converts the argument to radians.
random() Returns a random number.
Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics