The best Java Number classes Tutorial In 2024, In this tutorial you can learn Number Method,

Java Number classes

In general, when you need to use the numbers, we usually use the built-in data types, such as: byte, int, long, double, etc.

Examples

int a = 5000;
float b = 13.65;
byte c = 0x4a;

However, in the actual development process, we often encounter the situation requires the use of an object, rather than built-in data types. To solve this problem, Java language provides a corresponding wrapper class for each built-in data types.

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

Java Number classes

This is particularly supported by the compiler package called packing, so when the built-in data type is used as an object, the compiler will built-in type of packing and packaging. Similarly, the compiler can also put a target for the unpacking built-in types. Number class belongs to java.lang package.

Here is an instance of Integer object using:

public class Test{

   public static void main(String args[]){
      Integer x = 5;
      x =  x + 10;
      System.out.println(x); 
   }
}

The above examples compiled results are as follows:

15

When x is assigned to an integer value, since x is an object, so the compiler to be packing for x. Then, to make x-add operations can be performed, so to be on the x unpacking.


Number Method

The following table lists the methods Number subclass implementation:

No. Method and Description
1 xxxValue ()
The object is converted to a value xxx number data type and return.
2 compareTo ()
Comparing number object parameters.
3 equals ()
Determine whether the object is equal to the number parameter.
4 valueOf ()
Returns a Number object with the specified built-in data types
5 toString ()
Returned as a string value.
6 parseInt ()
Parse a string to an int.
7 abs ()
Returns the absolute value of the parameter.
8 ceil ()
Rounding of integer variable left, return type is double type.
9 floor ()
Rounded to integer variables to the right. The return type is double type.
10 rint ()
Returns the integer closest to the argument. The return type is double.
11 round ()
Returns the closest int, long type value.
12 min ()
Returns the minimum of the two parameters.
13 max ()
It returns the maximum of two arguments.
14 exp ()
Returns the natural log base e to the power of the argument.
15 log ()
Returns the values ​​of the parameters of the natural log base.
16 pow ()
Returns the first argument to the power of the second argument.
17 sqrt ()
Arithmetically square root parameter.
18 sin ()
Raising the specified sine double type parameter.
19 cos ()
Request parameter to specify the type of double cosine.
20 tan ()
Request parameter to specify the type of double tangent.
twenty one asin ()
Request parameter to specify the type of double arcsine.
twenty two acos ()
Specify the type of the parameter seeking double inverse cosine.
twenty three atan ()
Seeking specify double type parameters arctangent.
twenty four atan2 ()
Cartesian coordinates to polar coordinates and returns the value of the polar coordinates.
25 toDegrees ()
The argument is converted to an angle.
26 toRadians ()
Convert degrees to radians.
27 random ()
Returns a random number.
Java Number classes
10/30