Java Number & Math class

Source: Internet
Author: User
Tags cos sin square root wrapper

Java Number & Math class

Generally, when you need to use numbers, we usually use built-in data types, such as:byte, int, long, double , and so on.

Instance
int float byte c = 0x4a;

However, in the actual development process, we often encounter situations where objects need to be used rather than built-in data types. To solve this problem, the Java language provides the corresponding wrapper class for each built-in data type.

All wrapper classes (Integer, Long, Byte, Double, Float, short) are subclasses of the abstract class number.

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{    publicstaticvoid  main (String args[]) {      = 5;       =  x + ten;      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 {      publicstaticvoid  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:

Sine of 90 degrees: the cosine of the 1.00 degrees:The tangent of 1.0 60 degrees:1.73205080756887670.7853981633974483π/ Angle value of 2:90.03.141592653589793

Number & Math class methods

Some of the methods commonly used in the number & Math classes are listed in the following table:

Serial Number 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 ()
Rounding up the shape variable, the return type is double.
9 Floor ()
The integer variable is rounded down. The return type is of type double.
10 Rint ()
Returns the integer closest to the parameter. The return type is double.
11 Round ()
Returns the nearest int, Long value.
12 Min ()
Returns the minimum value from two parameters.
13 Max ()
Returns the maximum value in two parameters.
14 EXP ()
Returns the parameter of the base e of the natural number.
15 Log ()
Returns the value of the base of the natural number of the parameter.
16 POW ()
Returns the second parameter of the first argument to the other side.
17 sqrt ()
The arithmetic square root of the argument.
18 Sin ()
Specifies the sine of a double type parameter.
19 cos ()
Specifies the cosine value of the double type parameter.
20 Tan ()
Specifies the tangent of a double type parameter.
21st ASIN ()
Specifies the inverse chord value of the double type parameter.
22 ACOs ()
Specifies the inverse cosine value of the double type parameter.
23 Atan ()
Specifies the inverse tangent value of the double type parameter.
24 ATAN2 ()
Converts Cartesian coordinates to polar coordinates and returns the angular value of the polar coordinates.
25 Todegrees ()
Converts a parameter to an angle.
26 Toradians ()
Converts the angle to radians.
27 Random ()
Returns a random number.
List of Notes
  1. /*** @author dale* Automatic boxing and unpacking in Java * Simply put, 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 classNumber { Public Static voidMain (string[] args) {/**Integer i1 = 128;        Packing, equivalent to integer.valueof (128); int t = I1;        Equivalent to I1.intvalue () unpacking System.out.println (t); */        /**for a value between –128 to 127 (the default is 127), it is boxed and then reused in memory, but if this value is exceeded, the system will re-new an object*/Integer I1= 200; Integer I2= 200; /**Note = = differs from equals = = It compares the object's address Equlas compared to the object's contents*/        if(i1==I2) {System.out.println ("True"); } Else{System.out.println ("False"); }    }}
  2. Note that Java caches -127~127 integers and notes the difference between = = and equals.

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.