references : Java Glossary

*0-9ABCDEFGHIJKLMNOPQRSTUVWXYZ (all)

references
Safe pointers, pointers with airbags. You can’t do arithmetic on them. They always point to the beginning of objects. It is impossible to create an invalid one. They automatically dereference as appropriate. They always point to an object of the type advertised.

The advantage of a JVM (Java Virtual Machine) that implements Java references as pointers (direct machine addresses) is speed.

The advantage of a JVM that implements Java references as handles (pointers to pointers) is ease of shuffling objects around in RAM (Random Access Memory) to reduce memory fragmentation. Handles allow the thing pointed to be moved and all references are thereby automatically forwarded to the new location. You only need to update one place when the location of the object changes.

Java hides the details of how the references work from the programmer. The JVM automatically dereferences one or two levels of pointer indirection as needed. Your program cannot even figure out how references are implemented.

I have come up with an analogy to help you understand the behaviour of references, final references and Objects. I suggest you first read the right hand column to understand the rules of this strange tribe. Then with that analogy under your belt, tackle the left hand column. Every sentence should seem completely obvious once you understand the difference between references and objects.

I hope this analogy may prove useful for people who have no machine language experience to guide their intuition. The analogy becomes so bizarre, it should help someone familiar with the inner workings of a CPU (Central Processing Unit) to appreciate just how mysterious OO (Object Oriented) programming concepts are to someone without that background knowledge. This is quite off the wall. It was designed partly to make old timers realise just how complicated this matter is and hence to be patient with newbies. After you understand pointers and references it all seems so simple and obvious, but it is just as convoluted as this whacky analogy. Stop reading it you feel yourself getting even more confused.

