Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

pointers

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.

like image 448
Anubis05 Avatar asked Aug 19 '11 08:08

Anubis05


People also ask

Does Java have concept of pointers?

Java doesn't have pointers; Java has references.

Why pointer is unsafe in Java?

No pointer support make Java more secure because they point to memory location or used for memory management that loses the security as we use them directly.

Why are pointers eliminated in Java?

1)Pointers lead to confusion for a programmer. 2)Pointers may crash a program easily, for example, when we add two pointers, the program crashers immediately. 3)Pointers break security. Using pointers, harmful programs like Virus and other hacking programs can be developed.

What is unsafe code?

Unsafe code in general is a keyword that denotes a code section that is not handled by the Common Language Runtime(CLR). Pointers are not supported by default in C# but unsafe keyword allows the use of the pointer variables.


1 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.

like image 60
Sean Patrick Floyd Avatar answered Sep 28 '22 03:09

Sean Patrick Floyd