Enum <-> String comparison in JAVA -


i want compare string enum. know how comparison don't understand why. following example shows issue:

enum foo{     test1 }  string likeenumtest1 = "test1"; system.out.println("is enum equal string?: " + likeenumtest1.equals(foo.test1)); system.out.println("is enum.tostring() equal string?: " + likeenumtest1.equals(foo.test1.tostring())); system.out.println("value of enum '" + foo.test1 + "' , value of string '" + likeenumtest1+"'"); 

the output is:

is enum equal string?: false
enum.tostring() equal string?: true
value of enum 'test1' , value of string 'test1'

i understand enum.tostring() called implicit when using in system.println(), don't understand enum uses value when compared equals() in second line. foo.test1 used integer or else, java internally?

the string#equals(object) method first checks if passed argument instance of string or not. here's snippet of source code:

public boolean equals(object anobject) {     if (this == anobject) {         return true;     }     if (anobject instanceof string) {         // code [...]     }     return false; } 

since foo.test1 not instance of string, returns false.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -