2

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

Dale Steyn
  • 95
  • 1
  • 2
  • 10
  • 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 Answers7

7

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.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • So , you mean no "free" keyword in JAVA, am I right – Dale Steyn Aug 15 '13 at 09:31
  • 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
3

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

Community
  • 1
  • 1
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
2

The "replacement" of unmanaged pointer in Java is a managed reference .

Take a look at this answer in programmers forum for the differences between the two .

Community
  • 1
  • 1
aleroot
  • 71,077
  • 30
  • 176
  • 213
2

Java's references aren't pointers. You can't do pointer arithmetic with references in Java.

You can read a comprehensive analysis by Erik Demain here: C to Java: Converting Pointers into References.

Luis Sep
  • 2,384
  • 5
  • 27
  • 33
1

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.

christopher
  • 26,815
  • 5
  • 55
  • 89
1
Nothing. Java does not have a concept that is equivalent to Pointers.
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
1

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.

Ashwani
  • 3,463
  • 1
  • 24
  • 31