JAVA string constructor string, string pool, string content immutable

String is an object

  • Java of which only two types of data: basic types, reference types

  • Only eight basic data types a keyword, so the string is a reference type

  • Note: java as long as the string is an object


public class Demo01String {
	public static void main(String[] args) {
		//动态初始化一个数组,这就是创建了一个对象。数组就是一个对象
		int[] array1 = new int [5];
		
//		静态初始化一个数组,也是一个对象
		int[] array = {1,2,3,4,5};
		
//		字符串直接写上双引号,就是一个String类型的对象,String就是类名称
		//String为类名称 ,str为对象名,“Hello”为堆内存中的字符串对象
		String str = "Hello";  
		System.out.print(str); //Hello
		
		//直接使用的字符串常量,也照样是一个字符串对象!
		System.out.println("World");
	}
}
  • Two issues of the string :

1. String object has a corresponding class, String corresponding to

  • Why String class, no lead package can be used directly?
  • All classes will need to guide the package to use, in addition to two cases:
  • A. target class to be used, and the current class at the same package
  • B. target class to be used, located in the java.lang package (jdk / lib / src / java.base / java / lang). But that does not contain sub-packages.

2. Since the strings are objects, then why is the content directly to print it? How not a new address value out of it?

  • Println logical printing method:
    . If A is a string type, the display content directly.
    b. If the type is not a string, then converted to a string just try, and then displayed.
    The objects are generally used "+ @ + type information address value" as a string Content
public class Demo02StringQuestion {
	public static void main(String[] args) {
		String str = "Hello";
		System.out.println(str); //hello
		
		Person per = new Person();
		System.out.println(per);  //abc.Person@136432db
	}
}

Constructor string
string to create common ways 2 + 1 (two kinds of construction method, a direct assignment)

  • Two common constructor:
    public String (char [] Array): parameter is an array of characters, to create a string of characters according to the contents of the array
    public String (char [] array, int offset, int count): The character array part of the content to create a string.
    It represents an offset parameter offset index represents the initial array, starting with zero.
    Parameter count represents the number of characters drawn.
    Be careful not to out of range

    Double quotes is also a direct assignment String object:
    String = STR "the Hello";

public class Demo03StringInit {
	public static void main(String[] args) {
		char[] array = { 'H', 'e', 'l', 'l', 'o' };

		// 根据字符数组来创建字符串
		// 类名称 对象名 = new 类名称(构造参数)
		String str1 = new String(array);
		System.out.println(str1); // Hello
		
		//根据字符数组一部分来创建字符串
		String str2 = new String(array,2,3);
		System.out.println(str2); //llo
		
		//如果制定的索引或者个数超出了合理范围,那么将发生索引越界:
		//String str3 = new String(array,100,200);
		
		//直接赋值
		String str4 = "HelloWorld";
		System.out.println(str4);  //HelloWorld
		
	}
}

String pool
string is the most commonly used objects, they are often a number of duplicate strings which will appear in the program.
To conserve memory, the introduction of the "string pool" design, can be reused string.

Several pools on description string:
1. String pool is small pieces of a heap memory space is used to hold a plurality of address values of the string.
Absolute duplicate strings corresponding address will not appear 2. string pool, repeating strings have the same address value, achieve the purpose of saving memory.
3. Any direct assignment string Default double quotes are in the pool, while the new string out of the pool is not in default.

For basic types, == is a comparison of data content.
A reference to the book type, == is the same address value comparison.
If two objects, then the address value must be different, if the value of the same address, must be the same object.

public class Demo04Pool {
	public static void main(String[] args) {
		String str1 = "Hello";
		String str2 = "Hello";
		System.out.println(str1 == str2);  //true
		
		char[] array = {'H','e','l','l','o'};
		String str3 = new String(array);
		String str4 = new String(array);
		System.out.println(str3 == str4);  //false
		
		System.out.println(str1 == str3);  //false
		
	}
}

Contents of the string immutable

Strings are constant; their values can not be changed after creation.
Contents of the string does not change, whenever you feel the string contents have changed, must create a new string, change the address, point to the new space.

public class Demo05Eternal {
	public static void main(String[] args) {

		// 字符串赋值变化,是字符串地址值在变,而不是字符串在变
		String str = "Hello";
		System.out.println(str); // Hello

		str = "World";
		System.out.println(str); // World

		//两个字符串相加,会在堆内存中创建第三个字符串
		String str2 = "Love";  
		str2 += "Java";
		System.out.println(str2); // LoveJava
		//str2得到新的地址LoveJava的地址

	}
}
Published 52 original articles · won praise 6 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_43472877/article/details/104103594