catalogue
2, Overview of basic types and packaging classes
What is a basic type wrapper class
Packaging classes corresponding to various basic data types
3, Conversion between String and int types
4, Automatic packing and unpacking of JDK5
1, Arrays class
-
summary
A tool class that operates on arrays.
It provides functions such as sorting and searching.
-
Member method
public static String toString(int[] a) //Converts the passed in integer array into a string and returns
Example:
import java.util.Arrays; public class Test3 { public static void main(String[] args) { int[] a ={1,2,3,5,4,8,2,6,52}; System.out.println(Arrays.toString(a)); } }
public static void sort(int[] a) //Sort the passed in integer array
Example:
import java.util.Arrays; public class Test3 { public static void main(String[] args) { int[] a ={1,2,3,5,4,8,2,6,52}; Arrays.sort(a); System.out.println(Arrays.toString(a)); } }
public static int binarySearch(int[] a,int key) //Binary search, the array passed in must be ordered
Example:
import java.util.Arrays; public class Test3 { public static void main(String[] args) { int[] a ={1,2,3,5,4,8,2,6,52}; Arrays.sort(a); System.out.println(Arrays.binarySearch(a, 5)); } }
static boolean equals(int[] a, int[] a2) //Compare the elements in the two arrays. Are they the same
Example:
import java.util.Arrays; public class Test3 { public static void main(String[] args) { int[] a = {1, 2, 3, 5, 4, 8, 2, 6, 52}; int[] b = {1, 2, 3, 5, 4, 8, 2, 6, 52}; int[] c = {1, 2, 3, 5}; System.out.println(Arrays.equals(a, b)); System.out.println(Arrays.equals(b, c)); } }
static int[] copyOf(int[] original, int newLength) //Copy the elements from the old array to a new array, //The new array length is newLength. Copy the old array from 0
Example:
import java.util.Arrays; public class Test3 { public static void main(String[] args) { int[] a = {1, 2, 3, 5, 4, 8, 2, 6, 52}; int[] b = Arrays.copyOf(a, 5); System.out.println(Arrays.toString(b)); } }
static int[] copyOfRange(int[] original, int from, int to) //Copy several elements in the specified range in the old array to the new array from start index to end index
Example:
import java.util.Arrays; public class Test4 { public static void main(String[] args) { int[] arr = {1, 23, 4, 6, 20}; int[] arr1 = Arrays.copyOfRange(arr,0,3); System.out.println(Arrays.toString(arr1)); } }
2, Overview of basic types and packaging classes
-
What is a basic type wrapper class
In order to perform more and more convenient operations on basic types, java provides corresponding class types for each basic data type. The basic type wrapper class is to wrap the function methods of basic types such as int, short, byte and char in the corresponding class for our convenience.
-
Packaging classes corresponding to various basic data types
byte Byte short Short int Integer long Long float Float double Double char Character boolean Boolean
-
Integer class
summary:
The Integer class wraps a value of the basic type int in the object. This class provides multiple methods that can convert between int and String types. There are also some other constants and methods that are very useful for handling int types.
Construction method:
//Construct a new Integer object that represents the specified int value public Integer(int value) //Construct a new Integer object, which represents the int value indicated by the String parameter, and s is the String to be converted to Integer. public Integer(String s)
method:
//Returns a string representation of an integer parameter as a binary (Radix 2) unsigned integer public static String toBinaryString(int i) //Returns a string representation of an integer parameter as an octal (Radix 8) unsigned integer public static String toOctalString(int i) //Returns an unsigned integer as a hexadecimal argument public static String toHexString(int i)
3, Conversion between String and int types
-
int --> String
1. Empty string splicing 2. valueOf method: public static String valueOf(int i) 3. Complete through Integer 4. toString method: public static String toString(int i)
The example is completed by Integer:
public class Test6 { public static void main(String[] args) { Integer integer = new Integer(100); System.out.println(integer.toString()); } }
-
String --> int
1. Complete through Integer 2. parseInt method: public static int parseInt(String s)
The example is completed by Integer:
public class Test6 { public static void main(String[] args) { Integer integer = new Integer("100"); System.out.println(integer); } }
4, Automatic packing and unpacking of JDK5
-
Automatic packing: convert basic type to packing type;
Automatic unpacking: convert packaging type to basic type.
-
Case demonstration:
public class Test6 { public static void main(String[] args) { Integer i =10; //Automatic packing i+=20; //Automatic unpacking } }
-
matters needing attention
When used, Integer x = null; NullPointerException will appear in the code. It is recommended to judge whether it is null before using it.