• 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

What is the difference between object and referece?

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For example,
String s = new String("java");
As I know, s is a referece, but I do not clearly know what is a object. Does object only exist in runtime? who can explain for it or show me a example for what they are difference? Thanks in advance!
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>For example,
>String s = new String("java");
>As I know, s is a referece, but I do not clearly >know what is a object. Does object only exist in >runtime? who can explain for it or show me a >example for what they are difference? Thanks in >advance!

a) As you have said, "s" is a reference. So, its just a small piece of memory with an address in it. That address points to the real object in memory.
b) The real object (the object created by your 'new String("java")' statement) is an instance of the class java.lang.String. That object is a chunk of memory with "java" in it along with some other class instance members (non-static variables) that hold information about the String object. It also has references to the methods of the String object. See java.lang.String in the Java 2 Platform, Standard Edition, v 1.3.1
API Specification
c) Oh, yes, the run time question. Well ... this may be tricky. Because quoted strings are pretty static in nature (the compiler already knows what you want in your string) it could be creating something at compile time. Certainly dynamically created strings are run-time. To answer this more you need someone with more insight into compilers and Java VMs.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can read about objects, heaps and other beasts in this online chapter of Inside The Java 2 Virtual Machine by Bill Venners.
Enjoy and do not forget to ask if the text is not autoexplainable.
There are even object images in the heap.
 
Kelvin Mak
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thx all!!!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic