• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Objects and refrences

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all ,
Just wanted to clarify the answer for the following question :
Given that Thing is a class, how many objects and reference variables are created by the following code?
Thing item, stuff;
item = new Object();
Thing entity = new Object();
Answer : Refrences : 3
Objects : 2
Thanks in advance ,
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shallu Shankar:
Hi all ,
Just wanted to clarify the answer for the following question :
Given that Thing is a class, how many objects and reference variables are created by the following code?
Thing item, stuff;
item = new Object();
Thing entity = new Object();
Answer : Refrences : 3
Objects : 2
Thanks in advance ,


Hi Shallu,
First, let's assuming that we are not looking the code syntactically (i.e. ignore compilation error and concentrate on the ref and object only).
Next, other people might have different opinion; but I believe that you have 2 references, 2 objects, and just a variable.
Here is what I believe. The first line of your snippet of code only declares two variables of type Thing, namely item, and stuff. At this point, each of these two variables 'can' store a reference to an object of type Thing, if and when you create one.
The next two statements, you initialize variables 'item', and 'entity' with an object each using keyword 'new' (Assuming casting is properly done). Each object now occupies a memory block of their own. The address of each memory block is assigned to the variable 'item' and 'entity', respectively. Meanwhile 'stuff' still does not contain such an address yet since there is no object to point to. In other words, it can store a reference/address...but since there is no object, therefore no reference. Opinion, anybody?
Regards,
Lam


[This message has been edited by Lam Thai (edited April 23, 2001).]
 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


originally posted by lam
Next, other people might have different opinion; but I believe that you have 2 references, 2 objects, and just a variable.


Hi, lam, i agreed with your reasoning except some concept. i am not sure about the reason of your discrimination between the reference and variable here.
fist, i think 'item','stuff'and 'entity' are all legal variable, their type is Thing.
second,'item' and 'entity' both hold a valid refencnce(Assuming casting is properly done).
third, here we diverge, i think varible 'stuff' still hold a reference, a special reference, null reference(more exactlly, it is of a special type, null type, while the null reference can always be cast to any reference type), through i don't know whether a null reference is a valid reference, i think the variable 'stuff' is a valid reference variable.

we could suppose that any refencnce variable automatically hold a null reference whenever the variable declaration statement execuated.
that's all my own opinion, pls correct me if i'm wrong.
Regards.
James

[This message has been edited by James Du (edited April 24, 2001).]
 
Lam Thai
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi James,
First allow me to re-state what I read from your comment to make sure that I understand your viewpoint.
Per your comment, you said there are 3 variables of type Thing, namely 'item', 'stuff', and 'identity'. Two of them, 'item' and 'identity', through the initializion with the 'new' keyword, each has a reference pointing to a Thing object in heap. Meanwhile 'stuff' has no object but does have a reference that points to null.

Now let's look at the following snippet of code:
Thing a, b, c, d, e;
a = (Thing) new Object();
b = (Thing) new Object();
c = b;
Per your reasoning, we now have 2 objects, 5 references. The reference for a points to an object of type Thing. The two references for c and b both point to another object of type Thing. The two references for d and e point to null.
Maybe we are only semantically different... But let's do away with the term variable and see if we can agree with the following statements:
- Symbol a holds a reference (i.e a physical address) of the memory block for an object of type Thing in heap.
- Symbol b holds another reference (i.e. a physical address) of another memmory block for an object of type Thing in heap.
- Symbol c holds the same reference as that of b.
- Symbols d and e hold nothing... They are empty (i.e null, no physical address to refer to)
In short, among the 5 symbols, there are only three that hold 'TWO PHYSICAL ADDRESSES THAT REFER TO' two objects. One physical address that refers to an object of type Thing is held by a. Another is shared by b and c.
Regards,
Lam
 
James Du
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lam,
I think we could agree on what actually JVM do as to the given snippet of code, the divergence lies in the different explanation to the same understanding.
Let me try to begin from your comment:


- Symbols d and e hold nothing... They are empty (i.e null, no physical address to refer to)
In short, among the 5 symbols, there are only three that hold 'TWO PHYSICAL ADDRESSES THAT REFER TO' two objects.


From my viewpoint, the 'nothing' that d and e hold sould be regarded as a reference in the technical sense, deduced from the same logic that 0 is a valid integer through 'i hava 0 apple' in the human language seems senseless.
 
reply
    Bookmark Topic Watch Topic
  • New Topic