aspose file tools*
The moose likes Beginning Java and the fly likes Pointers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Pointers " Watch "Pointers " New topic
Author

Pointers

Sasha Hernandez
Greenhorn

Joined: Oct 14, 2003
Posts: 21
What is the significance of the fact the Java does not have any pointers?
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10
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).


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11422
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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

Joined: Aug 20, 2001
Posts: 1821

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.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34


aPerson.setName("George Shrinks");

My daughter loves that show
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11422
    
  16

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

Joined: Jul 30, 2003
Posts: 805
Okay, but can Java perform snarfblatt arithmetic? I don't remember seeing it in the language spec...


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pointers