When comparing Strings and StringBuffers, which of the following statements evaluate correctly and return true: a) new String("IBMVAJava") == new String("IBMVAJava") b) new String("IBMVAJava").toString().equals(new String("IBMVAJava")) c) new StringBuffer("IBMVAJava") == (new StringBuffer("IBMVAJava")) d) new StringBuffer("IBMVAJava").equals(new StringBuffer("IBMVAJava"))
Can any one explain the answer here? Thanks!
zhaobin74
Greenhorn
Joined: Sep 26, 2000
Posts: 6
posted
0
b) is right. equals() compare the content of Strings. Since "new" will create new objects and "==" compares 2 reference to see it they point to the same object,a) and c) are wrong. I am not very clear why d) is wrong.
I eapect to pass SCJP2 test
Kishan Kumar
Ranch Hand
Joined: Sep 26, 2000
Posts: 130
posted
0
d is wrong because the StringBuffer class does not override the Object's equals method and hence only object references are compared as specified in the Object's equals method. Hope this helps! ------------------ Regards, V. Kishan Kumar