# 1. Basic types of packaging

Recall that in the third article, when we learned about the basic data types in Java, we said that there are eight basic data types in Java, but these data are basic data, and it is very difficult to perform complex operations on them. What shall I do?

## 1.1 Summary of Basic Types of Packaging Classes

In the actual use of the program, the data input by the user on the program interface are stored in the string type. In the process of program development, we need to convert the string data into specified basic data types according to the needs, such as age needs to be converted into int type, test scores need to be converted into double type, etc. So what about the conversion between strings and basic data?

Java provides corresponding objects to solve this problem. Basic data type object wrapper class: Java encapsulates basic data type values into objects. What are the benefits of encapsulating objects? It can provide more functions to manipulate basic values.

The packaging categories corresponding to the eight basic types are as follows:

It should be noted that int corresponds to Integer, char corresponds to Character, and the other six are capital letters of the basic type.

Basic Data Type Object Packaging Class Features: Used for conversion between basic data and strings.

Converts a string to a basic type:

parseXXX(String s); where XXX represents the basic type and the parameter is a string that can be converted to the basic type. If the string cannot be converted to the basic type, the problem of NumberFormatException will occur.

System.out.println(Integer.parseInt("123") + 2); //The print result is 125

There are three ways to convert basic values into strings:

Basic types can be directly connected with "; 34+"

Call String's valueOf method; String.valueOf(34);

Call the toString method in the wrapper class; Integer.toString(34);

## 1.2 Basic Types and Object Conversion

The use of int type and Integer object transformation for demonstration, other basic type conversion methods are the same.

Basic Value - > Packaging Object:

Integer i = new Integer(4);//Using constructor functions Integer ii = new Integer("4");//A numeric string can be passed in the constructor

Integer iii = Integer.valueOf(4);//Use the valueOf Method Integer iiii = Integer.valueOf("4");//Use the valueOf Method

Packaging Object - > Basic Number

```
````int num = i.intValue();`

## 1.3 Automated Packing and Unpacking

The basic type and packaging type can be used in general when necessary. Sometimes when we have to use a reference data type, we can pass in the basic data type.

For example:

Basic types can be calculated directly using operators, but reference types cannot. The basic type wrapper class, as one of the reference types, can be computed because Java "secretly" automatically converts objects to basic data types.

Correspondingly, the value of the reference data type variable must be the new out-of-memory address value, and we can assign a basic type value to a reference of a basic type wrapper class. The reason is also that Java automatically converts basic data types to objects "stealthily"

Auto-unboxing: converting objects to basic values

Auto-packing: basic values converted into objects

Integer i = 4;//Automatic packing. Amount to Integer i = Integer.valueOf(4); i = i + 5;//On the right side of the equal sign: i Object to Basic Number(Auto-disassembly) i.intValue() + 5; After the addition operation is completed, the basic values are converted into objects.

Demonstration of details of automatic packing (byte constant pool)

When the value is within the byte range, automatic packing will not create new object space, but use the existing space of the doctor.

Integer a = new Integer(3); Integer b = new Integer(3); System.out.println(a==b);//false System.out.println(a.equals(b));//true System.out.println("---------------------"); Integer x = 127; Integer y = 127; //stay jdk1.5 When packing automatically, if the value is in byte Within the scope, the object space will not be created, but the existing space will be used. System.out.println(x==y); //true System.out.println(x.equals(y)); //true

# 2. System class

## 2.1 Concept

The introduction of System class in API is relatively simple. We give the definition. The system represents the system where the program is located, and provides some corresponding system attribute information and system operation.

The System class cannot create objects manually because the constructor is private ly modified to prevent the outside world from creating objects. In the System class, all static methods are accessible by class name. There are many such classes in JDK.

## 2.2 Common methods

CurrtTime Millis () Gets the millisecond difference between the current system time and 00:00 on January 01, 1970

exit(int status) is used to end a running Java program. The parameter can be passed in a number. Input 0 is usually recorded as normal and the others as abnormal.

gc() is used to run the garbage collector in the JVM to complete the garbage removal in memory.

getProperty(String key) is used to obtain system attribute information recorded in the specified key (string name)

The arraycopy method, which is used to copy some elements of the source array to the specified location of the target array

## 2.3 Method Exercises of System Class

Exercise 1: Verify the time (milliseconds) required to print the number 1-9999 for a loop.

public static void main(String[] args) { long start = System.currentTimeMillis(); for (int i=0; i<10000; i++) { System.out.println(i); } long end = System.currentTimeMillis(); System.out.println("It takes milliseconds:" + (end-start) ); }

Exercise 2: Copy the first three elements of the src array to the first three positions of the dest array

Before copying elements: src array elements [1, 2, 3, 4, 5], dest array elements [6, 7, 8, 9, 10]

After copying elements: src array elements [1, 2, 3, 4, 5], dest array elements [1, 2, 3, 9, 10]

public static void main(String[] args) { int[] src = new int[]{1,2,3,4,5}; int[] dest = new int[]{6,7,8,9,10}; System.arraycopy( src, 0, dest, 0, 3); After the code runs, the elements in the two arrays change src array elements [1,2,3,4,5] dest array elements [1,2,3,9,10] }

Exercise 3: Cyclically generate three digits between 100 and 999 and print the number. When the number can be divided by 10, the running program ends.

