Common class learning

1.String class

  • String is a constant and cannot be changed after creation

  • String literals are stored in the string pool and can be shared

  • String s = "hello"; Generate an object and store it in the string pool

  • String s = new String("hello"); Two objects are generated, one in heap and one in pool

common method

  • public int length(): returns the length of the string

  • public char charAt(int index): obtain characters according to the offline table

  • public boolean contains(String str): judge whether the current string contains str

  • public char[] toCharArray(): convert string to array

  • public int indexOf(String str): find the subscript that appears for the first time in str. if it exists, the subscript will be returned; If it does not exist, - 1 is returned;

  • public int lastindexOf(String str): find the index of the last occurrence of the string in the current string

  • public String trim(): remove the spaces before and after the string

  • public String toUpperCase(): convert lowercase to uppercase

  • public boolean endWith(String str): determines whether the string ends with str

  • public String replace(char oldChar,char new Char); Replace old string with new string

  • public String[] split(String str): split according to str

2.Object class

  • Superclass, base class, direct or indirect parent class of all classes, located at the top of the inheritance tree

  • For any class, if extensions is not written to show that it inherits a class, it inherits the Object class directly by default, otherwise it inherits indirectly

  • The methods defined in the Object class are the methods that all objects have

  • The Object type can store any Object

    • As a parameter, any object can be accepted

    • As a return value, any object can be returned

1.getClass() method

  • public final Class<?> getClass(){}

  • Returns the actual object type stored in the reference

  • Application: it is usually used to judge whether the actual storage object types in two references are consistent

public class Student {
      private String name;
  ​
      private int age ;
  ​
      public String getName() {
          return name;
      }
  ​
      public void setName(String name) {
          this.name = name;
      }
  ​
      public int getAge() {
          return age;
      }
  ​
      public void setAge(int age) {
          this.age = age;
      }
  ​
      public Student() {
      }
  ​
      public Student(String name, int age) {
          this.name = name;
          this.age = age;
      }
  }

  
  p
ublic class TestStudent {
      public static void main(String[] args) {
          Student s1 = new Student("aaa",20);
          Student s2 = new Student("bbb",22);
  ​
          //Judge whether s1 and s2 are of the same type (getClass method)
          Class class1 = s1.getClass();
          Class class2= s2.getClass();
          if(class1==class2){
              System.out.println("s1 and s2 Belong to the same type");
          }else {
              System.out.println("s1 and s2 Not of the same type");
          }
      }//The result output belongs to the same class
  }

2.hashCode() method

  • public int hashCode(){}

  • Returns the hash code value of the object

  • Hash value a numeric value of type int calculated using the hash algorithm based on the address or string or number of the object

  • Generally, the same object returns the same hash code  

//hashCode method    

 System.out.println(s1.hashCode());//460141958 
  
 System.out.println(s2.hashCode());//1163157884 
   
 Student s3 = s1;    

 System.out.println(s3.hashCode());//460141958 is equal to s1

3.toString() method

  • public String toString(){}

  • Returns a string representation of the object

  • This method can be overridden according to program requirements, such as displaying the attribute values of the object

      public String toString() {
          return getClass().getName() + "@" + Integer.toHexString(hashCode());
      }

4.equals() method

  • public boolean equals(Object obj){}

  • The default implementation is (this == obj). Compare whether the addresses of two objects are the same

  • You can overwrite and compare whether the contents of the two objects are the same

  
   //The equals method determines whether two objects are equal
   System.out.println(s1.equals(s2));//false

  
   public boolean equals(Object obj) {
          return (this == obj);
   }

5.finalize() method

  • When the object is determined to be a garbage object, the JVM automatically calls this method to mark the garbage object and enter the collection queue

  • Garbage object: garbage object when there is no valid reference to this object

  • Garbage collection: GC destroys garbage objects to free up data storage space

  • Automatic recycling mechanism: the JVM runs out of memory and recycles all garbage objects at one time

  • Manual collection mechanism: use System.gc(): notify the JVM to perform garbage collection

Type conversion, packing and unpacking

  • Six common methods are provided in the Number parent class

  • parseXXX() static method

public class demo1 {
      public static void main(String[] args) {
          //Type conversion: the process of converting a basic type into an application type
  ​
          int num1 = 18 ;
  ​
          //Reference the Inter class to create an object
          Integer integer = new Integer(num1);
          Integer integer1 = Integer.valueOf(num1);
          System.out.println("Packing");
          System.out.println(integer);//18
          System.out.println(integer1);//18
  ​
  ​
          //Type conversion: unpack and convert reference type to basic type
          Integer integer2 = new Integer(100);
          int num2=integer2.intValue();
          System.out.println("Unpacking");
          System.out.println(num2);//100
  ​
  ​
          //After jdk1.5, automatic packing and unpacking are provided
          int age = 30;
          //Automatic packing
          Integer integer3 = age;
          System.out.println(integer3);//30
          //Automatic unpacking
          int age2=integer3;
          System.out.println(age2);//30
  ​
  ​
          System.out.println("---------------------------");
          //Conversion between basic type and string
          //1 convert basic type to string
          int n1 =100 ;
          //1.1 use + sign
          String s1 = n1+"";
          System.out.println(s1.getClass().getName());
          //1.2 using toString() method in Integer
          String s2 = Integer.toString(n1);
          System.out.println(s1);//100
          System.out.println(s2);//100
  ​
          //2. Convert string to basic type
          String str = "150" ;
          //Use Integer.parseXXX();
          int n2=Integer.parseInt(str);
          System.out.println(n2);//150
  ​
  ​
          //Convert boolean string form to basic type "true" -- > true
          String str2 = "true";
          boolean b1 = Boolean.parseBoolean(str2);
          System.out.println(b1);//true
      }
  }

Keywords: Java Back-end

Added by fypstudent on Mon, 29 Nov 2021 00:11:50 +0200