Summary of utility classes
The Arrays utility class
@ Test public void Test () {int [] arr =,1,2,3,4,5,6,7,8,9 {0}; System.out.println(arr); Println (arrays.toString (arr)); system.out.println (arrays.toString (arr)); Public static String toString(int[] a) {* if (a == null) * return "null"; * int iMax = a.length - 1; * if (iMax == -1) * return "[]"; * * StringBuilder b = new StringBuilder(); * b.append('['); * for (int i = 0; ; i++) { * b.append(a[i]); * if (i == iMax) * return b.append(']').toString(); * b.append(", "); *} *} * */ / Sort array.sort (arr); Println (Arrays. BinarySearch (arr,6)); /** * public static int[] copyOf(int[] original, int newLength) {* int[] copy = new int[newLength]; * // see system tools * system. arrayCopy (original, 0, copy, 0, * math.min (original. Length, newLength)); * return copy; * } * */ int[] ints = Arrays.copyOf(arr, 15); }Copy the code
Math tools
@Test
public void test1(a){
System.out.println(Math.abs(-100));
System.out.println(Math.min(100.120));
// Round off
System.out.println(Math.round(100.5));/ / 100
System.out.println(Math.round(-100.6));/ / - 101
System.out.println(Math.round(-100.5));/ / - 100
// Returns the largest integer less than or equal to the argument
System.out.println(Math.floor(3.5)); / / 3.0
System.out.println(Math.floor(-3.5)); / / 4.0
// Returns the largest integer greater than or equal to the argument
System.out.println(Math.ceil(3.5)); / / 4.0
System.out.println(Math.ceil(-3.5)); / / 3.0
}
Copy the code
Objects tools
@Test
public void test1(a){
String s1=null;
String s2= "123";
// System.out.println(s1.equals(s2));
/** * NullPointerException because s1 is empty and not an object * if s1 is passed as an argument ** /
System.out.println(Objects.equals(s1,s2));/ / return false source code (a = = b) | | (a! = null && a.equals(b));
}
Copy the code
String utility class
@Test
public void test1(a){
// The string is stored in the string constant pool. The contents are the same. The objects are equal
String s1="123";
String s2="123";
String s3=new String("123");
System.out.println(s1==s2);//true
System.out.println(s1==s3);//false
}
@Test
public void test2(a){
// We can't modify the String.
// This is because it is the content that cannot be modified, which is equivalent to creating a new block of memory and pointing S1 to the new string
// Each addition creates a new memory and assigns the memory address to the reference object
String s1="123";
String s2="123";
s1=s1+s2;
System.out.println(s1==s2);//true
}
@Test
public void test3(a){
// Because of the use of String concatenation strings and their memory usage, the following classes are used to operate
// The StringBuilder thread is not safe. The StringBuffer thread is safe
/** * capacity() returns the current capacity * charAt(int index) returns the char value in this sequence at the specified index. * indexOf(String STR) returns the indexOf the String in which the specified substring first occurs. * insert(int offset, String STR) inserts the String of the STR argument into the sequence. * public delete(int start, int end) removes the character in the substring of this sequence. * /
StringBuffer stringBuffer=new StringBuffer("Start");
stringBuffer.append("123");
stringBuffer.append("456");
System.out.println(stringBuffer);
}
Copy the code
System tools
@Test
public void test1(a){
System.gc();// Call a method to make the JVM collect garbage
}
@Test
public void test2(a){
System.exit(0); //0 indicates normal termination. Non-0 indicates abnormal termination
}
@Test
public void test3(a){
long l = System.currentTimeMillis();// Time stamp of east 8 to the present
System.out.println(l);
}
@Test
public void test4(a){
Public static void arrayCopy (Object SRC, int srcPos, Object dest, int destPos, int length) * SRC - source array. * srcPos - The starting position in the source array. * dest - Destination array. DestPos - The starting position in the target data. * length - The number of array elements to copy. * /
int[] a={1.1.2};
int[] b = new int[10];
System.arraycopy(a,0,b,0,a.length);
System.out.println(Arrays.toString(b));//[1, 1, 2, 0, 0, 0, 0, 0, 0, 0]
}
Copy the code
The Date utility class
public static void main(String[] args) throws ParseException {
Date date=new Date(); // Accurate to the millisecond, the Date object is initialized to represent the specified number of milliseconds since the standard base time (called epoch), i.e. 00:00:00 GMT, January 1, 1970.
System.out.println(date.getTime());/ / 1632212649632 milliseconds
System.out.println(date.getTime()-(24*60*60*1000));// Subtract one day
/ * * * * y: year M: * d: * H: (24) H: (12) * M: * s: seconds * * /
// Format the timestamp
//SimpleDateFormat's parent class is DateFormat(abstract class), which allows formatting (date → text), parsing (text → date), and normalization.
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//format(object to be formatted) Format as required
String format1 = simpleDateFormat.format(new Date());
System.out.println(format1);
Date parse = null;
// Convert the timestamp to a Date object
parse = simpleDateFormat.parse("The 2021-09-21 16:35:03");
System.out.println(parse);//Tue Sep 21 16:35:03 CST 2021
}
@Test
public void Test1(a){
Calendar Abstract Calendar class uses Calendar getInstance(Locale aLocale) to get a Calendar object
Calendar calendar =Calendar.getInstance();
Public int get(int field) * {* complete(); * return internalGet(field); * } * protected final int internalGet(int field) * { * return fields[field]; *} * Calendar.YEAR is a constant defined in the Calendar class to represent the YEAR, month, day, etc. in the array subscript */
int i = calendar.get(Calendar.YEAR);
System.out.println(i); / / 2021
int i1 = calendar.get(Calendar.DAY_OF_YEAR);
System.out.println(i1);/ / 265
calendar.set(Calendar.YEAR,2023);
System.out.println(calendar.get(Calendar.YEAR));/ / 2023
/ / in 0-11
calendar.add(Calendar.MONTH,4); // The current value is 8 + 4-> 9 10 11 0
System.out.println(calendar.get(Calendar.MONTH)); / / 0
Date d=calendar.getTime();
System.out.println(d); //Mon Jan 22 11:18:18 CST 2024
// Maximum number of days in the current month
int actualMaximum = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
System.out.println(actualMaximum);/ / 31
}
Copy the code
BigDecimal(large floating point) and BigInteger (large integer) utility classes
public static void main(String[] args) {
// Errors occur when using floating-point arithmetic
System.out.println(0.1+0.2);/ / 0.30000000000000004
/** * Arithmetic with two BigDecimal objects * Returns a return value of type BigDecimal * add() * Subtract () * multiply() * remainder() * divide() * pow(); a.pow(b)=a^b * gcd(); Maximum common divisor * ABS (); Absolute value * negate(); Take the inverse * mod(); a.mod(b)=a%b=a.remainder(b); * max(); min(); * punlic int comareTo(); * boolean equals(); * /
BigDecimal b1=new BigDecimal(0.1);
/ / BigDecimal b1 = BigDecimal. The valueOf (0.1); // Either way
BigDecimal b2=new BigDecimal(0.2);
BigDecimal b3 = b1.add(b2);
System.out.println(b3); / / 0.3
System.out.println(b3 instanceof BigDecimal); //true
// Convert to double for other types
double v = b3.doubleValue();
/** * The same as a large floating point number is a large integer BigInteger which has the same method as BigDecimal ** /
}
Copy the code