Are there any smart pointers in Java?

Is there anything in C ++ smart pointer strings in Java?

+7
source share
4 answers

All Java pointers (references) are smart pointers.

Java runs in a managed environment. This means that Java uses the garbage collector to clean up variables that are no longer referenced.

Java references are slightly different from pointers. Java abstracts all pointer values ​​and math that you see in C ++. Thus, at any time when you create a new object and store it in a variable, you store it in the Java version of the smart pointer.

+12
source

I only heard about smart pointers in the context of memory management. Since memory management is at the core of the Java platform, this is not necessary.

The closest matches that come to mind are java.lang.ref.WeakReference and java.lang.ref.SoftReference, since they allow you to set garbage collection to some extent.

+6
source

Yes and no. No, in Java there is nothing like a pointer object. Yes, in the sense that every object is a "smart pointer", which means that it undergoes garbage collection and takes care of its own life, so to speak.

+5
source

No, in Java there are no pointers to any class. In general, you don't need smart pointers, because you already have a garbage collector that does reference counting to automatically control the memory allocation / release cycle.

+3
source

All Articles