A Brief Introduction to the Number and Math classes in Java, numbermath

Source: Internet
Author: User

A Brief Introduction to the Number and Math classes in Java, numbermath

Java Number

Generally, when a number is required, we usually use a built-in data type, such:Byte, int, long, double.

Instance:

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

However, in actual development, we often encounter the need to use objects instead of built-in data types. To solve this problem, the Java language provides corresponding packaging classes for each built-in data type.

All packages(Integer, Long, Byte, Double, Float, Short)Are subclasses of the abstract class Number.

This kind of packaging is especially supported by the compiler. Therefore, when the built-in data type is used as an object, the compiler will pack the built-in data type as a packaging class. Similarly, the compiler can split an object into a built-in type. The Number class belongs to the java. lang package.

The following is an example of using an Integer object:

1 public class Test{2  3    public static void main(String args[]){4       Integer x = 5;5       x =  x + 10;6       System.out.println(x); 7    }8 }

The above example compilation and running results are as follows:

15

When x is assigned an integer value, since x is an object, the compiler needs to pack x. Then, in order to enable the addition operation of x, it is necessary to split the box of x.

 

Java Math class

Java Math contains attributes and methods used to perform basic mathematical operations, such as Elementary Indexes, logarithm, square root, and trigonometric functions.

All Math methods are defined as static. The Math class can be called directly in the main function.

1 public class Test {2 public static void main (String [] args) 3 {4 System. out. println ("90 degree sine value:" + Math. sin (Math. PI/2); 5 System. out. println ("cosine of 0 degree:" + Math. cos (0); 6 System. out. println ("60 degree tangent:" + Math. tan (Math. PI/3); 7 System. out. println ("1's arc tangent value:" + Math. atan (1); 8 System. out. println ("π/2 angle value:" + Math. toDegrees (Math. PI/2); 9 System. out. println (Math. PI); 10} 11}

The above example compilation and running results are as follows:

Sine of 90 degrees: cosine of 1.00 degrees: Tangent of 1.060 degrees: arc tangent of 1.73205080756887671: angle of 0.7853981633974483 π/2: 90.03.141592653589793

 

Java online API: http://tool.oschina.net/apidocs/apidoc? Api = jdk_7u4

 

Alimail: AlanLee

Blog: http://www.cnblogs.com/AlanLee

This article is from the blog site. You are welcome to join the blog site.

 

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.