• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why java does not have pointer concept?  RSS feed

 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi:
Recently I wrote a c++ program. With pointer, things went smoothly. Java does not have pointer concept, even it was created after c++. So I was wondering whether pointer is a good thing? what advantage does java posses over c++ on pointer policy?
Thanks in advance!
Don
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Java does not have pointer concept, even it was created after c++.

Well, references to Java objects are pointers, in a sense that they point to a memory location. What Java does not have is pointer arithmetics. The designers of the language rightfully thought that anarchy of C++ pointer arithmetics causes more problems than flexibility.
Eugene.
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Eugene Kononov:
The designers of the language rightfully thought that anarchy of C++ pointer arithmetics causes more problems than flexibility.

Would you like to elaborate on that?
Thanks,
Don
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The two largest causes of bugs in C++ are pointers and multiple inheritance. The fact is that LOTS of developers mess these things up. Therefore these two concepts were left out of Java.
References are not pointers in the same way that C++ uses them. A pointer can be directly manipulated - you can do math on them and expect reasonable results. Not true with Java references, they are only locators to objects on the heap.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Don,
I don't want to put words in Eugene's mouth, but the problem with pointers is that you can code things like someObjectPointer++ which now refers to the the previous address offset by sizeOf(someObject). That may or may not be a problem depending on what is actually located at that address. Contrast that with a reference which by difinition is bound to a single address and it's easy to see why references are much safer. Semantically pointer arithmetic is similar to array notation in that someObjectPointer + 2 is equivalent to someObjectPointer[2]. But there are no restrictions on the arguments so someObjectPointer[-2] or someObjectPointer - 2 is valid. I doubt that C++ would have carried pointers in the first place except for the fact that it is a superset of C. So in Java, at least in theory, you can't corrupt the heap accidentally (or maliciously) by referring to a foo with a bar pointer.
Michael Morris
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Don Liu:

Would you like to elaborate on that?
Thanks,
Don

A couple problems with pointers (although, don't get me wrong, I've written lots of C++ and like it) is that, coupled with the lack of runtime checks in C++, they can be very dangerous to use. An array in C++, for example, is nothing but a pointer to a chunk of data in memory. There is absolutely nothing preventing you from continually incrementing the pointer to do something to every element of the array and then continuing right on past the array and writing things to random parts of memory. (Believe me, I've done that before )
In addition, C++ does not perform automatic garbage collection so, through the use of pointers, you can end up with what is known as a memory leak - a piece of memory that has been allocated, is no longer being used, and hasn't been released. Such problems can have disastrous effects on applications.
Now, take all of this with a grain of salt. What C++ does provide is amazing power and a lightning fast application.
I hope that helps,
Corey
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A thousand thanks to all of you! It helps a lot. I really appreciate it!
Best Regards,
Don
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!