A detailed description of the String class in Java
Examples of classes of embodiment 1.String
- Direct assignment
String str = "hello world"; //str是一个对象的引用,所以“hello world”应该保存在堆内存中
System.out.println(str);
- Conventional method (constructor)
//因为String是一个类,所以存在构造方法
String str = new String("hello world"); //通过构造方法赋值
System.out.println(str);
2. String string comparison
- If there are two int type variables, by determining whether the same
==
determination
int x = 10;
int y = 10;
System.out.println(x==y); //true
- If two String comparison, "==" comparison, the comparison is a reference to the type of address
String str = "hello world";
String str1 = "hello world";
String str2 = new String("hello world");
System.out.println(str == str1); //true
System.out.println(str2 == str1); //false
- The method of comparing strings with content equals
String str1 = "hello world";
String str2 = new String("hello world");
System.out.println(str1.equals(str2)); //true
to sum up:
- Value "==" operator is used to compare two variables are equal, the basic types, that is, comparison of the numerical size ; a reference to a data type, the comparison is saved in a Java String class described in detail and will not compare the contents
- When comparing the contents of the string, the String class provides equals () method, a case-sensitive; use ** equalsIngoreCase () ** case insensitive
- When the direct assignment, the string is determined whether there is an object in the pool, if any, to the direct reference to an object pool
- Pooling the equivalent of a one-dimensional array
- If you are using the constructor, then open up a new space
3. String anonymous objects (not from the name of the object)
All string constants" "
anonymous objects, are the String class
String str1= "hello";
String str = new String("hello");
System.out.println(str1.equals(str));
System.out.println("hello".equals(str));
- "String str =" hello ";" is an anonymous String class object is provided with a name on nature, but also to preserve the heap memory
- Any string constants are anonymous String object, the object will never be null
4. Examples of the difference
- Direct assignment
String str = "hello";
String str2 = "hello";
Stirng str1 = "hello";
System.out.println(str == str1); //true
System.out.println(str == str2); //true
System.out.println(str1 == str2); //true
//str, str1 , str2三个地址指向同一块空间
In fact, the underlying JVM will automatically maintain a string constant pool (array of objects) . If a direct assignment to instantiate an object, first find out whether there is a specified content object in the object pool, and if so, direct references; on the contrary, the newly created; in fact, is the use of design patterns in the Flyweight
- The method of construction using
String str = new String("hello");
String str1 = "hello";
System.out.println(str == str1); //false
- Using construction methods will open up two heap space
- First put the anonymous object "hello", is a string constant to open up, and then a new real object; points out that the new true object, that memory will become an anonymous object space junk
- Instantiated object and not saved in the memory pool
//入池操作
//把String对象入池(如果池中有,引用指向它;如果没有,入池加指向)
String str = "hello";
String str1 = new String("hello").intern();
System.out.println(str == str1); //true
The string can not be changed
- Once defined string, immutable
- All language is a fixed length of the string to achieve the greatest flaw, all character array, the array. When defining a string constant, its contents immutable
String str = "hello";
str = str +" world";
str += "!!!";
System.out.println(str); //hello world!!!
//4块垃圾空间
//堆中常量的值无法修改,但是栈的指向一直发生变化
Change the above string, the string is subject to change, instead of a string constants
illustrated as follows: