Getting Started with Java - language basis - 12.Number and Math Class

Original Address: http://www.work100.net/training/java-number-math.html
More Tutorials: beam cloud - free course

Number and Math class

No. Article section video
1 Outline
2 Math class
3 Math class and method Number
4 Math the floor, round and methods ceil Comparative Example

Please refer above 章节导航to read

1 Overview

Generally, when the required numbers, we usually use the built-in data types, such as: byte, int, long, doubleand the like.

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

However, in the actual development process, we often encounter situations require the use of objects instead of the 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 ) is a subclass of the abstract class Number.

This is called by the compiler packing packaging special support, so when the built-in data types are used as an object, the compiler will built-in types of packaging containers. Similarly, the compiler is able to unpack an object is built-in type. Number class belongs to java.langthe package.

Below is an example using the object Integer:

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 xthe time is assigned an integer value, since xan object, the compiler of x to be boxed. Then, in order to xbe able to perform addition operation, so to xbe unpacking.

2.Math class

Java's Math contains properties and methods for performing basic mathematical operations, such as ever exponential, logarithmic, square root, and trigonometric functions.

Math methods are defined to staticform in the main function can be called directly by 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);  
    }  
}

The above examples compiled results are as follows:

90 度的正弦值:1.0
0度的余弦值:1.0
60度的正切值:1.7320508075688767
1的反正切值: 0.7853981633974483
π/2的角度值:90.0
3.141592653589793

Math class and method Number

Math class and method 3.Number

Number & Math class is commonly used methods are listed in the following table:

No. Method and Description
1 xxxValue() Number object converted to a value of the data type and returns xxx.
2 compareTo() The number compares with the target parameters.
3 equals() Determining whether the object is equal to the number of parameters.
4 valueOf()Number object returns a built-in data types specified
5 toString() Return value as a string.
6 parseInt() Parse the string to type int.
7 abs() Returns the absolute value of the parameters.
8 ceil()Back less than ( >=) given parameter smallest integer, double precision floating point type.
9 floor()Back than ( <=) to the largest integer given parameters.
10 rint()Parameters and returns an integer closest. Return type is double.
11 round()It represents a rounding algorithm Math.floor(x+0.5), i.e. after the original number plus 0.5 rounded down, so that Math.round(11.5)the result is 12, Math.round(-11.5)the result is -11.
12 min() Returns the minimum of the two parameters.
13 max() Returns the maximum value of the two parameters.
14 exp() Returns the natural log base e of power parameters.
15 log() Returns the values ​​of the natural logarithm base parameters.
16 pow() Returns the first parameter the second parameter of the power.
17 sqrt() Find the square root of the arithmetic parameters.
18 sin() Seeking a double sine specified parameters.
19 cos() Request type parameter specifies double cosine.
20 tan() Request type parameter specifies double tangent.
21 asin() Seeking specify parameters of type double arc sine value.
22 acos() Request type parameter specifies double arccosine.
23 atan() Request type parameter specifies double arctangent.
24 atan2() Converting Cartesian coordinates into polar coordinates, polar coordinates and returns the value of the angle.
25 toDegrees() Into an angle parameter.
26 toRadians() Convert degrees to radians.
27 random() Returns a random number.

The floor 4.Math, round and methods ceil Comparative Example

parameter Math.floor Math.round Math.ceil
1.4 1 1 2
1.5 1 2 2
1.6 1 2 2
-1.4 -2 -1 -1
-1.5 -2 -1 -1
-1.6 -2 -2 -1

floor, roundAnd ceilexamples:

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));   
  }   
}

Examples of the above output is performed:

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

Previous: switch_case

Next: Character Class

Guess you like

Origin www.cnblogs.com/liuxiaojun/p/training-java-number-math.html