First acquainted with the Java language-String class and the conversion between strings and characters and character arrays

First acquainted with the Java language-String class and the conversion between strings and characters and character arrays

1. String class
String class is an encapsulation class related to strings.
1. What is a string?
In C language: a string of characters enclosed by ""; ending with'\0' at the end.
There is no such thing as'\0' ending in Java.
2. Where is the storage location?
The string constant pool in the heap, and each string is unique in the constant pool.
3. How to declare a string?

String str="hahahaha";
/
String str1=new String("hahahaha");

char[]c={
    
    'a','b','c'};
String str2=new String(c);

(Mainly use the above three methods to declare a string.)
4. Can the string be changed?
No, if you have to change it, you must use reflection to solve it.


The following introduces two string-related questions:
1. Point out the results of the following programs ()

public class Example{
    
    
    String str = new String("good");
    char[ ] ch = {
    
     'a' , 'b' , 'c' };
    public static void main(String args[]){
    
    
        Example ex = new Example();
        ex.change(ex.str,ex.ch);
        System.out.print(ex.str + " and ");
        System.out.print(ex.ch);
    }
    public void change(String str,char ch[ ]){
    
    
        str = "test ok";
        ch[0] = 'g';
    }
}

Assignment content
A.good and abc
B.good and gbc
C.test ok and abc
D.test ok and gbc
At first glance, choose D, but the correct answer chooses B. Why?
Insert picture description here
Because str in Change is a local variable, when passing parameters, it just changes the point of str to point to the object pointed to by str of ex. When changing, it changes the point of local variable str to point to "text ok", and The point of str of ex has not changed. So the answer is B.
2. What content will be output by the following code: ()

public class SystemUtil{
    
    
	public static boolean isAdmin(String userId){
    
    
		return userId.toLowerCase()=="admin";
	}
	public static void main(String[] args){
    
    
		System.out.println(isAdmin("Admin"));
	}
}

Job content
A.true
B.false
C.1
D. Compilation error
Choose B for the correct answer.
The toLowerCase method of the String class changes all uppercase letters in the string to lowercase letters and returns a new object, so the address of the new object is different from "admin". The latter is in the string constant pool, while the former is not. String constant pool, "==" is the comparison value, obviously the address value is not the same, so it is false.
2. Conversion between string and character and character array
1. String to character:

String str="hahahaha";
char c=str.charAt(0);//即根据下标转换

2. String to character array:

String str="hahahaha";
char[]c=str.toCharArray();

3. The character array becomes a string:

char[]c={
    
    'a','b','c'};
String str=new String(c);
char[]c={
    
    'a','b','c'};
String str=String.valueOf(c);
String str2=String.copyValueOf(c);

Go on firmly on the way to learn Java, and come on together!

Guess you like

Origin blog.csdn.net/qq_45841205/article/details/112826304