Yesterday I was attending a talk by a CTO of a reputed European Company, and he told until recently he did not know that java has pointers. On confronting him he said he is absolutely sure about existence of pointers/unsafe code in java.
-
2Perhaps he was confusing C# and Java? – Jason Coco Aug 19 '11 at 08:16
-
11"reputed" guy just lost some reputation :). – MAK Aug 19 '11 at 08:21
-
2Can we get a name, so I can sell some shares? – DJClayworth Aug 19 '11 at 13:23
-
3"Unsafe code" exists in every language which has at least disk i/o, I'd say :) – Kos Aug 19 '11 at 15:18
7 Answers
There's a class called sun.misc.Unsafe
, that much is true. But it doesn't use pointers in Java code (because Java has no pointers, although I agree that java references are similar in concept), most of it is implemented using native code.
As I mentioned in the comments, this is not part of the public API and shouldn't be used by client code. However, you can see it at work when you look at the sources of the Atomic*
classes in the java.util.concurrent.atomic
package.

- 292,901
- 67
- 465
- 588
-
After some more googling this is also what I came across . This might ve what he was talking about . Looking into the api now . – Anubis05 Aug 19 '11 at 08:28
-
@ganguly look at it but don't use it. It's an internal API you should stay clear of. You can see it in action if you loo at the sources of `AtomicBoolean` and the other `Atomic*` classes in the same package. – Sean Patrick Floyd Aug 19 '11 at 08:29
-
3Note that the `sun.misc.Unsafe` class is not a public class in the Java API; it might not (and probably does not) exist in other JVM implementations than Oracle's - so don't use it if you want your program to stay independent of the specific JVM implementation you're using. – Jesper Aug 19 '11 at 08:51
-
Will not use in production . But trying to have some fun with it . Not successful till now .. proving quite difficult to even import it due to security restrictions .:( – Anubis05 Aug 19 '11 at 08:54
There are no pointers in Java, only safe references. Unfortunately your highly reputed CTO was wrong.
Unsafe code is integrated through JNI.

- 82,057
- 50
- 264
- 435
-
1Look at the answer from @Sean Patrick Floyd . That might be what he was talking about. – Anubis05 Aug 19 '11 at 08:28
Java has pointers.
The confusion whether Java has pointers or not is strongly related to the discussion whether Java is call by reference or call by value.
Uninformed people think that Java has no pointers, and since a method can change an object that's passed in with the effects of this change vissible to the caller, they reason it must be call by reference.
This is not correct however.
What happens is that in Java pointers are passed by value. There are no other kinds of variables in Java for objects than pointer variables and there is no call by reference.
The "unsafe" story is quite something else. This normally is distinct from the question of whether Java has pointers or not. Java's pointers are safe; they point to objects and using the normal language constructs they can not be manipulated to point to arbitrary memory locations.
There is however JNI, but then native code does potentially unsafe things, not Java code.
There is also Real-time Java (jsr-1), where you absolutely can get access to specific memory locations in your system. This however is a very specific and rather rare version of Java that's mostly used for embedded purposes. If this was meant I guess it would have been explicitly mentioned.

- 3,729
- 17
- 15
-
Need one clarification . You are saying that in java only pass by value happens . But if that is the case then static variables or classes cannot be passed by value , right ? Because every calling method need to update the same reference . Correct me if I am wrong . – Anubis05 Aug 19 '11 at 08:43
-
In case of a static variable, it's a simple pointer again. The variable is static, but it holds a pointer to a normal heap allocated object. This same pointer can be passed around and assigned to non-static variables. There's nothing special about it. – Mike Braun Aug 19 '11 at 08:53
-
Java does not have explicit *pointers* like in C or C++, that you can make to point to any place in memory. Java has *references* instead of *pointers*. Sure, those are essentially the same at a low level, but saying that Java has pointers is confusing and misleading, especially for people who know what pointers are in C and C++. – Jesper Aug 19 '11 at 08:55
-
5Jesper, that is another kind of discussion. Does the official definition of 'pointer' necessarily include *has* to point to real memory where an object happens to be allocated, or is it more abstract 'point to object'? What does real memory even mean in the context of virtual address spaces and virtual machines? Adfitionally, does a pointer stop being a pointer when arithmatic on it is disallowed? Note that in C you can't multiply two pointers. Are they lesser pointers than those in a system that would allow this? – Mike Braun Aug 19 '11 at 09:17
-
@Mike _definition of 'pointer'_ -- looks like finding an agreed definition is the key; without it endless pointers-in-Java discussions sound apples vs oranges to me. How would you define a pointer? Would [Wikipedia atricle](http://en.wikipedia.org/wiki/Pointer_%28computing%29) do? – gnat Aug 19 '11 at 10:44
-
2@Mike My point is: don't call it *pointer* when you're talking about something else than explicit pointers as in C or C++, but that's what people think you mean when you use the word *pointer*. – Jesper Aug 19 '11 at 11:16
-
1I'm not 100% sure how authoritative Wikipedia really is. I've seen articles that for years said A and were then changed to say B, with B the opposite of A. The current Wikipedia article is fairly clear though that a 'pointer' MUST contain an actual memory address. Everything else is called either 'index', 'reference' or something else entirely. If you make a C implementation that is run on a VM, where malloc returns a number that represents an ID, then suddenly the exact same source code compiled for that VM does not have pointers anymore, but references? – akira Aug 19 '11 at 11:21
-
1The definition of pointer that I always uses: pointer points to an area in memory, reference points to an object. It is possible to multiply two pointer (although it makes no sense, really), you simply cast it into an integer first, then back again. It is impossible to do any sort of "reference arithmetic", since you can't turn a reference into an address and **back into object again**. – Lie Ryan Aug 19 '11 at 11:57
-
@akira: nowadays, unless you're programming an OS kernel, the address that you get are probably not the real memory address, they're virtual memory address. – Lie Ryan Aug 19 '11 at 11:59
-
You can't do pointer arithmetic and that's the sense on the question. Java doesn't have pointers in that sense. – David Heffernan Aug 19 '11 at 14:15
-
Agreed, no pointer arithmetic in Java, but is the ability to be involved in arithmetic a strong requirement in the definition of a pointer? – Mike Braun Aug 20 '11 at 15:01
-
Java or the JVM do not have pointers. The eventual existence of pointers as the underlying implementation of references is purely an implementation detail, and it's totally up to the JVM how it deals with it. Consequently, there is no guarantee whatsoever that whatever is returned by `new Object()` points to a specific location in memory, and unlike languages that have pointers, the JVM is completely free to move that object to a different location of the heap, or even to the stack, if the situation calls for it. HotSpot *will* do this, so thinking a reference is a pointer is a mistake. – JimmyMcHoover May 22 '19 at 12:58
Java has no pointers, only references to objects. A reference is similar to a pointer, because it points to a variable, an object, but you cannot view or edit the address memory of this reference, which you can do in C.
Another thing. In C, you can manage pointers with the referencing/dereferencing operation, putting a * first of the name of pointer. In Java, this operation, referencing/dereferencing, is completely absent, because it's totally automatic, hidden to the user.

- 7,972
- 3
- 38
- 61
-
1The address of and dereferencing operators are absent because Java only has pointer variables. These operators only make sense in a language that has both pointer- and stack ('automatic') variables to convert between the two. – Mike Braun Aug 19 '11 at 08:37
-
@Mike . Can you please explain this in bit details "These operators only make sense in a language that has both pointer- and stack ('automatic') variables to convert between the two" . This part went whooosh !!! – Anubis05 Aug 19 '11 at 08:45
-
@Mike Braun Thank you for the clarification. For 'automatic', I meant the operation is hidden to user, I forgot to write it. I will edit my answer adding this detail. :) – Alberto Solano Aug 19 '11 at 08:47
-
@Mike Braun Sorry but reading your comment another time, I think that saying Java only has pointer variables is a kind of mistake. I never heard that Java has pointer variables. Maybe you can say that Java has no explicit management pointer, because this is hidden to user, only references. I have written programs in C, and in Java I never see, so far, operation like &pointer, or *pointer. – Alberto Solano Aug 19 '11 at 09:03
-
1'Automatic' is a specific term in C++ to refer to non-pointer variables. The 'automatic' comes from the fact that stack allocated objects are automatically destroyed when they fall out of scope. This is not related to the automatic referencing and dereferencing in Java. In fact '.' in Java is simply '->' in C++ and Foo in Java is 'Foo*' in C++. Since in C++ terms, Java does not have 'Foo', they simplified 'Foo*' to 'Foo'. Easy for people who only ever use Java, very confusing for everyone else. – Mike Braun Aug 19 '11 at 09:06
-
In addition, this is what Oracle declared about Java features, in particular "No more pointers",paragraph 2.2.9 of this link: http://java.sun.com/docs/white/langenv/Simple.doc2.html – Alberto Solano Aug 19 '11 at 09:08
-
The Sun 'no pointers' claim was largely marketing. C++ was seen as difficult because of pointers. Since Java was to be the easy variant of C++ they had to say it did not have pointers. – Mike Braun Aug 19 '11 at 09:22
-
2Yes, this is marketing. I can tell you only that Java references is similar to C/C++ pointer at low level, like you said, but the management in Java is completely different from C/C++, because the programmer doesn't need to worry about pointer operations. This is the discussion. Java is different from C/C++ because has different management. – Alberto Solano Aug 19 '11 at 09:37
-
Look at the answer of krishna moorthy. I agree with what is written in it. – Alberto Solano Aug 19 '11 at 09:41
-
1Not only need the programmers not worry about the pointers (pointers are used in the background to implement Java references), but they cannot assign a reference a specific address in memory and the dereference it in the same way as you can do it in C/C++. I agree that Java has no pointers. – Giorgio Aug 19 '11 at 13:11
-
2The question is whether the concept of pointer is defined by how C/C++ does things. Is 'memory address' an essential part of the concept pointer, or do pointers happen to be implemented in C/C++ via a memory address? Does the C++ spec requires pointers to be memory addresses? – akira Aug 19 '11 at 13:41
Pointer is the concept of pointing to the reference. In C and C++ you could access the pointer explicitly but in java we use reference to point the objects. For example If you are passing the an instance to a method, Its reference passing we know but ultimately pointer is passed. when you change state of the instance within the method it reflects when that instance is used after the completing the method call.
We use reference in java which is pointer with respect to JVM.
When we use new operator it create the instance in heap and returns the address to the reference its pointing.
I believe this would answer your question if not you could comment on my answer.

- 293
- 1
- 4
- 14
-
What you are saying is absolutely correct , but I do not think that was the case . Because he is a security guy and was talking from the perspective of writing unsafe code in java like what you can do with pointers in C. I think sun.misc.Unsafe is the package he was talking about. Take a look – Anubis05 Aug 19 '11 at 08:34
You can call native functions in Java (JNI), so in this sense you can use pointers. But other than that - no, there are no pointers in Java.

- 91,536
- 11
- 82
- 95
He may have a slight confusion with call-by-value and call-by-reference. If he comes from C, he may think that call-by-reference is equal to a pointer. Just a guess.

- 2,622
- 1
- 22
- 34