jQuery in Action, 2nd edition*
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Printing" Watch "Printing" New topic
Author

Printing

craig long
Greenhorn

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é
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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é
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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
Greenhorn

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

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
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
Don't get me started about those stupid light bulbs.
 
subject: Printing
 
Similar Threads
Programing challenge
Question about packages.
Circle program errors
Help with Circle Class :)
Class get and set explanation