I read that pointers are not available in Java. But "This" pointer in C++ is replaced by "This" Keyword. So can someone explain about dynamic memory allocation and replacement for pointers in java
-
Although the question has been closed as non constructive... http://stackoverflow.com/questions/8080617/why-doesnt-java-have-pointers/8080709#8080709 – Costis Aivalis Aug 15 '13 at 09:30
-
Read it where? According to the [JLS](http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.3) they are available: "The reference values (often just references) are pointers to these objects, and a special null reference, which refers to no object." There is also `NullPointerException` to consider. – user207421 Aug 16 '13 at 00:36
7 Answers
There are no pointers in java. Java works with references.
There is no concept of dynamic memory allocation in java. And hence there is no alternative of malloc/calloc
in java. The JVM takes care of creating and releasing the memory for objects. As JVM has built in functionality of garbage collection hence no alternative to free
is also provided.

- 67,789
- 12
- 98
- 136
-
-
1@DaleSteyn No there is no free keyword in java, supporting link : http://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html – Juned Ahsan Aug 15 '13 at 09:34
-
Could you please giv info about passing objects in java. How is it done??? – Dale Steyn Aug 15 '13 at 09:59
-
Ofcourse there is dynamic memory allocation in Java, using the `new` keyword. You just don't have to explicitly free dynamically allocated memory, because the garbage collector does that automatically for you. – Jesper Aug 15 '13 at 10:13
Internally a reference to an object is implemented as a pointer.. There is an anonymous inner class in java which you can use as a substitute for function pointer.
2.2.9 No More Pointers
Most studies agree that pointers are one of the primary features that enable programmers to inject bugs into their code. Given that structures are gone, and arrays and strings are objects, the need for pointers to these constructs goes away. Thus, Java has no pointer data types. Any task that would require arrays, structures, and pointers in C can be more easily and reliably performed by declaring objects and arrays of objects. Instead of complex pointer manipulation on array pointers, you access arrays by their arithmetic indices. The Java run-time system checks all array indexing to ensure indices are within the bounds of the array.
Check out this Thread

- 1
- 1

- 168,305
- 31
- 280
- 331
Java does use pointers. It's just handled for you, as "Object References". So for example:
String str = new String("efi");
// str is NOT the string object.
// It is a reference to the String object.
The difference is, Java does this for you and doesn't let you do it yourself. C++ allows you to manage them.

- 26,815
- 5
- 55
- 89
Nothing. Java does not have a concept that is equivalent to Pointers.

- 34,993
- 17
- 75
- 115
-
So objects are not passed. Only there reference is indicated or passed as parameters. is it? – Dale Steyn Aug 15 '13 at 10:03
Java uses reference to do initialization, setting or getting variables etc.. In C++ pointers are basically reference to the memory of that variable. So here each declaration is reference in java which is equivalent to pointer in c++, but internally handled by JVM not by programmer. Java internally handles this, we nothing have pointers like c++ in front.

- 3,463
- 1
- 24
- 31