Solved

Are two variables referring to the same object?

Posted on 2014-07-25
5
120 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

813 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

13 Experts available now in Live!

Get 1:1 Help Now