Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pointers

 
Sasha Hernandez
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the significance of the fact the Java does not have any pointers?
 
Marilyn de Queiroz
Sheriff
Posts: 9063
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java does not allow you to manipulate pointers like C/C++ does. This saves developers a lot of grief (and causes some frustration among some developers).
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a very common misconception. Java does have pointers. Every reference to an object in Java is through a pointer. In this code snipper
Object o = new Object();
o is a pointer.
What Java doesn't have is pointer arithmetic like C or C++, or any kind of equivalence between pointers and numerical addresses. A Java pointer can point to an object or null, not to an arbitrary location in memory.
By removing most of the properties of C/C++ pointers, Java's pointers lost the ability to cause segmentation faults and to corrupt data in memory. You can't have a "stray pointer" in Java, only well-formed ones.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12125
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java does have pointers

Now, i admit i've never read any of the real technical specification on the language, but...
is java REQUIRED to use pointers? I mean, if i wanted to write my own java compiler, if i could figure out a different way of doing it, am i allowed to?
IHMO, it would be better to say "java may or may not use pointers, but it doesn't matter, because you can't PROGRAM with them". just like a boolean - i don't know how it's represented inside the JVM, and i don't care. it could be a bit, a byte, a string... whatever. as long as it works, i don't care what it is.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred's post is assuming that the definition of a "pointer" is simply that it's a memory cell holding an actual RAM address. Given this definition, then no, Java doesn't use pointers. You could then just say that an object variable in Java is "an object reference".
But C++ already has "references," and Java's "references" aren't like the C++ ones, either, because in C++, references can't be reassigned -- only pointers can. So, by the same logic that lets you say that Java doesn't have pointers, you could say Java doesn't have references. Java must have something, though, since you can refer to dynamically allocated objects. Let's call them "snarfblatts."
So Java has "snarfblatts", which behave much like C/C++ pointers, except that you can't do pointer arithmetic with snarfblatts, nor can you convert a snarfblatt to an integer, nor can you create a snarfblatt from an integer.
The name "snarfblatt" is kind of ugly, and nonintuitive. Let's just call them "Java pointers."
Hmmm. Isn't that what I already said?
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Java does have to use references. Do those refrences have to be pointers? Well, not really...but the important thing is that they have to act like pointers, whatever they are. And if they act like pointers, well, then why call them something else?
(OK, we call them references because we can't do the pointer arithmetic that you can in C/C++...)
Consider:

Now, what is printed out? Since Java uses references, "George Shrinks" is printed. If you did not use references/pointers, you would get "Brian Henderson" printed out.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

aPerson.setName("George Shrinks");

My daughter loves that show
 
fred rosenberger
lowercase baba
Bartender
Posts: 12125
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks. i guess it's partially my C background that i hear "pointer", and i think "variable holding memory address". and i never got far with C++, so i got C pointers, C++ references and Java References all muddled in my brain.
thanks for the clarification!!!
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, but can Java perform snarfblatt arithmetic? I don't remember seeing it in the language spec...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic