Solved

Are two variables referring to the same object?

Posted on 2014-07-25
5
117 Views
Last Modified: 2014-07-25
I am debugging some Groovy code for a website (that I did not write) and have hit an issue where I create an object A in controller in one part of the flow and set a variable within it (read it back and its correct).

I then pick up what I had understood to the the same object in a different controller. But the variable is no longer set.

Either my assumption that the object A in the first controller is the same object A as picked up the the second controller is wrong or something has modified the value en-route.

So, what might be a very basic question (and I have have a horrible feeling that it points to some fundamental misunderstanding on my part of how Groovy/Java works - so please be gentle) :
How can I tell if the object A in controller 1 is the same as the object A in controller 2 (by the same I mean point to the same object, not that they are equivalent).
0
Comment
Question by:monoceros
  • 2
  • 2
5 Comments
 
LVL 33

Expert Comment

by:ste5an
ID: 40219226
Java: == tests for whether two variables point to the same memory location (identity). Per default this does also equals(). But equals() can and often is overwritten (also is hasCode()) to test for value equivalence only.

Groovy: == tests for value equivalence. Use .is for testing for identity.

One question is: How is the object passed from controller 1 to controller 2? It sounds like it is only a shallow copy.
0
 
LVL 1

Author Comment

by:monoceros
ID: 40219252
Ste5an, thanks - reading your answer made me realise that I was not completely clear in my question: I do not have access to both variables at the same place & time so I cannot test for identity with .is - so what I really need is something that can be generated to a log file that allows me to see that both variables refer to the same object.

To answer your question: I am still trying to figure it out! There is much use of re-entrant code driven by state and its quite hard to map out all the possible paths (I currently have the horrible feeling that the objects are actually re-created in error later in the flow - which would explain what I am seeing).
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 40219270
If the object doesn't override toString, you can simply print the reference to the log file and later ascertain that the references are the same (or not)
0
 
LVL 1

Author Closing Comment

by:monoceros
ID: 40219352
How could I manage this if toString has been overridden?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 40219446
You can't really
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Eclipse Neon and jdk 1.8.0 11 126
Cipher Configuration on Apache HTTPD 4 50
Java DateChooser? 3 29
javap bin 2 25
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now