Summary of common categories of a Xin, a rookie

Keywords: Java JDK ascii REST

;

Summary of common categories (I)

Object:

java.lang.Object

Object:Java All classes in (custom class, class provided by jdk) inherit from object (all root classes)

public int hashCode() returns the hash code value of the object (the hash code value of each object in memory is different). It has a relationship with the address value (calculated by the hash algorithm). Although it has a relationship with the address value, it is understood as "address value", but it is not the address value in actual sense!

public final Class getClass() returns the runtime class of the current class (that is, the bytecode file is running)

The return value is a reference type (concrete class)

An instance of the Class class represents the Class and interface in the running Java application (related to reflection)

Class member method: public String getName(): returns the entity (class, interface) represented by this class object in the form of String )

//give an example
public class ObjectDemo{
     public static void main(String[] args){
        Student s1=new Student();
        Student s2=new Student();
        System.out.println(s1.hashCode());//305808283
		System.out.println(s2.hashCode());//292938459
        Class  c=s1.getClass();
        System.out.println(c);//class  com.qianfeng.object_ 01. Student / / class class object
     //public String getName()
		String name = c.getName() ;
		System.out.println(name);//com.qianfeng.object_01.Student (fully qualified name of a class: package name. Class name)
        }
}

toString() method in Object class

public String toString(): the string returned to this object indicates that the result should be a concise but easy to read information expression. (it is recommended that all subclasses override this method)

Create a class object: directly outputting the object name is equivalent to using the object name. toString()

toString(): package name. Class name + "@" + hex data = = = > equivalent to an object. Getclass(). Getname() + "@"+ Integer.toHexString (object. hasCode())

public class ObjectDemo {
	public static void main(String[] args) {
		//Create a student class
		Student s1 = new Student("High circle",30) ;
		System.out.println(s1);//com.qianfeng.object_02.Student@7de26db8
		//toString of output s1
	   System.out.println(s1.toString());//com.qianfeng.object_02.Student@7de26db8
       //The essence of toString() is to describe the information expression (property information) of the current object

equals method in Object class

public boolean equals(Object obj): indicates whether an object and the current obj object are "equal"

==: connect two objects: compare whether the address values of two objects are the same

==: if two values are connected (int is the default integer), whether the two values compared are the same!

public class ObjectDemo {
   public static void main(String[] args) {
		//Create student object
		Student s1 = new Student("High circle",27) ;
		Student s2 = new Student("High circle",27) ;
		System.out.println(s1==s2);//false
		Student s3 = s1 ;
		System.out.println(s1==s3);//true
		System.out.println(s1.equals(s2));//false (does not override the equals method in the Object class)
		//True (overriding the equals method in the Object class, comparing whether the contents (member variables) of the current two objects are the same)
		}
}

clone() method in Object class (learn)

protected Object clone(): (shallow clone)
throws CloneNotSupportedException
Create and return a copy of the object

matters needing attention:
The clone method of the Object class performs a specific copy operation.
First of all, if the class of this object cannot implement the interface Cloneable, a CloneNotSupportedException will be thrown

Finalize(): it is related to the garbage collector (when an object has no more references at present, it is recycled by the GC, and this finalize() is called)

System.gc(): manual garbage collector

String:

The String class represents a String.

All string literals in Java programs, such as "abc," are implemented as instances of this class. String is a constant, once assigned, its value cannot be changed! (here refers to the address value)

Construction method:

1) String(): null parameter construction

2) public String(char[] value): convert character array to string

3) String(char[] value, int offset, int count): converts a part of a character array to a string

4) public String(byte[] bytes): converts the number of bytes into a string

5) public String(byte[] bytes, int offset, int length): convert part of byte array to string content

6) String(String original): directly pass a string constant value String("hello");

public class StringDemo {
	
