This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

ob1.equals(ob2) why can't I understand this ?  RSS feed

 
Rasul Patrick
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In the following code below :


how exactly this code is working and with this method
Note: As I'm a greenhorn I want a bit by bit description (I understand equals(ob2) in ob1.equals(ob2) but not the whole of it ).
EDIT: Means how is this code is working. What is o.a and o.b ? then what is a and b here ?
Is there any other way to compare ob1 and ob2 ? what am I lacking as a beginner of Java so that I am not able to understand this code ?

 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your question is rather vague. Either use should consult a Java primer, or ask a more specific question.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

So, what exactly do you not understand? The statement ob1.equals(ob2) means: call the equals method on the object that ob1 refers to, passing it ob2 as the argument.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rasul Patrick wrote:What is o.a and o.b ? then what is a and b here ?

I suspect you need to read the Java Tutorials a bit further.

However, to get you on your way, if you write:you've just defined a class called Test that contains two fields: a and b, with package-private visibility.
That means that if you create a Test object called t, you can refer to its fields using the dot notation t.a and t.b.
However, this is generally bad practise, and I really think you need to read the tutorials a bit further before you tackle something like this.

Don't worry. You'll get there.

Winston
 
Campbell Ritchie
Marshal
Posts: 55722
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually you should give those fields private access. You can find them inside the equals() method, because you are inside the class.
If you have worked out how to write methods and fields at all, there are three commonly‑quoted references about equals(): 1 2 3
 
Rasul Patrick
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rasul Patrick wrote:
In the following code below :


EDIT: A dot operator links the name of the object with the name of the instance variable. The dot operator also used to access the methods withinn and object.
So in ob1.rasul(ob2) what is linking with what and how this code exactly working with this method
how the linking process is going on ?


 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rasul Patrick wrote:
how the linking process is going on ?


The spec says that when we to "A-dot-B" that the JVM has to look at the reference value stored in A and access the B member of the object that that reference points to. Exactly how that does it is undefined. It's left up to the implementation. As an oversimplification though, we can consider it as if "A' holds the "memory address" of the object.

Also, this is wrong:
dot operator links the name of the object with the name of the instance variable


Objects don't have names. It would be more accurate to say that the dot operator links a reference value with the object that reference points to.
 
edwin moses ma
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hope the below code helps you understand the difference, it has output with code explaination below





 
Campbell Ritchie
Marshal
Posts: 55722
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Don't use // comments for anything of any length. Use /* comments */. I have changed the comments and broken them in to multiple lines for reasons explained here. Now you can actually read the code.

Unfortunately, you are making two serious errors in those comments. One is picked up in the output, and it is in lines 5‑7 of the new code block. You are mistaken that new String(...) recreates the object. That is why you are printing false in line 28. You have two different object with the same contents.
The other mistake is in line 54 where you think that the String hash code has anything to do with its memory location. Like all overridden versions, String#hashCode() returns something calculated from the state of the object, so as to fulfil this part of the Object#hashCode() general contract:-
If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
It has nothing to do with memory locations. So having the same hash code does nothing to suggest the two references point to the same object.
And nowhere does it say that you can use the hash code to prove that two references point to the same object. This is what it says in Object#hashCode():-
This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the Java™ programming language.
It does not say that occurs all the time. Nor does it say that the integer returned is the same as the memory address. It might be an offset from the first memory address in the heap. In fact, if you buy a posh 64‑bit machine with >4GiB memory, you cannot get the memory address into an int unambiguously; there will be several memory addresses which convert to the same int when you calculate them modulo 2³². It is then not possible to identify a memory location from a hash code. Not even with identityHashCode(). The only thing you can use to show two references point to the sam instance is ==.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!