Java Number & Math Class

In general, when we need to use numbers, we usually use built-in data types such as: byte, int, long, double, and so on.

Examples

int a = 5000 ; float b = 13 .65 f ; byte c = 0x4a ;
However, in the actual development process, we often encounter the need to use the object, rather than the built-in data types. To solve this problem, the Java language provides a corresponding wrapper class for each built-in data type.
All wrapper classes (Integer, Long, Byte, Double, Float, Short) are subclasses of the abstract class Number.

This kind of wrapper that is specifically supported by the compiler is called boxing, so when the built-in data type is used as an object, the compiler wraps the built-in type as a wrapper class. Similarly, the compiler can also unbox an object as a built-in type. The Number class belongs to the java.lang package.
Here is an example using an Integer object:

Test.java file code:

public class Test{ public static void main(String args[]){ Integer x = 5; x = x + 10; System.out.println(x); } }
The above example compiled and run results are as follows:
15
When x is assigned an integer value, the compiler wraps x because x is an object. Then, in order to add x, it is necessary to unbox x.

Java Math class

Java's Math contains properties and methods for performing basic mathematical operations, such as elementary exponents, logarithms, square roots, and trigonometric functions.
Math's methods are all defined as static and can be called directly in the main function via the Math class.

Test.java file code:

public class Test {
public static void main ( String [ ] args ) {
System.out.println(" The sine of 90 degrees: " + Math.sin(Math.PI / 2 ) ) ; System.out.println(" 0 The cosine of the degree: " + Math.cos ( 0 )) ; System.out.println(" 60 degrees tangent: " + the Math.Tan(Math.PI / . 3 ) ) ; System.out.println(" arctangent 1: " + Math.Atan( 1 )) ; System.out.println(" π/2's value: " + Math.ToDegrees(Math.PI / 2 ) ) ; System.out.println(Math.PI ) ; }
}
The above example compiled and run results are as follows:
The sine of 90 degrees: 1.0 
The cosine of 0 degrees: 1.0 
The tangent of 60 degrees: 1.7320508075688767 
The arctangent of 1 : 0.7853981633974483 
The angle value of π/ 2 : 90.0 3.141592653589793

Number & Math class methods

The following table lists some of the common methods of the Number & Math class:
No.Method and description
1xxxValue()
converts a Number object to the value of the xxx data type and returns.
2compareTo()
Compares the number object with the argument.
3Equals()
Determines whether the number object is equal to the argument.
4valueOf()
returns the built-in data type specified by a Number object
5toString()
Returns the value as a string.
6parseInt()
parses the string as an int.
7Abs()
returns the absolute value of the parameter.
8Ceil()
returns the smallest integer greater than or equal to (>=) the given argument.
9Floor()
returns the largest integer less than or equal to (<=) the given argument.
10Rint()
returns the nearest integer to the argument. The return type is double.
11Round()
indicates rounding . The algorithm is Math.floor(x+0.5) , which adds 0.5 to the original number and thenrounds it down. Therefore, the result of Math.round(11.5) is 12, Math.round(- The result of 11.5) is -11.
12Min()
returns the minimum of two arguments.
13Max()
returns the maximum of two arguments.
14Exp()
returns the argument power of the natural number base e.
15Log()
returns the logarithm of the natural number base of the argument.
16Pow()
returns the second argument of the first argument.
17Sqrt()
finds the arithmetic square root of the argument.
18Sin()
finds the sine of the specified double type parameter.
19Cos()
finds the cosine of the specified double type parameter.
20Tan()
finds the tangent of the specified double type parameter.
twenty oneAsin()
finds the arcsine of the specified double argument.
twenty twoAcos()
finds the inverse cosine of the specified double type parameter.
twenty threeAtan()
finds the arc tangent of the specified double type parameter.
twenty fourAtan2()
converts Cartesian coordinates to polar coordinates and returns the polar coordinate values.
25toDegrees()
converts arguments to angles.
26toRadians()
Converts the angle to radians.
27Random()
returns a random number.

Comparison of Math's example of floor,round and ceil methods

parameterMath.floorMath.roundMath.ceil
1.4112
1.5122
1.6122
-1.4-2-1-1
-1.5-2-1-1
-1.6-2-2-1

Floor,round and ceil examples:

public class Main { public static void main(String[] args) { double[] nums = { 1.4, 1.5, 1.6, -1.4, -1.5, -1.6 }; for (double num : nums) { test(num); } } private static void test(double num) { System.out.println("Math.floor(" + num + ")=" + Math.floor(num)); System.out.println("Math.round(" + num + ")=" + Math.round(num)); System.out.println("Math.ceil(" + num + ")=" + Math.ceil(num)); }
}
The above example executes the output as:
Math.floor(1.4)=1.0
Math.round(1.4)=1
Math.ceil(1.4)=2.0
Math.floor(1.5)=1.0
Math.round(1.5)=2
Math.ceil(1.5)=2.0
Math.floor(1.6)=1.0
Math.round(1.6)=2
Math.ceil(1.6)=2.0
Math.floor(-1.4)=-2.0
Math.round(-1.4)=-1
Math.ceil(-1.4)=-1.0
Math.floor(-1.5)=-2.0
Math.round(-1.5)=-1
Math.ceil(-1.5)=-1.0
Math.floor(-1.6)=-2.0
Math.round(-1.6)=-2
Math.ceil(-1.6)=-1.0

Comments