• Post Reply Bookmark Topic Watch Topic
  • New Topic

what's a "java reference"  RSS feed

 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java, remote controls are called references. They store a value which the Java Virtual Machine (JVM) uses to get to your object. It sure looks and feels a lot like a pointer, and it might very well be a pointer to a pointer, or...

You Can't Know. It's an implementation detail that you, as a programmer, can't access. Don't even think about it.

Can someone describe the implementation details? A java reference looks and feels a lot like a C++ pointer. Is it something else?
[ October 19, 2006: Message edited by: sven studde ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As the quote from the story says, it depends on the implementation. I know that very early Sun JVMs used "handles" just like the original Mac OS did. A handle is a pointer to a pointer to an object. You can move the object in memory without breaking the handle by moving the object and updating that "middle pointer," so handles are convenient for simple memory-compacting garbage collectors. They were also kinda slow. Microsoft's first JVM was demonstrably faster than Sun's at the time because it used direct pointers. Of course, it had terrible heap fragmentation.

For more recent Sun JVMs the picture is much more complex: there are both handles and pointers, and there are "smart" pointers of various kinds (C++ objects that "look" like pointers.)

But the point is that all of this is transparent to the Java application. Don't worry, be happy!
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You copied this part but didn't believe it?

You Can't Know. It's an implementation detail that you, as a programmer, can't access. Don't even think about it.

Not a problem, curiosity is good. But even if the content of a reference were standard across all JVMs there is no way to dig into it or manipulate it in the language. So that's one big difference from C++.

I used to like playing with raw addresses in assembler. You could do crazy sounding things like add an ASCII character to an address to get an entry in the middle of an array. I don't seem to miss it a bit in Java.

Forced to guess, I'd venture it might be a hash into a table of memory locations. During GC the JVM moves objects around in memory, and it's good to change it once and not in every darned reference. Beyond that I'm going with "Don't even think about it."
 
Lucas Lee
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java reference is a "safe" pointer,that is to say,you can pass it like a pointer,but you can not do some operation like "pointer++" to point to other memory address.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed, a Java reference "references" a location in memory where the state of the instance in questions is stored. It certainly sounds like a pointer, and you can even use the equals sign to make other variable names point to that same memory location, put despite all of this pointing, it is not a pointer.

I find for people who are new to programming, this fact is like water off a ducks back. A newbie doesn't know what C++ can do with pointers, and they're not too anxious to learn. On the other hand, people with experience with pointers somehow wonder how we can say that a Java reference isn't similar in many way to C++ pointers.

Certainly, a reference 'references' a memory location. However, the manner in which a Java 'reference' can be used is quite limited when compared to C++ pointers, and that's a fact that no C++ programmer I've ever met would care to debate me on.

Cheers!

-Cameron McKenzie
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!