Java learning 6: instantiation and size comparison of String objects

Instantiation and size comparison of String objects

Instantiation of String objects:

1. Two ways to obtain String objects

package Main;
import java.util.Scanner;

public class Main {
    
    
	
	public static void main(String[] args) {
    
    
		Scanner sc = new Scanner(System.in);
		
		String str1 = sc.next();
		System.out.println("str1 = " + str1);
		
		String GetChar = sc.nextLine();
		System.out.println("只是为了读取回车,处理缓冲区!");
		
		String str2 = sc.nextLine();
		System.out.println("A line String str2 = " + str2);
		
		String str3 = "Hello World";
		System.out.println("Static String str3 = " + str3);
		
		String str4 = new String("Hello World");
		System.out.println("String str4 = " + str4);
		
		sc.close();
	}

}

Knowledge combing:

First: read a string with spaces

Let me talk about getting a string from keyboard input first! The general next(); method is to read the end of the space or carriage return mark, and from the space or carriage return to the end of the keyboard type (including carriage return) will be put into the buffer, which means that Scanner scans Line reading is the same as scanf() of C language; standard input is the same, not C++ IO stream input!
How to solve this buffer problem?
Add a String type variable and read the buffer specifically! The code above can be seen.

Second: static initialization of String objects and dynamic initialization

We know that static initialization is to assign a string to a String object, but how does this assignment work? In fact, the String object (such as strObj) is used as a reference to the String class. To put it bluntly, this object is actually a number in the stack area, and its meaning is the first address of the object, that is, the reference. Originally, a static string has its own storage space, that is, a space in the heap area, and it also has its established first address. Suppose:

The process of static initialization:

Static string: "Hello World" -----> Init = 0x0000

Static initialization StrObj = "Hello World" -----> StrObj = 0x0000

The disadvantage is that both StrObj and Init point to the same address, which is very dangerous. Once a variable is garbage collected, another object is likely to be accessed again, but such access is not allowed! Is a possible cause of the null pointer exception!

The process of dynamic initialization:

String str = new String(“Hello World”);

Generate anonymous objects: Init = 0x0000 -----> Init = "Hello World"

Call the copy constructor (bottom layer): str = 0x0011 -----> str = "Hello World"

This will not cause the above-mentioned null pointer exception problem, in fact, there are other problems! When we compare objects of the String class, we compare their addresses! ! !
This is the problem of string comparison that we will briefly describe later!

Size comparison of String objects:

package Main;
import java.util.Scanner;

public class Main {
    
    
	
	public static void main(String[] args) {
    
    
		Scanner sc = new Scanner(System.in);
		
		String str1 = sc.nextLine();
		
		String str2 = "Hello World";
		
		String str3 = new String("Hello World");
		
		System.out.println(str1 == str2);
		System.out.println(str1 == str3);
		System.out.println(str2 == str3);
		
		
		sc.close();
	}

}
Let's take a look at the output first:

Insert picture description here

Why is this? According to what we have said above, we can know that String objects are actually references, so when we compare objects, we are actually comparing their addresses. Obviously, these addresses are not the same (if you have any questions, please send me a message)

Infer other things: you can't compare strings with normal size comparison operators

So what method is used?

compareTo(); Method introduction:

package Main;
import java.util.Scanner;

public class Main {
    
    
	
	public static void main(String[] args) {
    
    
		Scanner sc = new Scanner(System.in);
		
		String str1 = sc.nextLine();
		
		String str2 = "Hello World";
		
		String str3 = new String("Hello World");
		
		System.out.println(str1.compareTo(str2));
		System.out.println(str1.compareTo(str3));
		System.out.println(str2.compareTo(str3));
		
		
		sc.close();
	}

}
Let's take a look at the execution results first:

Insert picture description here

to sum up:

In fact, this object .compareTo (the object being compared
); is exactly the same as strcmp(str1, str2); in C language, which returns the difference (0/1/-1) of the two strings!
Obviously, if the two strings are the same, then return 0, the previous object is small, return -1, otherwise return 1...

So today we deeply analyze and master the differences between the different instantiation methods of String objects from the perspective of object instantiation, reference, and copy construction, as well as the methods and methods of size comparison!

Guess you like

Origin blog.csdn.net/qq_44274276/article/details/104901061