Object/reference male/female analogy
reference loving petite monogamous female
object loved big polygamous male
Both references and objects live inside the virtual RAM of a single program running in a single computer. They cease to exist when the program that created them terminates. The males and females of this mythical tribe live on an island. They never leave the island. They never receive visitors from other islands. They all die when the island sinks back into the ocean.
References point to objects. Females love males. They can love from any distance. Females always know where their male love objects live.
A reference points to but single object at any one time. A female loves but a single male at any one time.
A object can be pointed to by many references at once. A male can be loved by many females at once.
An object does not point back to its references. A male does not love his female admirers.
A reference must at all times either be null, or point to a valid object. Females cannot ever love imaginary or dead males, or anything but a living male, though they may love no one.
There are never more objects than references in the universe. There are never more males than females in the universe.
References have names; objects do not. Females have names; males do not. To avoid running out of female names, sometimes they are given numbered names like Jane[0], Jane[1],… Sometimes women are given names like Jane.Mary.Judy which means Jane loves a male. Mary piggy backs on him. Mary loves a male. Judy piggy backs on him. That’s the Judy I mean. There may be other Judy’s with a different lineage. Males don’t have names. Females know them only by the latitude and longitude of where they live.
If you want to access an object, you have to get at it via a reference. If you want to talk to a male to ask it a question or tell it a new fact, you must relay your message through one of his female lovers.
Objects contain data; references point to objects. Males remember facts about the world; females do not. A female does nothing but pass messages to her male lover. There is only one fact a female remembers — which male, if any, does she love.
A final reference always points to the same object its entire life. The fields of the object may change, but the reference always points to the same object. A married female loves the same male her entire life. The man may change character, but she always loves the same man.
A non final reference may point to different objects over its lifetime. An unmarried female may love many different men over her life, but never more than one at a time.
A reference may be null. This has no effect on its longevity. A female may elect to love no man for periods of her life, or her entire life. This has no effect on her longevity.
There is no such thing as a null object. Since males never love females, it make no sense to talk about a non-loving male.
If an object has no references pointing to it, it will eventually be garbage collected. If ever a man has no female lovers, he immediately dies of loneliness. However, it may take a while to recycle the corpse.
The only way an object can acquire new references to it is by copying the value of one existing reference to another. The only way a male acquires new lovers is by females talking among themselves about him.
The initial reference to an object is created by the new operator. When a male is born, his mother is his first lover.
If an object has no references pointing to it, there is no possible way for a reference ever to point to it again. It can never be used again. The object is for all practical purposes dead. If ever a man has no female lovers, he has no chance ever again of finding one, since new lovers can only find out about him from existing lovers. No one can ever communicate with him again, since all communication must be via one of his lovers. All of the females have forgotten him. For all practical purposes he is dead.
If a reference is pointing to an object, that object cannot die. If a female loves a male, it is impossible for that male to die.
If a reference dies (goes out of scope), the object it is pointing to will also die, unless there is at least one other reference pointing to it. If a female dies, her male lover will die with her, unless, of course, he has other female lovers to keep him alive.
It is possible for an Object to outlive a reference to it, but only if it has other references to it. It is possible for a male to outlive one of its female lovers, but only if he has some other living female lovers.
It is possible for a reference to outlive one of the objects it points to, but only if it becomes null, or points to some new object. It is possible for a female to outlive one of her male lovers, but only if she elects to later love no male or to love some different male.
References live on the stack, or inside objects. Objects live on the heap. The alpha females live in a tall apartment building. Females of the same generation all live on the same floor. The oldest females live in the highest stories. The alpha females are so status conscious that the floors are sloped so that the oldest female can live a little higher than the next oldest. The lower status females live piggybacked on males. Males live on a huge compost heap.
Objects and references never move. This is mild lie. They might move, but the JVM hides that fact from the programmer. Both males and females never move their entire lives from the spot they were born, well, that’s how it looks if you don’t stare too closely.
When an object dies, all references inside it die with it. This of course can trigger a chain reaction of deaths of more objects and references. The process is instantaneous. Nothing has to be done to each object to kill it. When a male dies, all females piggybacking on him die with him. These female deaths can trigger a wave of male deaths which in turn could trigger more piggybacking female deaths in an instantaneous Walt Disney ping pong ball fission explosion.
It is possible for a reference to point to the object in which it is embedded. It is possible for a female to love the male she is piggybacked on.
Embedded references that point circularly back the object in which they are embedded are not useful in keeping the object alive. An object needs outside references to it. Piggy backed females that love their carrier male are not sufficient to keep that male alive. The male needs at least one outside admirer.
Circular references are not sufficient to keep a group of objects alive. you need at least one outside reference into the group. If you had two males with piggybacked females that loved the other male, that would not be sufficient to keep the pair of males alive. You need the love of at least one outside female to one of the two males.
A simple mark/sweep garbage collection algorithm works by recursively chasing references on the stack and marking all reachable objects as live. Everything else is considered dead and that space can be recycled en masse. The undertaker works by going to the apartment building where the alpha females live. He asks each female whom she loves. He puts a red daub on males that somebody loves. He puts a green daub on each male, after he has finished asking all the piggybacked females on the loved male whom she loves. Note, he does not bother asking the piggybacked females on unpainted males whom they love. Their love does not count since those females are effectively dead too. The daubs help him avoid repeating work. When he is done some of the males will have both a red and green daub and some will have no paint at all. He then prepares a mass grave and shoves all the unpainted males into it. Note that he never touches any of the unpainted males, even once, at any time in the whole process.
A reference can only point to an object of the corresponding class or subclass. Every male and female belongs to a clan, such as the Dog or Bear clan. Some also belong to a subclan, such as the Dalmatian. Every clan and subclan has a totem animal. There are strict miscegnation rules. A female of the Dog clan can only love a male of the Dog clan. A female of the Dog clan can love a male of the Dalmatian subclan because all Dalmatians are also Dogs. However, a female of the Dalmatian subclan cannot love a male of the Dog clan, unless he too is also a Dalmatian.
You can’t do arithmetic on references. You can change the value of a reference only by assigning it to the value of some other reference, or by using the new operator. There is no way you can use arithmetic to calculate a female’s chosen lover. The only way she can learn about a potential lover is from some other female who already loves him, or by giving birth to him.
When objects are created with the new operator, by default, all their references are automatically initialised to null. When males are born, they come into the world complete with any piggybacking females already attached. These females start out life loving no one. Males never gain or lose any piggybacking females after birth. All male of the same subclan have the same number of piggybacking females.
Objects of a class have a fixed number of data fields and a fixed number of references. All males of the Dog clan have the same number of piggybacking females and those females all have the same set of given names! All males of the Dog clan memorise the exact same set of facts. Members of the Dalmatian clan have a few extra piggybacking females. Again all Dalmatian males have an identical number of piggybacking females and those females all have the same set of given names.
All objects come into being via the new operator. All males are born.
Local/stack references come into being when a method starts executing and those references go out of scope and disappear when it exits. In the beginning God created Eve. That male fellow came later. God periodically creates a new generation of alpha females to serve Her purpose. Entire generations of alpha females die at once, when their purpose is complete. Ironically great great grandmothers outlive granddaughters. Grandmother is just a figure of speech. Grandmothers don’t give birth to the next generation of alpha females. God creates them. Eve is the last female to die.
Embedded reference come into being when the object they are embedded in is created. Piggybacking females are born at the same time as the male they piggyback on.
A reference always points to an object as a whole i.e. to the start of the object, not to some field inside the object. A female always loves her male as a whole, not just his big penis.

This page is posted
on the web at:

http://mindprod.com/jgloss/reference.html

Optional Replicator mirror
of mindprod.com
on local hard disk J:

J:\mindprod\jgloss\reference.html
logo
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.

IP:[65.110.21.43]
Your face IP:[54.157.19.94]
You are visitor number