Meaningless Drivel is fun!
The moose likes Beginning Java and the fly likes Printing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Printing" Watch "Printing" New topic


craig long

Joined: Jan 19, 2010
Posts: 7
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é

Joined: Nov 24, 2005
Posts: 14688

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.

[My Blog]
All roads lead to JavaRanch
Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

Using '==' on objects compares their reference, not their value. Check this page.
Alpesh Padra
Ranch Hand

Joined: Jan 10, 2010
Posts: 41
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

Joined: Apr 21, 2010
Posts: 13
you can use the .equals() operator to compare those two objects.......
Vineet Kakati

Joined: May 03, 2010
Posts: 21
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

Joined: Oct 27, 2005
Posts: 20279

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.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: Printing
It's not a secret anymore!