Java Number & Math class

Source: Internet
Author: User
Tags square root

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.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 bjrongjinhuiyin.com 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 ()
Rounding up the shape variable, the return type is double.
9floor ()
The integer variable is rounded down. The return type is of type double.
10rint ()
Returns the integer closest to the parameter. The return type is double.
11round ()
Returns the nearest int, Long value.
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.
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, gold and silver boxing is the automatic conversion of the basic 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]
10 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

Related Article

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.