public static void main(String[] args){ Random random = new Random(); while(true){ int number = random.nextInt(900)+100; //0-899 + 100 if (nmumber % 10 == 0) { System.exit(0); } } }

# 3. Math class

## 3.1 Concept

Math class is a mathematical tool class that contains methods for performing basic mathematical operations, such as elementary exponents, logarithms, square roots and trigonometric functions.

Tool classes like this are all static methods and generally do not create objects. For example, the System class

Tool classes: Represents classes that perform a series of functions without creating objects. The methods in this class are static methods.

## 3.2 Common Methods

abs method, the results are positive

double d1 = Math.abs(-5); // d1 The value is 5. double d2 = Math.abs(5); // d2 The value is 5.

ceil method, the result is the double value of the smallest integer larger than the parameter value

double d1 = Math.ceil(3.3); //d1 The value is 4..0 double d2 = Math.ceil(-3.3); //d2 The value is -3.0 double d3 = Math.ceil(5.1); // d3 The value is 6..0

floor method, the result is the double value of the largest integer smaller than the parameter value

double d1 = Math.floor(3.3); //d1 The value is 3..0 double d2 = Math.floor(-3.3); //d2 The value is-4.0 double d3 = Math.floor(5.1); //d3 The value is 5..0

The max method returns the larger of the two parameter values

double d1 = Math.max(3.3, 5.5); //d1 The value is 5..5 double d2 = Math.max(-3.3, -5.5); //d2 The value is-3.3

min method, which returns the smaller of the two parameter values

double d1 = Math.min(3.3, 5.5); //d1 The value is 3..3 double d2 = Math.max(-3.3, -5.5); //d2 The value is-5.5

The pow method returns the value of the second parameter power of the first parameter

double d1 = Math.pow(2.0, 3.0); //d1 The value is 8..0 double d2 = Math.pow(3.0, 3.0); //d2 The value is 27..0

round method, which returns the result of rounding parameter values

double d1 = Math.round(5.5); //d1 The value is 6..0 double d2 = Math.round(5.4); //d2 The value is 5..0

The random method generates a double decimal with a value greater than or equal to 0.0 and less than 1.0

```
````double d1 = Math.random();`

# 4. Array class

## 4.1 Concept

This class contains various methods for manipulating arrays, such as sorting and searching. Note that if the specified array is referenced to null, accessing methods in this class throws a null pointer exception NullPointerException.

## 4.2 Common Methods

Sort method, which is used to sort elements in a specified array (element values are sorted from small to large)

//source arr Array element{1,5,9,3,7}, After sorting arr The array element is{1,3,5,7,9} int[] arr = {1,5,9,3,7}; Arrays.sort( arr );

The toString method, which returns the string form of the content of the specified array element

int[] arr = {1,5,9,3,7}; String str = Arrays.toString(arr); // str The value is[1, 3, 5, 7, 9]

The binarySearch method finds the location of a given element value in a specified array. If not, the return location is - 1. Requires that the array must be an ordered array.

int[] arr = {1,3,4,5,6}; int index = Arrays.binarySearch(arr, 4); //index The value is 2. int index2= Arrasy.binarySearch(arr, 2); //index2 The value is-1

## 4.3 Method Exercises of Array Class

Exercise 1: Define a method that receives an array and stores 10 student test scores. This method requires that the last three test scores with the lowest test scores be returned.

public static int[] method(double[] arr){ Arrays.sort(arr); //Sort array elements (element values from small to large) int[] result = new int[3]; //Three test scores after storage System.arraycopy(arr, 0, result, 0, 3);//hold arr The first three elements of the array are copied to result In arrays return result; }

# 5. Big Data Operations

## 5.1 BigInteger

In the Java world, integers over long type can no longer be called integers. They are encapsulated as BigInteger objects. In the BigInteger class, all four operations are implemented by methods, not operators.

The construction method of BigInteger class:

In the construction method, integers are given in the form of strings.

Four arithmetic codes:

public static void main(String[] args) { //Big data is encapsulated as BigInteger object BigInteger big1 = new BigInteger("12345678909876543210"); BigInteger big2 = new BigInteger("98765432101234567890"); //add Implementing additive operations BigInteger bigAdd = big1.add(big2); //subtract Implementing subtraction operation BigInteger bigSub = big1.subtract(big2); //multiply Implementing multiplication BigInteger bigMul = big1.multiply(big2); //divide Implementing division operation BigInteger bigDiv = big2.divide(big1); }

## 5.2 BigDecimal

What problems will arise when the following code is executed in the program?

System.out.println(0.09 + 0.01);

System.out.println(1.0 - 0.32);

System.out.println(1.015 * 100);

System.out.println(1.301 / 100);

double and float types are easy to lose accuracy in operation, which results in inaccuracy of data. Java provides us BigDecimal class to realize high-precision operation of floating-point data.

The construction method is as follows:

It is recommended that floating-point data be given in the form of strings, since the results of parameters are predictable.

The code for implementing addition, subtraction, multiplication and multiplication is as follows:

public static void main(String[] args) { //Big data is encapsulated as BigDecimal object BigDecimal big1 = new BigDecimal("0.09"); BigDecimal big2 = new BigDecimal("0.01"); //add Implementing additive operations BigDecimal bigAdd = big1.add(big2); BigDecimal big3 = new BigDecimal("1.0"); BigDecimal big4 = new BigDecimal("0.32"); //subtract Implementing subtraction operation BigDecimal bigSub = big3.subtract(big4); BigDecimal big5 = new BigDecimal("1.105"); BigDecimal big6 = new BigDecimal("100"); //multiply Implementing multiplication BigDecimal bigMul = big5.multiply(big6); }

For the division of floating-point data, unlike integers, infinite non-cyclic decimals may occur, so it is necessary to reserve and select rounding modes for the required digits.