A Brief Introduction to the Number and Math classes in Java, numbermath
Java Number
Generally, when a number is required, we usually use a built-in data type, such:Byte, int, long, double.
Instance:
1 int a = 5000;2 float b = 13.65f;3 byte c = 0x4a;
However, in actual development, we often encounter the need to use objects instead of built-in data types. To solve this problem, the Java language provides corresponding packaging classes for each built-in data type.
All packages(Integer, Long, Byte, Double, Float, Short)Are subclasses of the abstract class Number.
This kind of packaging is especially supported by the compiler. Therefore, when the built-in data type is used as an object, the compiler will pack the built-in data type as a packaging class. Similarly, the compiler can split an object into a built-in type. The Number class belongs to the java. lang package.
The following is an example of using an Integer object:
1 public class Test{2 3 public static void main(String args[]){4 Integer x = 5;5 x = x + 10;6 System.out.println(x); 7 }8 }
The above example compilation and running results are as follows:
15
When x is assigned an integer value, since x is an object, the compiler needs to pack x. Then, in order to enable the addition operation of x, it is necessary to split the box of x.
Java Math class
Java Math contains attributes and methods used to perform basic mathematical operations, such as Elementary Indexes, logarithm, square root, and trigonometric functions.
All Math methods are defined as static. The Math class can be called directly in the main function.
1 public class Test {2 public static void main (String [] args) 3 {4 System. out. println ("90 degree sine value:" + Math. sin (Math. PI/2); 5 System. out. println ("cosine of 0 degree:" + Math. cos (0); 6 System. out. println ("60 degree tangent:" + Math. tan (Math. PI/3); 7 System. out. println ("1's arc tangent value:" + Math. atan (1); 8 System. out. println ("π/2 angle value:" + Math. toDegrees (Math. PI/2); 9 System. out. println (Math. PI); 10} 11}
The above example compilation and running results are as follows:
Sine of 90 degrees: cosine of 1.00 degrees: Tangent of 1.060 degrees: arc tangent of 1.73205080756887671: angle of 0.7853981633974483 π/2: 90.03.141592653589793
Java online API: http://tool.oschina.net/apidocs/apidoc? Api = jdk_7u4
Alimail: AlanLee
Blog: http://www.cnblogs.com/AlanLee
This article is from the blog site. You are welcome to join the blog site.