• Post Reply Bookmark Topic Watch Topic
  • New Topic

two String object having same content which is created using new keyword.is that same object ?  RSS feed

 
Mukund A. Kumar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


in above code reference variable a and b refers to same object ? if yes then how ?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mukund A. Kumar wrote:

in above code reference variable a and b refers to same object ? if yes then how ?



No. Reference a points to the object created at line 1, and reference b points to the object created at line 2. These are two different objects -- although they are equal objects.

Henry
 
Mukund A. Kumar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Henry Wong: what is the meaning of "although they are equal objects. "
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mukund A. Kumar wrote:@Henry Wong: what is the meaning of "although they are equal objects. "


Calling the equals() method, using one instance, and passing the other, will report that they are equal.... or perhaps I should say "although they have equal values" in the previous response instead?

Henry
 
Mukund A. Kumar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But equals() method of Object return true when two reference variable refer to same object.but here


reference variable a and b both refer different object
then why a.equals(b) return "true" ?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mukund A. Kumar wrote:But equals() method of Object return true when two reference variable refer to same object.but here


reference variable a and b both refer different object
then why a.equals(b) return "true" ?



The String class, like many of the classes that represents data, in the core Java library, overrides the equals() method, to report equality based on value.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!