About common classes

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
// If the two names access the external attribute at the same time, add Outer.this

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(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(){                 
        }              // 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    
            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(){          
      public void service() {       
          System.out.println("How do you do");          
      }        };

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


//In my opinion, toString is mostly used in the text text to String input


        String a = "Weibei spring tree";
        String b = "Weibei spring tree";
        //String b = "sunset cloud in Jiangdong";
         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); 

//Convert string to int 
//Use Integer.parseInt 

String string = "666"; 
int num = Integer.parseInt(string); 

//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); 

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    

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;   


[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";

//charAt() returns the character of a position

//contains() determines whether a string is included. If yes, true is returned

toCharArray() returns the array corresponding to the string

indexOf() returns the first occurrence of a string

lastIndexOf() returns the last occurrence of a string



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 ";

String yyy = "hello,java";

Replacea() replaces the string

split() splits the string

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)   

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

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)       
    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      

Use of StringBuffer and StringBuilder (more efficient)

  1. append add
  2. insert()
  3. replace()
  4. delete(0,str.length); delete
  5. reverse() reverse
    StringBuffer sb = new StringBuffer();   
//append add    
sb.append("java World first"); 
sb.append("java not bad");   
sb.append("java Really fragrant");  

sb.insert(0,"I'm at the front");   


//delete(); delete    

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));


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,,

double result = (0.4+0.5)/0.9;

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 date1 = new Date();   

//Yesterday, today minus milliseconds   
Date date2 = new Date(date1.getTime()-(60*60*24*1000));  

// Method after before   

// Compare compareTo   

// Compare equality equals   


 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        
     int year = calendar1.get(Calendar.YEAR);      
     int month = calendar1.get(Calendar.MONTH);       
     int day = calendar1.get(Calendar.DAY_OF_MONTH);     
     int hour = calendar1.get(Calendar.HOUR);       
     int minute = calendar1.get(Calendar.MINUTE);
     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);                


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);   
    //Convert string to date   
    Date date2 = sdf.parse("18/05/01");   

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];   
    for(int c:b)        
    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);    

Keywords: Java

Added by Jassal on Mon, 08 Nov 2021 16:57:41 +0200