Generally, we will use the basic data types of data: byte, int, short, long, double, float, boolean, char;
There are also eight corresponding packaging types: Byte, Integer, Short, Long, Double, Float, Character, Boolean;
Packaging types are declared with final and cannot be overridden by inheritance; in actual situations, the compiler will automatically box the basic data type into an object type, or unbox the object type into a basic data type; as follows:
public static void main(String[] args) { int num1 = 1; //Box basic data types into object packaging types Integer num2 = num1; Integer num3 = 3; //Unbox the object data class int num4 = num3; }
The Number class is an abstract class under the java.lang package. It provides a method for unboxing packaged types into basic types. All packaged types of basic types (data types) inherit this abstract class, and its final declaration cannot be inherited and changed. ;
package java.lang; public abstract class Number implements java.io.Serializable { public abstract int intValue(); public abstract long longValue(); public abstract float floatValue(); public abstract double doubleValue(); public byte byteValue() { return (byte)intValue(); } public short shortValue() { return (short)intValue(); } private static final long serialVersionUID = -8742448824652078965L; }
Packaging | Basic data types |
---|---|
Boolean | boolean |
Byte | byte |
Short | short |
Integer | int |
Long | long |
Character | char |
Float | float |
Double | double |
This type of packaging that is specially supported by the compiler is called boxing, so when a 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 unbox an object into a built-in type. Number class belongs to java.lang package.
Here is an example of boxing and unboxing:
publicclassTest{
publicstaticvoidmain(Stringargs[]){
Integerx=5;//boxesinttoanIntegerobject
x=x+10;//unboxestheIntegertoaint
System.out.println(x);
}
}
The compilation and running results of the above example are as follows:
15
When x is assigned to an integer value, the compiler has to box x because x is an object. Then, in order for x to be added, x is unboxed.
Java's Math contains properties and methods for performing basic mathematical operations, such as elementary exponentials, logarithms, square roots, and trigonometric functions.
Math methods are all defined in static form and can be called directly in the main function through the Math class.
Example
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("Arctangent of 1: " + Math.atan(1));
System.out.println("The angle value of π/2: " + Math.toDegrees(Math.PI/2));
System.out.println(Math.PI);
}
}
The compilation and running results of the above example are as follows:
Sine of 90 degrees: 1.0
Cosine of 0 degrees: 1.0
Tangent value of 60 degrees: 1.7320508075688767
Arctangent of 1: 0.7853981633974483
The angle value of π/2: 90.0
3.141592653589793
Number&Math class methods
The following table lists the commonly used Number class and Math class methods:
serial number | Methods and Description | |||||
---|---|---|---|---|---|---|
1 | xxxValue() converts the number object into a value of xxx data type and returns it. | |||||
2 | compareTo() compares the number object with the parameter. | |||||
3 | equals() determines whether the number object is equal to the parameter. | |||||
4 | valueOf() returns an Integer object specifying the built-in data type | |||||
5 | toString() returns the value in string form. | |||||
6 | parseInt() parses a string into type int. | |||||
7 | abs() returns the absolute value of the argument. | |||||
8 | ceil() returns the smallest integer greater than or equal to (>=) the given parameter, type is double precision floating point. | |||||
9 | floor() returns the largest integer less than or equal to (<=) the given argument. | |||||
10 | rint() returns the nearest integer to the argument. The return type is double. | |||||
11 | round() returns the nearest int or long value. | |||||
12 | min() returns the minimum value of the two parameters. | |||||
13 | max() returns the maximum value of the two parameters. | |||||
14 | exp() returns the parameter power of the natural number base e. | |||||
15 | log() returns the logarithm of the natural number base of the argument. | |||||
16 | pow() returns the first parameter raised to the power of the second parameter. | |||||
17 | sqrt() finds the arithmetic square root of the argument. | |||||
18 | sin() finds the sine value of the specified double type parameter. | |||||
19 | cos() finds the cosine value of the specified double type parameter. | |||||
20 | tan() finds the tangent value of the specified double type parameter. | |||||
twenty one | asin() finds the arcsine value of the specified double type parameter. | |||||
twenty two | acos() finds the arc cosine value of the specified double type parameter. | |||||
twenty three | atan() finds the arctangent value of the specified double type parameter. | |||||
twenty four | atan2() converts Cartesian coordinates to polar coordinates and returns the angle value of the polar coordinates. | |||||
25 | toDegrees() converts parameters into angles. | |||||
26 | toRadians() converts angles to radians. | |||||
27 | random() returns a random number. |