Java Number & Math class
development process, we will meet applicable object rather than the case of built-in data types. To solve this problem, Java language provides a corresponding wrapper class for each built-in data types.
Integer, Long, Byte, Double, Float, Short abstract classes are subclasses of Number.
- Java Math
- Number & Math methods
Special support by the compiler package to be packing, so when the built-in data types are treated as objects used, the compiler will it as a built-in type packing wrapper class, the compiler can also put a target for the unpacking built-in objects. Number java.lang class data packet.
Examples
public class Test{
public static void main(String args[]){
Integer x = 5;
x = x + 10;
System.out.println(x);
}
}
Java Math
It contains the properties and methods used to perform basic mathematical operations, like ever exponential, logarithmic, trigonometric functions and square root.
Math methods are defined as static in the form of the function can be called directly by the main math class.
public class Test {
public static void main (String []args)
{
System.out.println("90 度的正弦值:" + Math.sin(Math.PI/2));
System.out.println("0度的余弦值:" + Math.cos(0));
System.out.println("60度的正切值:" + Math.tan(Math.PI/3));
System.out.println("1的反正切值: " + Math.atan(1));
System.out.println("π/2的角度值:" + Math.toDegrees(Math.PI/2));
System.out.println(Math.PI);
}
}
Number & Math methods
Numbering | Methods and description |
---|---|
1 | xxxValue () converts the value XXX Number object data type and returns |
2 | the compareTo () will be compared with parameters of the object number |
3 | equals () is determined number |
4 | valueOf () Returns a specified Number object built-in data types |
5 | toString () Returns a string value |
6 | the parseInt () parse a string of type int |
7 | ABS () returns the absolute value of the parameter |
8 | ceil () Returns greater than or equal to (> =) to a given parameter smallest integer, double precision floating-type |
9 | floor () returns is less than or equal to (<=) the largest integer given parameter |
10 | RINT () returns the nearest integer parameters. Return type is double |
11 | round () rounding, Math.floor (x + 0.5), plus the original digital round down 0.5 |
12 | min () returns the minimum of the two parameters |
13 | max () Returns the maximum value of the two parameters |
14 | exp () Returns the number of parameters of power base e of natural |
15 | log () to return the value of the parameters of the natural logarithm base |
16 | POW () returns the first parameter the second parameter th |
17 | sqrt () request parameter square root |
18 | sin () Sine the parameter value of the specified type double |
19 | cos () request type parameter specifies the cosine double |
20 | Tan () request tangent type parameter specifies double |
21 | asin () request to specify the parameters of the type double arcsine |
22 | ACOS () request type parameter specifies double arccosine |
23 | ATN () request type parameter specifies double arctangent |
24 | atn2 () to Cartesian coordinates to polar coordinates, polar coordinates and returns the value of the angle |
25 | toDergrees () converts the parameter to an angle |
26 | toRadians () will be converted to an angle in radians |
27 | random() 返回一个随机数 |
示例:floor,round和ceil
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));
}
}