Java Numbers
We use primitive data types like byte, int, long, double, etc to work with the numbers, When we need objects, we use wrapper class like Integer, Double, Long, Byte, etc.
All the wrapper classes (Integer, Long, Byte, Double, Float, Short) are subclasses of the abstract class Number.
The wrapper class has different wraps which convert the respective primitive data types to its corresponding object types. It is done by the compiler.
We pass the primitive type and compiler convert it into there object type, this is called boxing. The conversion of the wrapper class object to the primitive is called unboxing.
public class Numbers { public static void main(String args[]) { Integer x =10; // boxes int to an Integer object x = x + 10; // unboxes the Integer to a int System.out.println(x); } }
Output:
20
Methods of Number class:
xxxValue():- It Converts the value of this Number object to the xxx data type and returns it.
CompareTo():- It Compares this Number object to the argument.
Equals():- It Determines whether this number object is equal to the argument.
ValueOf():- It Returns an Integer object holding the value of the specified primitive.
ToString():- It 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():- It Returns the absolute value of the argument.
Ceil():- It Returns the smallest integer that is greater than or equal to the argument. Returned as a double.
Floor():- It Returns the largest integer that is less than or equal to the argument. Returned as a double.
Rint():- It Returns the integer that is closest in value to the argument. Returned as a double.
Round():- It Returns the closest long or int, as indicated by the method's return type to the argument.
Min():- It Returns the smaller of the two arguments.
Max():- It Returns the larger of the two arguments.
Exp():- It Returns the base of the natural logarithms, e, to the power of the argument.
Log():- It Returns the natural logarithm of the argument.
Pow():- It Returns the value of the first argument raised to the power of the second argument.
Sqrt():- It Returns the square root of the argument.
Sin():- It Returns the sine of the specified double value.
Cos():- It Returns the cosine of the specified double value.
Tan():- It Returns the tangent of the specified double value.
Asin():- It Returns the arcsine of the specified double value.
Acos():- It Returns the arccosine of the specified double value.
Atan():- It Returns the arctangent of the specified double value.
Atan2():- It Converts rectangular coordinates (x, y) to polar coordinate (r, theta) and returns theta.
ToDegrees():- It Converts the argument to degrees.
ToRadians():- It Converts the argument to radians.
Random():- It Returns a random number.