Problems in programming languages many times we seem to understand, actually do not understand, because many times do not see more situations, thus lack of these can not see the situation of cognition and interpretation.
Today mark is the difference between a string and a new string (). It's actually very simple.
string S1 = new String ("string")//This is an object that is stored in the heap
String s2 = "string"//This is a string constant stored in a constant pool, which is the method area
String s3 = "string"//....
These are the two biggest differences. each object has its own memory address, while string constants may have different names, but as long as the values of the constants are the same, their memory addresses are the same memory address in the constant pool, which is equivalent.
So s2 = = S3 is true,s1 = = S2 is false, and s1.equals (S2) is true.
"= =" means the same thing (e.g. objects, constants, etc.)
". Equals ()", which represents the same value as the variable. That is, the hash value of the comparison variable
Note that the constant pool is for a Java process, so no matter where the constant is: in the file A.java, File B.java Well, no matter what the variable is a type: private or static, as long as these files are running in a Java process and contain the same worth of string constants (a larger range is said to be constant), they are equal (= =), All point to the same memory address. (Of course, a new string constant will open up a new address in the constant pool)
Quote in-depth understanding of the Java Virtual machine: "The runtime Constant pool is part of the method area." class file In addition to the class version, fields, methods, interfaces, and other descriptive information, there is a constant pool (Constant pool Table), for the compilation period generated by a variety of literal and symbolic references, which will be loaded in the class load method to go to the run of the constant pool storage. ”
Code test: Not homogeneous, the same worth of different variable names in different files the string constant
Public classApp {Private StaticString astring = "abc"; Public Static voidMain (string[] args) {String astring= "ABC"; String bstring=NewString ("ABC"); String cString= "ABC"; System.out.println (astring==NewT (). bstring); System.out.println (astring==t.astring); System.out.println (astring==app.astring); System.out.println (astring==cString); System.out.println (astring==bstring); System.out.println (Astring.equals (bstring)); }}classT {StaticString astring = "abc"; String bstring= "ABC";}
Program output:
true true true true false
True
One thing to consider is: Is there a difference in efficiency between "= =" and "Equals" for constants? The answer is no, test the code:
Public classApp {Private StaticString astring = "abc"; Public Static voidMain (string[] args) {String astring= "ABC"; String bstring=NewString ("ABC"); String cString= "ABC"; Longctimes = 1000 * 1000 * 1000 * 1000 * 1000; LongTime1 =System.currenttimemillis (); for(Longi = 0; i < ctimes; i++) { if(astring = =cString) { } } LongTime2 =System.currenttimemillis (); for(Longi = 0; i < ctimes; i++) { if(Astring.equals (cString)) {}}LongTime3 =System.currenttimemillis (); System.out.println ("= = time spent =" + (Time2-time1)); System.out.println ("equals time Spent = =" + (Time3-time2)); }}
The output is not different, the reason is actually very simple, we go to see the source of equals :
Public Booleanequals (Object anobject) {if( This==anobject) { return true; } if(AnObjectinstanceofString) {String anotherstring=(String) AnObject; intn =value.length; if(n = =anotherString.value.length) {CharV1[] =value; CharV2[] =Anotherstring.value; inti = 0; while(n--! = 0) { if(V1[i]! =V2[i])return false; I++; } return true; } } return false; }
That's clear!! In fact this = = anobject and = = There is no difference.
Differences between string direct copy and new string creation objects in Java and differences and efficiencies between equals and = =