File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question on string/stringbuffer

 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kishan Kumar
Ranch Hand
Posts: 130
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Vivek Nambiar
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please go though this link, it helps u in understanding it further...
http://www.javaranch.com/ubb/Forum24/HTML/000530.html
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic