Method Rewriting (Supplementary)
In case of rewriting:
- Inheritance
- Method Name Same
- The list of parameters should be identical (order, number, type)
- When the return value type is a reference type, the return value type of the subclass is exactly the same when it is less than or equal to the basic type of the return value type of the parent class.
- The modifier permission of the subclass is greater than or equal to that of the parent class (the modifier of the parent class cannot be private)
- Exceptions thrown by subclasses are less than or equal to those thrown by parent classes (runtime exceptions are thrown by any method)
public class Test01 { public static void main(String[] args) { } } class F{ F fun(F s) { System.out.println(1/0); return null; } } class S extends F{ @Override public S fun(F s)throws NullPointerException{ return null; } }
String class
String: A sequence of characters (immutable strings)
How String objects are created:
package com.mage.string; import java.io.UnsupportedEncodingException; public class Test01 { public static void main(String[] args) throws UnsupportedEncodingException { String str1=new String(); System.out.println(str1); //Create objects directly through double quotation marks String str2="hello"; //Assignment by string constants String str3=""; //Create string objects according to byte arrays byte[] buf=new byte[] {65,66,67,68,69}; String str4=new String(buf); System.out.println(str4); str4=new String(buf, 2, 2); System.out.println(str4); str4=new String(buf,2,2,"UTF-8"); System.out.println(str4); //Create string objects according to character arrays char[] ch=new char[] {'you','good','drift','bright'}; str4=new String(ch); System.out.println(str4); str4=new String(ch,0,2); System.out.println(str4); //Create string objects according to code points int[] ins=new int[] {88,89,90,99}; str4=new String(ins,0,4); System.out.println(str4); //Create objects according to strings String str5=new String("abc"); System.out.println(str5); } }
Common methods in String:
charAt(int index): Returns the character at the specified index location. codePointAt(index): Returns the code point of the element whose index location is specified. compareTo(String): Compare the sizes of two strings concat(String): Splicing copyValueOf (char []): Spliced into a new string getBytes(): Gets a byte array of strings getChars(): Gets an array of characters for a string indexOf(): Find the location of the element and return a negative number to indicate that it was not found isEmpty(): See if the array is empty replace(): replace splite(): interception substring(): interception toUpperCase (): Uppercase toLowerCase (): Change to lowercase
public class Test02 { public static void main(String[] args) { // Create a String object String str = "ilovejavaandlaoxue"; //charAt(int index); char ch = str.charAt(3); System.out.println("Returns the specified character at the current index position:"+ch); //codePointAt(index) int num = str.codePointAt(0); System.out.println("Returns the code point of the element at the specified index location:"+num); //compareTo(String) String cStr = "ilovejavaandlaoxu"; num = str.compareTo(cStr); System.out.println("Compare the size of two strings:"+num); //compareToIgronCase(String) cStr = "ILOVEJAVAANDLAOXUE"; num = str.compareToIgnoreCase(cStr); System.out.println("Compare the size of two strings (ignoring case):"+num); //concat(String) cStr = "andgirl"; str = str.concat(cStr); System.out.println("The result of splicing is as follows:"+str); //copyValueOf(char[]) char[] chs = new char[] {'i','l','o','v','e'}; str = String.copyValueOf(chs); System.out.println("Create a new string:"+str); str = String.copyValueOf(chs,1,4); System.out.println("Create a new string:"+str); str = "Simple and honest.avi"; boolean flag = str.endsWith(".avi"); System.out.println("str Is it?.avi Format:"+flag); //getBytes byte[] buf = str.getBytes(); System.out.println("Gets the byte array of the string:"+Arrays.toString(buf)); //getChars(); chs = new char[10]; // Copy from the 2 index of str to the end of the 5 index of str (excluded) to the first position in the chs character array str.getChars(2,6, chs, 1); System.out.println("Gets an array of characters for a string:"+Arrays.toString(chs)); //indexOf returns a negative number to indicate that it was not found str = "hehe.avi.png"; int index = str.indexOf("."); System.out.println(".stay string For the first time, the location is:"+index); index = str.indexOf(100); System.out.println("e stay string For the first time, the location is:"+index); index = str.indexOf(".",5); System.out.println(".stay string The fifth index position is calculated, and the first place to appear is:"+index); index = str.lastIndexOf("."); System.out.println(".stay string The last place to appear is:"+index); //isEmpty() System.out.println("See if the array is null:"+str.isEmpty()); //repleace str = "hehe.avi.png"; str = str.replace(".", "+"); System.out.println("The result after replacement:"+str); //splite: str = "login?unme=zs&pwd=zs"; String[] strs = str.split("\\?"); for(String string:strs) { System.out.println(string); } System.out.println("======"); String parms = strs[1]; strs = parms.split("&"); for(String string:strs) { String[] ss = string.split("="); System.out.println(ss[1]); } //subString str = "login?unme=zs&pwd=zs"; str = str.substring(4);// Interception from 4 Index to End System.out.println("Intercept strings:"+str); str = str.substring(2, 7);//Cut from 2 index to 7 index and end without 7 System.out.println("Intercept strings:"+str); //toggle case str = "login?unme=zs&pwd=zs"; str = str.toUpperCase(); System.out.println("Turn to capital letters:"+str); str = str.toLowerCase(); System.out.println("Turn to lowercase:"+str); //Creating String Objects by Static Method String str1 = String.valueOf(123); System.out.println(str1); //Object to String is actually a toString method that calls the current object User u = new User(10); String str2 = String.valueOf(u); System.out.println(str2); } }
Differences and connections between StringBuffer and StringBuilder:
- StringBuffer and StringBuilder are both inherited from AbstractStringBuilder
- StringBuffer is less efficient than StringBuilder
- StringBuffer is more secure than StringBuilder
- Generally, StringBuilder is used
Packaging
- Eight packaging classes evolved from eight basic data types
byte->Byte short->Short char ->Character int ->Integer long ->Long float->Float double->Double boolean->Boolean
-
All constructors in JDK12 Integer are obsolete and get int corresponding to Integer object by valueOf method
-
String -> int with Integer.parseInt() method
-
int -> String with String.valueOf()
-
After JDK 1.5, automatic disassembly and packing is supported:
- Boxing: The Integer.valueOf() method is called
- Unpacking: Object. intValue()
-
tips: When boxed automatically, it first determines whether the current value is in the buffer [-128,127], if in the interval, it gets the corresponding Integer object directly from the buffer; if not, it overrides to create a new Integer object.
public class Test02 { public static void main(String[] args) { Integer in1 = 123; int num = in1; Integer in2 = 88; Integer in3 = 88; System.out.println(in2==in3); in2 = 127; in3 = 127; System.out.println(in2==in3); } }
- When literal values create Boolean objects, only true and false can be specified and the corresponding true and false objects can be created.
- Strings create Boolean objects that correspond to true and false as long as the character is true/false and it doesn't matter whether it's case or case.
- All other string assignments are false
public class Test03 { public static void main(String[] args) { Boolean f1 = new Boolean(true); System.out.println(f1); f1 = new Boolean(false); System.out.println(f1); f1 = new Boolean("1"); System.out.println(f1); f1 = new Boolean("0"); System.out.println(f1); f1 = new Boolean("TRue"); System.out.println(f1); f1 = new Boolean("False"); System.out.println(f1); f1 = new Boolean("123"); System.out.println(f1); } }
Date class
The empty constructor creates the current system time object
public class Test02 { public static void main(String[] args) { //Create a time object Date date = new Date(); //Creating SimpleDateFormat Objects SimpleDateFormat sd = new SimpleDateFormat(); //Set the content format of the output sd.applyPattern("yyyy year MM month dd day HH time mm branch ss Second is the first in a year. D day W w"); //Call formatting method to format date String str = sd.format(date); System.out.println(str); //Create objects and specify formatted content SimpleDateFormat ss = new SimpleDateFormat("yy/MM/dd hh:mm:ss"); System.out.println(ss.format(date)); } }