	public static void main(String[] args) {
		//String(): null parameter construction
		String s1 = new String() ;
		System.out.println(s1+"---"+s1.length());
		System.out.println("---------------------");
		//public String(char[] value) / / convert character array to string
		char[] chs = {'a','b','c','d','e'} ;
		//String s2 = new String(chs); / / convert part of the character array to a string
		String s2 = new String(chs, 2, 3) ;
		System.out.println(s2);//"abcde"
	    System.out.println("---------------------");
		//public String(byte[] bytes) / / converts the number of bytes into a string
		byte[] bytes = {97,98,99,100,101} ; 
		//String S3 = new string (bytes); / / 97 ------ > corresponding characters a, B, C in ASCII code table
		String s3 = new String(bytes, 1, 2) ;
	    System.out.println("---------------------");
		String s4 = new String("hello") ;//Similar to: String s4="hello"; (this is different from String s4="hello"; the former is to open space in heap memory and store a copy (two) of constant in constant; the latter is to use constant directly to find constant value, whether it exists, and if it exists, return address (one))
		System.out.println(s4);
		System.out.println(s4.length());
		}
}

Common methods of judging function:

public boolean equals (Object anObject): (common)

Compare whether the contents of two strings are the same (case sensitive)

public boolean equalsIgnoreCase (String anotherString):

Compare whether the contents of two strings are the same, case insensitive

public boolean isEmpty(): judge whether the object is empty

public boolean contains(String s): determine whether the string is included in the specified string

public class StringDemo {
		public static void main(String[] args) {
		String s1 = "helloWorld" ;
		String s2 = "HelloWORLD" ;
		//	public boolean equals (Object anObject): / / compare whether the contents of two strings are the same (case sensitive)
		System.out.println("equals:"+s1.equals(s2));//false
		//public boolean equalsIgnoreCase (String anotherString):
		System.out.println("eqalsIgoreCase:"+s1.equalsIgnoreCase(s2));//true
		//public boolean isEmpty(): judge whether the object is empty
		System.out.println(s1.isEmpty());
		// public boolean contains(String s):
		System.out.println("contains:"+s1.contains("ak47"));
		System.out.println("contains:"+s1.contains("oWo"));
		System.out.println("-------------------------------");
		//public boolean startsWith(String prefix) 
		System.out.println("startsWith:"+s1.startsWith("hel"));
         //public boolean endsWith(String suffix) :
		System.out.println("endsWith:"+s1.endsWith("ld"));
		}
}

==Difference with equals:

==: connect two objects, and check whether the address values of the two objects compared are the same (if the address values of the objects are compared);

Most of the common classes provided by jdk have overridden the equals method

An object. Equals (passing an object): the default comparison is the address value. The String class overrides the equals method, so whether the content of the comparison String is the same

How to get the function (important)
public int length(): returns the length of this string.
public String concat (String str): connects the specified string to the end of the string. (splicing function)
public char charAt (int index): returns the char value at the specified index.
public int indexOf (String str): returns the index in which the specified substring first appears.
public String substring (int beginIndex): returns a substring, starting from beginIndex and intercepting the string to the end of the string.
public String substring (int beginIndex, int endIndex) :
Returns a substring, intercepting the string from beginIndex to endIndex. With beginIndex and without endIndex.

public class StringDemo {
	public static void main(String[] args) {
		//Define a string
		String s = "hellojava" ;
		//public int length(): returns the length of this string
		System.out.println("length:"+s.length());
    	public String concat (String str) : Connects the specified string to the end of the string
		String s1 = "ee" ;
		String s2 = s.concat(s1) ;
		System.out.println("s2:"+s2);
		System.out.println("------------------");
		//public char charAt (int index): returns the char value at the specified index.
		System.out.println("charAt:"+s.charAt(4)); 
		//There is a string data, which needs to be traversed! (you can traverse the string by combining common for with charAt())
		//public int indexOf (String str)
		System.out.println("indexOf:"+s.indexOf("owo"));
		System.out.println("-----------------------------");
		//public String substring (int beginIndex): default truncation from the specified location to the end (return the intercepted string)
		System.out.println("substring:"+s.substring(5));//java
		//public String substring (int beginIndex, int endIndex): no package before package (no package left, no package right)
		System.out.println("substring:"+s.substring(5, 9));
		System.out.println(s.toUpperCase());
		
		
	}
}

New experience: this week, I learned common classes. Because there are many common classes, I will summarize some of them first, and then fill in the rest

Posted by rel on Fri, 19 Jun 2020 22:37:43 -0700