Equality Object equality is tested using the == operator, while value equality is tested using the .equals(Object) method. For example: String one = new String("abc"); String two = new String("abc"); String three = one; if (one != two) System.out.println("The two objects are not the same."); if (one.equals(two)) System.out.println("But they do contain the same value"); if (one == three) System.out.println("These two are the same, because they use the same reference."); The output is: The two objects are not the same. But they do contain the same value These two are the same,
Read full article from Java gotchas - OWASP
No comments:
Post a Comment