Java Number & Math class

Source: Internet
Author: User
Tags square root wrapper

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
1xxxValue ()
Converts the number object to the value of the XXX data type and returns.
2compareTo ()
Compares a number object to a parameter.
3equals ()
Determines whether the number object is equal to the parameter.
4valueOf ()
Returns the built-in data type specified by a number object
5toString ()
Returns a value as a string.
6parseInt ()
Resolves a string to an int type.
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 parameter.
10rint ()
Returns the integer closest to the parameter. The return type is double.
11round ()
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.
12min ()
Returns the minimum value from two parameters.
13max ()
Returns the maximum value in two parameters.
14exp ()
Returns the parameter of the base e of the natural number.
15log ()
Returns the value of the base of the natural number of the parameter.
16pow ()
Returns the second parameter of the first argument to the other side.
17SQRT ()
The arithmetic square root of the argument.
18sin ()
Specifies the sine of a double type parameter.
19cos ()
Specifies the cosine value of the double type parameter.
20tan ()
Specifies the tangent of a double type parameter.
21asin ()
Specifies the inverse chord value of the double type parameter.
22acos ()
Specifies the inverse cosine value of the double type parameter.
23atan ()
Specifies the inverse tangent value of the double type parameter.
24atan2 ()
Converts Cartesian coordinates to polar coordinates and returns the angular value of the polar coordinates.
25toDegrees ()
Converts a parameter to an angle.
26toRadians ()
Converts the angle to radians.
27random ()
Returns a random number.
Comparison of Floor,round and Ceil method instances of Math
Parameter Math.floorMath.roundMath.ceil
1.4112
1.5122
1.6122
-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
List of Notes
Easier x
502***[email protected]
/**
* @author Dale
* Automatic packing and unpacking in Java
* To put it simply, boxing is the melting gold bank 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);
*/

/**
For values between –128 to 127 (by default, 127), they are boxed and placed in memory for reuse.
But if this value is exceeded, the system will re-new an object
*/
Integer i1 = 200;
Integer i2 = 200;

/**
Notice the difference between = = and equals
= = It compares the address of the object
Equlas compares the contents of an object
*/
if (I1==I2) {
System.out.println ("true");
} else {
System.out.println ("false");
}
}
}
Easier x
Easier x
502***[email protected]
11 months ago (03-23)
Miao
Zha***[email protected]
Note that Java caches -127~127 integers and notes the difference between = = and equals.
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.