4 Replies - 655 Views - Last Post: 11 October 2010 - 01:13 PM Rate Topic: -----

#1 giggly kisses  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 391
  • Joined: 29-March 09

comparing objects

Posted 11 October 2010 - 12:14 PM

I'm having a little bit of trouble understanding how this method that compares two objects work. Here is the method:

	public boolean equals(Stock object2) {
		boolean status;
		
		if(symbol.equals(object2.symbol) &&
				sharePrice == object2.sharePrice)
			status = true;
		else
			status = false;
		return status;
	}



Stock is the class, symbol and sharePrice are fields in the class. What's really confusing me is the symbol.equals(object2.symbol). Is this calling the equals method above or is it using an already written equals method that is part of the API? Also, why doesn't he use the equals method to compare sharePrice? I've been trying to wrap my brain around this for a good hour now, if anyone can shed some light on this it would be greatly appreciated.

EDIT: symbol is a string and sharePrice is a double, sorry forgot to mention this.

This post has been edited by giggly kisses: 11 October 2010 - 12:19 PM


Is This A Good Question/Topic? 0
  • +

Replies To: comparing objects

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2792
  • View blog
  • Posts: 11,827
  • Joined: 20-September 08

Re: comparing objects

Posted 11 October 2010 - 12:45 PM

Quote

or is it using an already written equals method that is part of the API?


It's using String.equals. All objects have an equals method
Was This Post Helpful? 1
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4364
  • View blog
  • Posts: 12,182
  • Joined: 18-April 07

Re: comparing objects

Posted 11 October 2010 - 12:52 PM

All objects in Java have certain methods defined for it. One of these methods is "equals()". It is used to determine if two objects are the same... that is, the same object itself. Strings in Java are only stored once. When I define a string variable with "hello" it is only ever defined once. If I create another string variable and give it the value "hello" they will be equal because they are in fact stored as the sequence "hello" once. Strings are said to be "interned". This isn't two separate string variables, think of them as pointing to the same value. When you reassign the first variable to "hello world" it creates a completely new string (because strings are immutable... aka can't be changed in place) and the second variable is left pointing at "hello" at which point the two strings are now different.

This is why you should always use the object's equals() method to compare string values. Now this is for strings. Primitive ints, doubles etc are mutable. This is why you can use == with the double. equals() is for comparing if two objects are the same (which two strings with the same value are) and == is for comparing values themselves.

So the code here is simply comparing the symbol string of object2 against the symbol string of the local class. Are they the same object? Then it is comparing the sharePrice of the object passed in against the local sharePrice value (value comparison).

If they are equal, status is set to true and returned. Of course if the object's strings are pointing to the same value and their sharePrice "value" are the same, they are the same object as far as this method is concerned.

Hope I did a good enough job to explain this. :)
Was This Post Helpful? 2
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2792
  • View blog
  • Posts: 11,827
  • Joined: 20-September 08

Re: comparing objects

Posted 11 October 2010 - 01:01 PM

It can be simplified to

    public boolean equals(Stock object2) {
        return symbol.equals(object2.symbol) &&
        (sharePrice == object2.sharePrice);
    }


This post has been edited by g00se: 11 October 2010 - 01:22 PM
Reason for edit:: spelling

Was This Post Helpful? 0
  • +
  • -

#5 giggly kisses  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 391
  • Joined: 29-March 09

Re: comparing objects

Posted 11 October 2010 - 01:13 PM

Awesome thanks guys, for some reason I thought symbol.equals was calling the equals method that was written above. Confused the heck out of me. Thanks for the replies!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1