Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Are two variables referring to the same object?

Posted on 2014-07-25
5
Medium Priority
?
138 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 35

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 1500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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…
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

722 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