Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

pointers doubt

 
geet kaur
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ankit Garg
Sheriff
Posts: 9527
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15436
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Somnath
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic