开发者

Is there a concept of "pointers" or "unsafe code" in Java?

开发者 https://www.devze.com 2023-03-29 00:07 出处:网络
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 s

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.


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.


There are no pointers in Java, only safe references. Unfortunately your highly reputed CTO was wrong.

Unsafe code is integrated through JNI.

http://en.wikipedia.org/wiki/Java_Native_Interface


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.


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.

Other info on Wikipedia and Oracle.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消