2013年10月22日星期二

For java character comparison


public static void main(String[] args) {
String str1 = new String("hello");
String str2 = "hello";
if (str1 == str2) {
System.out.println("123");
}
if (str1.equals(str2)) {
System.out.println("234");
}
}

Why just print out the 234 , rather than 123 too ! Talk == and equals in the end what is the difference , I checked on the Internet , that's all exactly the same , but they are copied pasted station answers , that's very do not understand , did not say how the java code to compare , find great God guide us , be grateful !
------ Solution ---------------- ----------------------------
== comparison is the memory address value ,
equals compare string values
As to why , we recommend a look at the old black bamboo blog, is we java version of the big moderator .
http://blog.csdn.net/java2000_net
inside again devoted to this one
------ Solution ----------------------------- ---------------
== determine whether two references refer to the same instance
equals two references point to determine whether the contents of the same instance .
------ Solution ---------------------------------------- ----
String object equals method overrides the equals method in the String equals method is one of the comparison value, the comparison is == object memory address , String str1 = new String ("hello"); String str2 = "hello";
(str1 == str2 are comparing Str1 and str2 address , obviously these two are not the same address
------ Solution ----------- ---------------------------------
== compare the two object memory address, for instance, your str1 memory address is 0x00000000, str2 memory address 0x00000001. those two values ​​are different , so it returns false.
but equals is to compare two string values ​​are the same. So your code is true
------ Solution --------------------------------- -----------

these two memory addresses any different ? If you are using a new in-memory open up a place for him, then do not be new to the store Where is it ? this is not very clear ah ! reference variables stored in the heap , just want to stack objects , I think the two are the same ah !   without a new pool is created in the string , and then assign a reference to str, with the new is the direct object is created in memory
------ Solution ------- -------------------------------------
String str1 = new String ("hello");
String str2 = "hello";
if (str1 == str2) {
System.out.println ("123");
}
if (str1.equals (str2)) {
System.out.println ("234");
}
out through the new object , stored in a heap . So str1 within the heap , the heap of data references. String str2 = "hello", this form is to be stored in a stack of str2 . str2 represents a reference to the data in the stack only.

jdk description == comparisons are: two references refer to the same object. Obviously str1 and str2 point is not a reference. equals compare string values. Although references are not the same, but the same value .
------ Solution ---------------------------------------- ----
corresponding installation directory has src.zip , the next will be able to look at the source can be associated with it.

see java official instructions.
------ For reference only -------------------------------------- -

these two memory addresses What is the difference ? If you are using a new in-memory open up a place for him, then do not be new to the store where is it ? this is not quite understand ah ! reference variables stored in the heap , just want to stack objects , I think the two are the same ah !
------ For reference only -------------- -------------------------
look string constant pool , as well as stack and heap
------ For reference only ---------------------------------------


thank the great God, that I have a problem , go to know these things ? advice about ways of learning ! jdk I looked at these things and you have no ah ! saying I found the source to engage in. equals in this way is like this :


  public boolean equals(Object anObject) {
if (this == anObject) {
    return true;
}
if (anObject instanceof String) {
    String anotherString = (String)anObject;
    int n = count;
    if (n == anotherString.count) {
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;
while (n-- != 0) {
    if (v1[i++] != v2[j++])
return false;
}
return true;
    }
}
return false;
    }


This is actually the main class suffix can not be tracked into view ! source in the Notes ( own understanding ) : Compares the specified string object , the result if it is true, and the argument is not null, and the string type , represents the same sequence of strings !

pursuing big learning methods under the guidance of God ! may I ask a little more, this professional counterparts , and sure enough knowledge not !
------ For reference only ---------- -----------------------------
I found an article seems good ! http://www.iteye.com/ topic/634530
Thank you for your help, see points !

没有评论:

发表评论