• Post Reply Bookmark Topic Watch Topic
  • New Topic

Difference between an object and a reference  RSS feed

 
Mohd Nayeemuddin
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai,
I am having two quiries please solve me out as soon as possible
The first one is that I want to know the basic difference between an object and a reference.
My second query is that when we type a command to execute a java program I mean c:>java filename
After this command is typed what does the Java Virtual Machine do will the Interpreter performs the job first then after the Just in Time compiler or first Just in Timecompiler then after the interpreter or both simultanously performs. I want to also know that the program once executed with all the output required and then if we want to execute the same code will the interpreter again performs the job or it runs prevous code. And if we make some minor changes and compile and then after again execute the code will the whole code again interpreted by the Java Virtual Machine or only that part of code which is changed.

please solve my above two quiries as soon as possible..

Regards....
Nayeem.
 
Saathvik Reddy
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reference holds the address to the object in the heap. To call the methods on object one should know the address of the object. Object is an instance of a class.
Think of a person as an object and his name as reference. if anyone wants to call him they call you with his name which is a reference.

Hope this helps!
 
Mark Dexter
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way to think about a reference (this from the book "Head First Java") is as a "remote control" to the object. The buttons on the remote correspond to the methods for the object's class. This analogy is particularly useful when thinking about inheritence and subclasses. If you have a reference of type Object, that only gives you the Object class' remote control buttons (i.e., methods for the Object class). However, if the reference is a subtype (e.g., String), then you get the remote control buttons (i.e., methods, etc.) that comes with the String class.

Both references could point to the same exact location on the heap and therefore the exact same object, but what you can do with them is limited by the type of the reference. Hope this helps. Mark Dexter
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Though Java does not deal with pointers, you can think of a reference is same as a pointer (if you are from C/C++ background).

You can even think it as a "handle" or "key" with which only you can ever get access to an object. How a filepointer to a file, a reference to an object.

HtH.
 
arulk pillai
Author
Ranch Hand
Posts: 3417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also can think

A class is a cookie cutter (or a template)
An object (is an istance of a class) is a cookie.
A reference is the name of the cookie with wich you can refer to it.


The Classes are loaded into the JVM by the "Class loaders". The very first class with the main method is specially loaded. Other classes are introduced into the JVM as they are referenced by name in a class that is already running in the JVM.

[ June 05, 2007: Message edited by: arulk pillai ]
[ June 05, 2007: Message edited by: arulk pillai ]
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mohd Nayeemuddin:

My second query is that when we type a command to execute a java program I mean c:>java filename
... I want to also know that the program once executed with all the output required and then if we want to execute the same code will the interpreter again performs the job or it runs prevous code. And if we make some minor changes and compile and then after again execute the code will the whole code again interpreted by the Java Virtual Machine or only that part of code which is changed.

Well - first of all - you don't call java filename but java classname - do you?

After finishing the program, you start it again, and ask yourself, whether previously work of the runtime will have effect on a next run.

Well - assume the first run would have some influence, the work done by the JIT-Compiler must be stored somewhere - doesn't it?
So does your classfile change its timestamp? Its size?
Do you find some other file generated by the JVM when you ran your class?
 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
As already mentioned in above posts, the Classloader has to first load the class files before the JVM can actually execute them.
And about reference and object, you can think of a reference as a pointer to the actual object, very similar to pointers in C,C++.
That is you use the reference to manipulate the actual object present o the heap(memory).
Reference -Object is analogous to
string of a kite - kite
Tv remote - Tv
So you can infer that the reference is something attached to the object(not always) and object is the actual entity under consideration.

Hope this helps
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!