Java first Knowledge of string

Source: Internet
Author: User

Label:

The String class is the final class and cannot be inherited.

The String class represents strings. All string literals In a Java program are implemented as instances of this class.

Strings are constants, and their values cannot be changed after they are created. A string buffer supports variable strings. Because the String object is immutable, it can be shared.

The Java language provides special support for string concatenation symbols ("+") and for converting other objects to strings. string concatenation is StringBuilder implemented through the (or StringBuffer ) class and its append methods. String conversions are implemented by methods that are toString defined by the Object class and can be inherited by all classes in Java.

  Constant Pool constant:

Chang refers to some data that is determined at compile time and is saved in the compiled. class file. Constant values (final), in addition to the various basic types defined in the code (such as int, long, and so on) and the object type (String), contain symbolic references that appear as text, such as the fully qualified name of the class and interface, the name and descriptor of the field, and the name and descriptor of the method.

The virtual machine must maintain a constant pool for each mounted type. A constant pool is an ordered set of constants used by that type, including direct constants (String,integer and floating point constants), and symbolic references to other types, fields, and methods.

For a string constant, its value is in a constant pool. The Chang in the JVM exists in the form of tables in memory,

For string types, there is a fixed-length constant_string_info table for storing literal string values, note that the table stores only literal string values and does not store symbol references. When the program executes, the constant pool is stored in the method area, not the heap.

  The nature of string strings:

  String = char[] + operation (toUpperCase (), concat ()). The bottom of the string is the char[] array, which is the encapsulated class of the char[] array

Classes: Data + related operations. Char array is just data, no action

string is used to represent text, which is a series of Unicode characters.

A string is an ordered collection of Unicode characters that is used to represent text. So the string object is an ordered collection of System.Char objects that represent strings. The value of the String object is the contents of the ordered collection, and the value is immutable. The string essence is an array of characters.

string Two instantiation methods

The object is instantiated by direct assignment, by receiving the object of a string class, and instantiating the object again.

1. Create directly using "" double quotation marks;//compile-time creation

2. Create with new String ();//run-time Create

3. Create using New String ("somestring") and other overloaded constructors;

4. Create using the overloaded string connection operator "+".

  Description of the String class

1.String uses private final char value[] to store strings, and after a string object is created, it is no longer possible to modify the string contents stored in this object.

The 2.String class has a special creation method, which is created using the double quotation marks.

3.Java overloads the "+" operator for string types, and you can connect two strings directly using "+".

4. Calling the Intern () method of the string class during runtime can add objects dynamically to the string pool.

When the Intern method is called, if the pool already contains a string equal to this string object (as determined by the Equals (Object) method), the string in the pool is returned. Otherwise, this string object is added to the pool, and a reference to this string object is returned.

12345678910111213141516171819202122232425262728 publicclassTest {    publicstaticvoid main(String[] args) {        String str1 = "Hello";        String str2 = "Hello";        String str3 = newString("Hello");        String str4 = newString(str1);        System.out.println(str1 == str2); // true        System.out.println(str1 == str3); // false        System.out.println(str1 == str4); // false        System.out.println(str2 == str3); // false        System.out.println(str2 == str4); // false        System.out.println(str3 == str4); // false        str3 = str3.intern();        System.out.println(str1 == str3); // true        String str5 = "Hello""World";        String str6 = "HelloWorld";        System.out.println(str5 == str6); // true        String hello = "Hello", lo = "lo";        System.out.println(hello == "Hello"); // true        System.out.println(Other.hello == hello); // true        System.out.println(hello == ("Hel""lo")); // true    编译时创建 栈中        System.out.println(hello == ("Hel"+ lo)); // false 运行时创建 堆中        System.out.println(hello == ("Hel"+ lo).intern()); // true    }}

  

Java first Knowledge of string

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: