Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Printing

 
craig long
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I submitted the wrong code last time. Why won't this print "objects are equal" or "objects are unequal properly?" It always prints the second println regardless of whether spot1 and spot2 are the same or not.



Thanks for any help.
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I submitted the wrong code last time.

No need to make a new thread. You can press the Edit button () to edit your post. I have removed the other one.
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using '==' on objects compares their reference, not their value. Check this page.
 
Alpesh Padra
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when you create object using new operator. application will create new object in JVM.

if you just copy reference to another object then it just copy reference not creating new object.
 
Arjun Bajaj
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can use the .equals() operator to compare those two objects.......
 
Vineet Kakati
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all your existing 'if' condition means that you are basically trying to compare whether spot1 and spot2 point to same object which they do not ,so they are displayed to be not equal;

Secondly if you want execute the println statement on basis of equality of radius,change your 'if' condition to


Also make variable radius non-static
 
Rob Spoor
Sheriff
Pie
Posts: 20381
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vineet Kakati wrote:Also make variable radius non-static

Definitely. At the moment all circles have the same radius - that of the last created circle. So spot1.radius == spot2.radius always returns true.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic