Java Number & Math class

Source: Internet
Author: User
Tags cos sin square root

This kind of wrapper, specifically supported by the compiler, is called boxing, so when the built-in data type is used as an object, the compiler will box the built-in type into a wrapper class. Similarly, the compiler can also disassemble an object into a built-in type. The number class belongs to the Java.lang package.
Here is an instance that uses an Integer object:
Test.java File Code:
public class test{

public static void Main (String args[]) {
Integer x = 5;
x = x + 10;
SYSTEM.OUT.BJRONGJINHUIYIN.COM.PRINTLN (x);
}
}
The results of the above example compilation run as follows:
15
When x is assigned an integer value, because X is an object, the compiler is boxing the X. Then, to allow X to be added, the X is disassembled.
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.
The math method is defined as the static form, which can be called directly from the main function through the math class.
Test.java File Code:
public class Test {
public static void Main (String []args)
{
System.out.println ("Sine of 90 degrees:" + Math.sin (MATH.PI/2));
System.out.println ("Cosine of 0 Degrees:" + math.cos (0));
System.out.println ("Tangent of 60 degrees:" + Math.tan (MATH.PI/3));
System.out.println ("1 of the inverse Tangent value:" + Math.atan (1));
System.out.println ("Π/2 angle value:" + math.todegrees (MATH.PI/2));
System.out.println (Math.PI);
}
}
The results of the above example compilation run as follows:
Sinusoidal value of 90 degrees: 1.0
Cosine of 0 Degrees: 1.0
Tangent of 60 degrees: 1.7320508075688767
1 of the inverse tangent value: 0.7853981633974483
Angle value of Π/2:90.0
3.141592653589793
Number & Math class methods
Some of the methods commonly used in the number & Math classes are listed in the following table:
Ordinal method and Description
1 Xxxvalue ()
Converts the number object to the value of the XXX data type and returns.
2 CompareTo ()
Compares a number object to a parameter.
3 Equals ()
Determines whether the number object is equal to the parameter.
4 ValueOf ()
Returns the built-in data type specified by a number object
5 toString ()
Returns a value as a string.
6 parseint ()
Resolves a string to an int type.
7 ABS ()
Returns the absolute value of the parameter.
8 Ceil ()
Returns the smallest integer greater than or equal to (>=) the given argument.
9 Floor ()
Returns the largest integer less than or equal to (<=) the given parameter.
Ten rint ()
Returns the integer closest to the parameter. The return type is double.
Round ()
It represents rounding, the algorithm is Math.floor (x+0.5), the original number is added 0.5 and then rounded down, so the result of Math.Round (11.5) is 12,math.round (-11.5) of the result is-11.
Min ()
Returns the minimum value from two parameters.
Max ()
Returns the maximum value in two parameters.
+ exp ()
Returns the parameter of the base e of the natural number.
Log ()
Returns the value of the base of the natural number of the parameter.
POW ()
Returns the second parameter of the first argument to the other side.
sqrt ()
The arithmetic square root of the argument.
Sin ()
Specifies the sine of a double type parameter.
cos ()
Specifies the cosine value of the double type parameter.
Tan ()
Specifies the tangent of a double type parameter.
ASIN ()
Specifies the inverse chord value of the double type parameter.
ACOs ()
Specifies the inverse cosine value of the double type parameter.
Atan ()
Specifies the inverse tangent value of the double type parameter.
ATAN2 ()
Converts Cartesian coordinates to polar coordinates and returns the angular value of the polar coordinates.
Todegrees ()
Converts a parameter to an angle.
Toradians ()
Converts the angle to radians.
Random ()
Returns a random number.
Comparison of Floor,round and Ceil method instances of Math
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
Examples of Floor,round and 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));
}
}
The output of the above instance execution is:
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
Java Branch Structure –if...else/switch Java Character class br/> note list
Easier x
502***[email protected]
< p="">

  • @author Dale.
  • Automatic boxing and unpacking in Java
  • To put it simply, boxing is melting gold. Automatically converts the base data type to the wrapper type; unpacking is the automatic conversion of the wrapper type to the base data type.
    */
    public class Number {
    public static void Main (string[] args) {
    /*
    Integer i1 = 128; Packing, equivalent to integer.valueof (128);
    int t = I1; Equivalent to I1.intvalue () unpacking
    System.out.println (t);
    /

    /**对于–128到127(默认是127)之间的值,被装箱后,会被放在内存里进行重用但是如果超出了这个值,系统会重新new 一个对象*/Integer i1 = 200;Integer i2 = 200;/**注意 == 与 equals的区别== 它比较的是对象的地址equlas 比较的是对象的内容*/if(i1==i2) {    System.out.println("true");} else {    System.out.println("false");}

    }br/>}
    Easier x
    Easier x
    502***[email protected]
    Miao
    zha***[email protected]

    Integer a = 10;
    Integer B = 10;
    System.out.println (A = = B); True
    System.out.println (A.equals (b)); True
    Integer a = 1000;
    Integer B = 1000;
    System.out.println (A = = B); False
    System.out.println (A.equals (b)); True

Java Number & Math class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.