aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes pointers doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "pointers doubt" Watch "pointers doubt" New topic
Author

pointers doubt

geet kaur
Ranch Hand

Joined: Sep 03, 2008
Posts: 78
guys why don't we have pointers in java?like c++
i have tried to understand it many a times but still confused about this..
can any one explain??
Paul Somnath
Ranch Hand

Joined: May 19, 2008
Posts: 177
Originally posted by geet kaur:
guys why don't we have pointers in java?like c++
i have tried to understand it many a times but still confused about this..
can any one explain??

A very general explanation:
Pointers directly allow you to manipulate memory. If you are allowed to manipulate memory, then it may lead to destructive programs. Hence they are not allowed in Java for security reasons.
In C++, you can use pointers to reclaim memory and sometimes may lead to memory leak since you do it manually. In Java you have the garbage collector which does this automatically and minimizes the risk of memory leak and hence you can concentrate on the application programming rather than memory considerations/optimizations.
[ October 21, 2008: Message edited by: Paul Somnath ]

Preparing for SCJP 6.0
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9304
    
  17

why do you need pointers in java. pointers come with many difficulties like memory leaks, memory corruption etc.

With pointers though you can create dynamic sized arrays and linked lists these are already available in java as references in java are like pointers with automatic memory management....


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14268
    
  21

Pointers are hard. Probably most of the bugs in programs written in C or C++ are related to the use of pointers - for example, you're using a pointer that's not initialized properly to point to an object in memory, or you are using a pointer that isn't valid anymore (because the object to which it points has already been deleted).

Pointers are not really necessary, as Java (and other languages that don't have pointers) have proven. The designers of the Java language decided to not include pointers to make Java easier to use.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Paul Somnath
Ranch Hand

Joined: May 19, 2008
Posts: 177
As Jesper says, pointers that do not point to anything(dangling pointers) are really painful. With automatic memory management, it is a hell lot easier to code and forget about memory considerations.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: pointers doubt