You are on page 1of 4

Normally, when we work with Numbers, we use primitive data types such as byte, int, long, double, etc.

Example:
int i = 5000; float gpa = 13.65; byte mask = 0xaf;

However, in development, we come across situations where we need to use objects instead of primitive data types. n!order to achieve this "ava provides wrapper classes for each primitive data type. #ll the wrapper classes $ nteger, %ong, &yte, 'ouble, (loat, )hort* are subclasses of the abstract class Number.

+his wrapping is taken care of by the compiler, the process is called boxing. )o when a primitive is used when an object is re,uired, the compiler boxes the primitive type in its wrapper class. )imilarly, the compiler unboxes the object to a primitive as well. +he Number is part of the java.lang package. Here is an example of boxing and unboxing:
public class Test{ public static void main !t"ing a"gs#$%{ &ntege" x = 5; '' boxes int to an &ntege" ob(ect x = x ) 10; '' unboxes t*e &ntege" to a int !ystem.out.p"intln x%; + +

+his would produce the following result:


15

-hen x is assigned integer values, the compiler boxes the integer because x is integer objects. %ater, x is unboxed so that they can be added as integers.

Number .ethods:
Here is the list of the instance methods that all the subclasses of the Number class implement: SN Methods with Description / 2 3 4 7 8 9 : xxx0alue$* 1onverts the value of this Number object to the xxx data type and returned it. compare+o$* 1ompares this Number object to the argument. e,uals$* 'etermines whether this number object is e,ual to the argument. value5f$* 6eturns an nteger object holding the value of the specified primitive. to)tring$* 6eturns a )tring object representing the value of specified int or nteger. parse nt$* +his method is used to get the primitive data type of a certain )tring. abs$* 6eturns the absolute value of the argument. ceil$* 6eturns the smallest integer that is greater than or e,ual to the argument. 6eturned as a double. floor$* 6eturns the largest integer that is less than or e,ual to the argument. 6eturned as a double. rint$* 6eturns the integer that is closest in value to the argument. 6eturned as a double.

; /<

round$* // 6eturns the closest long or int, as indicated by the method=s return type, to the argument. /2 /3 /4 min$* 6eturns the smaller of the two arguments. max$* 6eturns the larger of the two arguments. exp$* 6eturns the base of the natural logarithms, e, to the power of the argument.

/7 log$*

6eturns the natural logarithm of the argument. pow$* /8 6eturns the value of the first argument raised to the power of the second argument. /9 /: /; 2< 2/ 22 23 s,rt$* 6eturns the s,uare root of the argument. sin$* 6eturns the sine of the specified double value. cos$* 6eturns the cosine of the specified double value. tan$* 6eturns the tangent of the specified double value. asin$* 6eturns the arcsine of the specified double value. acos$* 6eturns the arccosine of the specified double value. atan$* 6eturns the arctangent of the specified double value.

atan2$* 24 1onverts rectangular coordinates $x, y* to polar coordinate $r, theta* and returns theta. 27 28 29 to'egrees$* 1onverts the argument to degrees to6adians$* 1onverts the argument to radians. random$* 6eturns a random number.

-hat is Next>
n the next section, we will be going through the 1haracter class in "ava. ?ou will be learning how to use object 1haracters and primitive data type char in "ava.
@revious @age @rint 0ersion @'( 0ersion Next @age

#dvertisements
Share on facebookShare on twitter