This article is mainly to explain some of the basic knowledge of string data type, some messy, but are more important things, mainly refer to the information of people on the Internet. Original URL: http://dev.yesky.com/91/2309091.shtml
The main points are:
1. String is not part of the basic data type in 8, and string is an object.
Here are 8 basic data types for Java: The basic types are divided into three categories, character type (char), Boolean (Boolean), numeric (byte, short, int, long, float, double). There are two types of numeric types, integer (int, short, long) and float (float, double). There is no unsigned value type in Java, their range is fixed, and will not change with the machine hardware environment or the operating system.
The default value of the object is null, so the default value for string is also null. But it is also a unique object, has its own unique some properties.
2.new string () and new String (""), all declaring an empty string, which is to notice that the empty string is declared instead of NULL.
3.String str1= "Hello"; and string str2= new string ("Hello"), the difference:
There is no stack involved here, just a simple introduction to the concept of a constant pool. Chang (constant pool) refers to some of the data that is determined during compilation and saved in the compiled. class file, including constants for classes, methods, interfaces, and so on, including string constants. Here are two examples:
1 string s0=2 string s1=3 string s2= "kv" +4 System.out.println (s0==5 System.out.println (S0==S2);
The output here is:
true true
Explanation, Java ensures that a string constant has only one copy, and in the example, "Kvill" in S0 and S1 are string constants that are determined at compile time, so s0==s1 is true. For S2, "kv" and "ill" are constants, and when a string constant is added by multiple string constants, it is itself a string constant. So S2 is resolved to a string constant at compile time, so we can draw s0==s1==s2. However, the string created with the new string ("Kvill") is not a constant, so the string created by the new string () is not placed in the constant pool. They have their own address space.
The Intern () function in 4.String.
The constant pool that exists in the. class file is loaded by the JVM at run time and can be expanded. The Intern () method of string is a method of extending a constant pool, and when a string instance str calls the Intern () method, Java looks for a constant pool with the same Unicode string constant, if any, and returns its reference, if not, Adds a string in the constant pool that is Unicode equals STR and returns its reference.
Let's look at the example below:
1String s0="Kvill";2String s1=NewString ("Kvill");3String s2=NewString ("Kvill");4System.out.println (s0==S1); 5System.out.println ("**********");6 S1.intern ();7S2=s2.intern ();//assign a reference to "Kvill" in the constant pool to S28System.out.println (s0==S1); 9System.out.println (s0==S1.intern ()); TenSystem.out.println (S0==S2);
The result of the output is:
false false // true//true
The 5.String is immutable.
Once the instance of string is generated, it will no longer change, for example: string str= "kv" + "ill" + "+" ans "; There are 4 string constants, first "KV" and "ill" generate "Kvill" in memory, and then "Kvill" and "Generate" Kvill "in memory, and finally generated" Kvill ans ", and the address of the string to the STR, This is because the "immutable" of string produces a lot of temporary variables, which is why it is recommended to use StringBuffer, because StringBuffer can be changed.
In a comprehensive way, there is some understanding of string in Java. Mainly refer to the article at the beginning of the link that article, some things I have not touched, such as Java Virtual machine, so for some things still do not know, ready to look at this book, some people recommend this kind of books. Just have not looked, must seize the refueling to study well!
String data types in Java