number
Normally, when we process numbers, we use primitive data types such as byte, int, long, double, etc.
Example
int i = 5000;float gpa = 13.65;byte mask = 0xaf;
However, in development, we encounter situations where we need to use objects instead of primitive data types. To achieve this, Java provides wrapper classes for each original data type.
All wrapper classes (Integer, Long, Byte, Double, Float, Short) are subclasses of the abstract class Number.
This packaging is processed by the compiler, and this process is called boxing. Therefore, when a primitive data type is used, when an object is needed, the compiler puts the primitive data into its wrapper class. Similarly, the compiler can also take the object out and return it to the original data type. Number is part of the java.lang package.
Here is an example of packing and unboxing:
public class Test{ public static void main(String args[]){ Integer x = 5; // boxes int to an Integer object x = x + 10; // unboxes the Integer to a int System.out.println(x); }}This will produce the following results:
Copy the code as follows: 15
When x is assigned an integer value, the compiler puts the integer into the box because x is an integer object. x is then unboxed so that they can be added as integers.
Number method
Here is a list of instance methods implemented in all subclasses implemented by the Number class:
| SN | Method Description |
|---|---|
| 1 | xxxValue() The value of this Number object is converted to the data type of XXX and returns |
| 2 | compareTo() Compare this Number object with parameters |
| 3 | equals() Determine whether this number object is equal to a parameter |
| 4 | valueOf() Returns an Integer object holding the specified original value |
| 5 | toString() Returns a String object representing the value of the specified int or integer |
| 6 | parseInt() This method is used to get the original data type of a string |
| 7 | abs() Return the absolute value of the parameter |
| 8 | ceil() The minimum integer returned is greater than or equal to this parameter. Return as double |
| 9 | floor() The maximum integer returned is less than or equal to this parameter. Return as double |
| 10 | rint() Returns an integer, which is the closest value to the parameter value. Return as double |
| 11 | round() Returns the closest long or int, the parameter referred to by the return type of this method |
| 12 | min() Returns the smaller of the two parameters |
| 13 | max() Returns the larger of the two parameters |
| 14 | exp() Returns the base e of the natural logarithm, the power value of this parameter |
| 15 | log() Returns the natural logarithm of the parameter |
| 16 | pow() Returns the power value of the first parameter to the second parameter |
| 17 | sqrt() Return the square root of the parameter |
| 18 | sin() Returns the sine value of the specified double value |
| 19 | cos() Returns the cosine value of the specified double value |
| 20 | tan() Returns the tangent value of the specified double value |
| twenty one | asin() Returns the inverse sine of the specified double value |
| twenty two | acos() Returns the inverse cosine value of the specified double value |
| twenty three | atan() Returns the arctangent value of the specified double value |
| twenty four | atan2() Convert the Cartesian coordinates (x,y) to polar coordinates (r,θ) and return θ |
| 25 | toDegrees() Convert parameters to degrees |
| 26 | toRadians() Convert parameters to radians |
| 27 | random() Return a random number |
Array
Java provides a data structure: an array, which stores a fixed-size continuous set of elements of the same type. Arrays are collections used to store data, but arrays are often considered as collections of variables of the same type.
In contrast to declaring a single variable, such as number0, number1, ... number99, declares an array variable, such as numbers and uses numbers[0], numbers[1] ..., numbers[99] to represent each variable.
This tutorial will explain how to declare array variables using index variables, create arrays, and process arrays.
Declare array variables
To use an array of a program, you must declare a variable to reference the array, and you must specify the type that the array's variable can reference. Here is the syntax to declare an array variable:
dataType[] arrayRefVar; // preferred way.
or
dataType arrayRefVar[]; // works but not preferred way.
Note style dataType[] arrayRefVar is preferred. Style dataType arrayRefVar[] comes from the C/C++ language, which is convenient for Java to inherit the C/C++ programming style.
Example
The following code snippet is an example of this syntax:
double[] myList; // preferred way.
or
double myList[]; // works but not preferred way.
Create an array
An array can be created by using the following syntax using the new operator:
arrayRefVar = new dataType[arraySize];
The above statement does two things:
Declare array variables, create an array, and assign them to variable array references can be used in combination in a statement, as follows:
dataType[] arrayRefVar = new dataType[arraySize];
Additionally, you can create an array as follows:
dataType[] arrayRefVar = {value0, value1, ..., valuek};Array elements are accessed through indexes. The subscripts of the array start at 0, that is, they start at 0 to arrayRefVar.length-1.
Example
The following statement declares an array variable myList, creates an array of 10 elements of double type, and assigns its reference to myList:
double[] myList = new double[10];
The following image represents the array myList. Here, myList has 10 double values, and the index is from 0 to 9.
Processing arrays
When processing array elements, loop loops or foreach loops are often used because all elements in an array are of the same type and the size of the array is known.
Example
Here is a complete example demonstrating how to create, initialize, and process an array:
public class TestArray { public static void main(String[] args) { double[] myList = {1.9, 2.9, 3.4, 3.5}; // Print all the array elements for (int i = 0; i < myList.length; i++) { System.out.println(myList[i] + " "); } // Summing all elements double total = 0; for (int i = 0; i < myList.length; i++) { total += myList[i]; } System.out.println("Total is " + total); // Finding the largest element double max = myList[0]; for (int i = 1; i < myList.length; i++) { if (myList[i] > max) max = myList[i]; } System.out.println("Max is " + max); }}This will produce the following results:
1.92.93.43.5Total is 11.7Max is 3.5
foreach loop
JDK 1.5 introduces a new for loop called foreach loop or enhanced for loop, which does not require an index variable to completely traverse the array.
Example
The following code shows all elements in the array myList:
public class TestArray { public static void main(String[] args) { double[] myList = {1.9, 2.9, 3.4, 3.5}; // Print all the array elements for (double element: myList) { System.out.println(element); } }}This will produce the following results:
1.92.93.43.5
Pass an array to a method
Just like a method that passes a primitive type value, an array can also be passed to a method. For example, the following method shows elements in an int array:
public static void printArray(int[] array) { for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); }}You can call it by passing an array. For example, the following statement calls the method PrintArray to display 3, 1, 2, 6, 4, 2:
printArray(new int[]{3, 1, 2, 6, 4, 2}); Return an array from a method
A method can also return an array. For example, the method shown below returns an array which is an inversion of another array:
public static int[] reverse(int[] list) { int[] result = new int[list.length]; for (int i = 0, j = result.length - 1; i < list.length; i++, j--) { result[j] = list[i]; } return result;} Arrays Class
The class in java.util.Arrays contains various static methods for sorting and searching arrays, comparing arrays and filling array elements. These methods are overloaded for all primitive types.
| SN | Methods and descriptions |
|---|---|
| 1 | public static int binarySearch (Object[] a, Object key) Use the binary search algorithm to search for the specified array (bytes, integers, doubles, etc.) of the object to specify the value. The array must be classified before this call is made. If it is included in the list (-(insertion point + 1), the index search keyword will be returned. |
| 2 | public static boolean equals (long[] a, long[] a2) Return true if the two specified arrays of a long header are equal to each other. Two arrays are considered equality determination methods: if two arrays contain the same number of elements and all corresponding pairs of the two array elements are equal. Return true if the two arrays are equal. The same method can be used for all other primitive data types (Byte, short, Int, etc.) |
| 3 | public static void fill(int[] a, int val) Puts the specified int value to each element in the specified int-type array. The same method can be used for all other primitive data types (Byte, short, Int etc.) |
| 4 | public static void sort(Object[] a) Arranges the array specified by the object ascending order, according to the natural order of its elements. The same method can be used for all other primitive data types (Byte, short, Int, etc.) |