Common class
Inner class
class Outer{ class Inner { // The inner class also forms a. Class file // Access the private property in outer. // The method + ().... attribute is defined by itself } }яА
public static void main(String[] args) { //Outer outer = new Outer(); //Outer.Inter inter = outer.new Inter(); //Two steps are troublesome. You can take one step directly Outer.Inter inter = new Outer().new Inter(); inter.show(); // The method in another class is not self-contained }
System.out.println(Outer.this.name); // If the two names access the external attribute at the same time, add Outer.this System.out.println(name);
private static cannot be included, that is, static
But you can add a constant to private final static
– about static –
public class StaticText { private String name = "Crossing crane shadow in cold pond"; static class Inner{ private String name = "Cold moon buries the soul"; public void show(){ // static is actually separated from external classes!!!!!! important //So it should be new again StaticText outer = new StaticText(); System.out.println(outer.name); System.out.println(name); }
– local internal classes –
The class in the method, that is, the class cannot be preceded by modifiers such as private, but the contents can be
Main code
package InnerClass.inClass;public class StaticText { private String name = "Crossing crane shadow in cold pond"; public void show(){ String name2 = "Cold moon buries the soul"; class Inner{ private String name3 = "lindaiyu"; public void show2(){ System.out.println(StaticText.this.name); } } // Because a class executes only one method, it will not be executed in the local internal class //So in the outer layer, instantiate the local inner class. Just. fierce! Inner inner = new Inner(); inner.show2(); }}
Test code
package InnerClass.inClass;public class Static { public static void main(String[] args) { StaticText outer = new StaticText(); outer.show(); ,,//The variable object may need to be added with final to prevent it from disappearing } }
//Local inner class
//Use with interface
public class UsbTest { public static void main(String[] args) { //Local inner class //Use with interfa interface class ak implements Usb{ !!!!!!Connected to the interface @Override public void service() { System.out.println("Good wind can send me to Qingyun"); } } Usb / Or call ak ak = new ak(); //Object instantiation ak.service(); //Usb usb = new Mouse(); //usb.service(); }
Anonymous inner classes directly the new interface. I didn't know about the interface before,} add; Rewrite method: alt+insert
Usb usb = new Usb(){ @Override public void service() { System.out.println("How do you do"); } }; usb.service();
Object class
Class class1 = s1.getClass();
Class class2 = s2.getClass();
If see if class 1 and class 2 are equal
. hashCode(), look at the hash code
toString()
System.out.println(b.toString()); //In my opinion, toString is mostly used in the text text to String input
equial()
String a = "Weibei spring tree"; String b = "Weibei spring tree"; //String b = "sunset cloud in Jiangdong"; System.out.println(a.hashCode()); System.out.println(b.hashCode()); System.out.println(a.equals(b)); // If the contents of a and b are equal, they are the same, and the hash value is also equal. Otherwise, false is not returned //But if it is new, although the content is not equal
– finalize –
garbage collection
Use System.gc(). To notify the JVM for garbage collection
Boxing (storing the data in the stack in the heap, basic type (int a = 10; in the stack at the beginning) - "reference type") and unpacking (stacking to the stack)
int age = 10; Integer name = 111; Integer integer = age; int aaa = 111;
String conversion
int n1 = 8;int n2 = 800; //Convert int type to String s1 = n1 + ""; // 2: Using the Interger function String s2 = Integer.toString(n1); // How to change to hexadecimal String s3 = Integer.toString(n1,16); System.out.println(s3); System.out.println(""+n1+n2); //"============================" //Convert string to int //Use Integer.parseInt String string = "666"; int num = Integer.parseInt(string); System.out.println(num); //================= //boolean can also be converted to basic type //Only "ture" is converted to true, and others are false String string2 = "ture"; boolean b1 = Boolean.parseBoolean(string2); System.out.println(b1);
Integer memory area
Integer integer1 = new Integer(200); Integer integer2 = new Integer(200); // If they are not equal, the result is an error //Integer is a reference variable, stored in the stack, and the memory address is compared //So it's different System.out.println(integer1==integer2); Integer integer3 = 100; Integer integer4 = 100; System.out.println(integer3==integer4); //Equal, in buffer //The code is not equal / / because the buffer in Integer is -128 - 127 between; //Beyond this range, it will be created with new. Become different Integer integer5 = 200; Integer integer6 = 200; System.out.println(integer5==integer6);
String
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-xbgwwnlv-1636275871836) (C: \ users \ 74771 \ appdata \ roaming \ typora user images \ 1632213302511. PNG)]
String name = "hello"; name = "zhangsan"; String name2 = "zhangsan";
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-l3xvx61q-1636275871852) (C: \ users \ 74771 \ appdata \ roaming \ typora user images \ 1632214123752. PNG)]
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-6nobyekm-16362758759) (C: \ users \ 74771 \ appdata \ roaming \ typora user images \ 1632214757080. PNG)]
String str = new String("java"); String str2 = new String("java"); //Two are created, one in the heap and one in the string pool, //Like others, str, this basic type is on the stack System.out.println(str == str2);// ==The comparison is that the addresses are not equal System.out.println(str.equals(str2));// Equals compares the content and equals
length() charAt() contains()
String string = "java Is the best language in the world"; //Length System.out.println(string.length()); //charAt() returns the character of a position System.out.println(string.charAt(string.length()-1)); //contains() determines whether a string is included. If yes, true is returned System.out.println(string.contains("java"));
toCharArray() returns the array corresponding to the string
indexOf() returns the first occurrence of a string
lastIndexOf() returns the last occurrence of a string
System.out.println(Arrays.toString(string.toCharArray())); System.out.println(string.indexOf("java")); System.out.println(string.indexOf("java",4)); System.out.println(string.lastIndexOf("java"));
trim() removes spaces before and after the string
toUpperCase() toLowerCase() changes the string from uppercase to lowercase or vice versa
endsWith() startsWith() determines whether it starts or ends with a character
String sss = " hello,WOrld "; System.out.println(sss.trim()); System.out.println(sss.toUpperCase()); System.out.println(sss.toLowerCase()); String yyy = "hello,java"; System.out.println(yyy.endsWith("java")); System.out.println(yyy.startsWith("hello"));
Replacea() replaces the string
split() splits the string
System.out.println(yyy.replace("hello","hi")); String say = "java is the best language of ,prongram"; String[] arry = say.split("[] ,]+");//Important, [,]: indicates a space or comma, and a plus sign indicates several for(String x:arry) System.out.println(x);
equals() compares the contents. There are only two results: equal or unequal, false or true
equalsIgnoreCase() ignores case for comparison
compareTo(): compare sizes
String s1 = "hello"; String s1 = "HELLO"; System.out.println(s1.equalsIgnoreCase(s2));//Ignore case comparison String s3 = "abc"; String s4 = "advsdg"; System.out.println(s3.compareTo(s4));// The comparison code value is - 23, which is different from the previous one String s3 = "abc";String s4 = "abcsdg"; //Compare the string length as before, which is - 3 System.out.println(s3.compareTo(s4));
Change the first letter of the string to uppercase
public static void main(String[] args) { String str = "this is text"; String[] array = str.split(" "); for (String x :array) System.out.println(x); for(int i =0;i<array.length;i++){ char first = array[i].charAt(0); //Convert the first character to uppercase char upperfirst = Character.toUpperCase(first); String news=upperfirst+array[i].substring(1); //substring(1) cut the string from the first position System.out.println(news); }
Use of StringBuffer and StringBuilder (more efficient)
- append add
- insert()
- replace()
- delete(0,str.length); delete
- reverse() reverse
StringBuffer sb = new StringBuffer(); //append add sb.append("java World first"); System.out.println(sb); sb.append("java not bad"); System.out.println(sb); sb.append("java Really fragrant"); //insert() sb.insert(0,"I'm at the front"); System.out.println(sb); //replace() sb.replace(0,5,"hello"); //delete(); delete sb.delete(0,5); System.out.println(sb);}
Can test computer performance
long start = System.currentTimeMillis(); //Time function String string = ""; // Using StringBuffer and StringBuilder will greatly reduce the time //This computer even has a card for(int i=0;i<99999;i++) { string += i; } long end = System.currentTimeMillis(); System.out.println("Time use"+(end-start));
BigDecimal
You can mention double instead
double a=1.0;double b=0.9; // In the double operation, approximate values appear // Higher precision operation is required,, BigDecimalSystem.out.println(a+b); double result = (0.4+0.5)/0.9; System.out.println(result); BigDecimal bd1 = new BigDecimal("1.0"); BigDecimal bd2 = new BigDecimal("0.9"); BigDecimal bd3 = bd1.multiply(bd2); BigDecimal bd4 = bd1.add(bd2); //BigDecimal bd5 = bd1.divide(bd2); //Round BigDecimal r5 = bd1.divide(bd1,2,BigDecimal.ROUND_UP);System.out.println(r5);
Date
//today Date date1 = new Date(); System.out.println(date1); System.out.println(date1.toLocaleString()); //Yesterday, today minus milliseconds Date date2 = new Date(date1.getTime()-(60*60*24*1000)); System.out.println(date2); // Method after before System.out.println(date1.after(date2)); System.out.println(date1.before(date2)); // Compare compareTo System.out.println(date1.compareTo(date2)); // Compare equality equals System.out.println(date1.equals(date2));}
Calender
public static void main(String[] args) { // Create an object because the class is of protected type Calendar calendar1 = new Calendar.getInstance(); System.out.println(calendar1.getTime().toLocaleString()); System.out.println(calendar1.getTimeInMillis()); // Get time information //year int year = calendar1.get(Calendar.YEAR); //month int month = calendar1.get(Calendar.MONTH); //day int day = calendar1.get(Calendar.DAY_OF_MONTH); //hour int hour = calendar1.get(Calendar.HOUR); //minute int minute = calendar1.get(Calendar.MINUTE); //second int second = calendar1.get(Calendar.SECOND); //Modification time Calendar calender2 = Calendar.getInstance(); calender2.set(Calendar.DAY_OF_MONTH,5); System.out.println(calender2.getTime().toLocaleString()); //add method modification time calender2.add(Calendar.HOUR,-1); System.out.println(calender2.getTime().toLocaleString()); //Supplementary maximum value int max = calender2.getActualMaximum(Calendar.DAY_OF_MONTH); int min = calender2.getActualMinimum(Calendar.DAY_OF_MONTH); } }
SimpleDateFormat
Time conversion
public static void main(String[] args) throws Exception{ //Convert date to string SimpleDateFormat sdf = new SimpleDateFormat("yy/MM/dd"); Date date4 = new Date(); String str = sdf.format(date4); System.out.println(str); //Convert string to date Date date2 = sdf.parse("18/05/01"); System.out.println(date2); }
System, time, garbage collection
public static void main(String[] args) { int[] a = {1,2,3,4,5,6,7,8,9}; // b the initial array cannot be empty. new is required int[] b= new int[9]; System.arraycopy(a,0,b,0,a.length); for(int c:b) System.out.println(c); String result = " "; long start = System.currentTimeMillis(); for(int i=0;i<99;i++) { result += i ; } long end = System.currentTimeMillis(); System.out.println(end-start); new Student("zhangsan",87); System.gc(); System.exit(0);}