Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt about equals vs ==

 
NarendraMohan Prasad
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've written the following code for the test
I want reason for the difference between ==
and .equals.
a
why == is giving true for the "Test1"


(Changed title to be meaningful)
[ June 06, 2007: Message edited by: Barry Gaunt ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Narendra,

This sort of question has been discussed hundreds of times. If you use
the search facility of JavaRanch, you will find so many interesting discussion
on this.

Hint: When you use new operator to create String object, object is created
at run time. Without using "new" you call compile time created String object.
Objects created with "new", gives false result when you use == to compare
them even if the content is same. Because == compares references of the
objects.


Thanks,
 
ram shah
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://www.javaranch.com/journal/200409/Journal200409.jsp#a1

pls go through the link mentioned above......your doubt would be clear.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic