• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java does not use pointers but uses references why?  RSS feed

 
kiran madhan
Ranch Hand
Posts: 34
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why Java does not use pointers but uses references only? How it helps Java to maintain safety for the code?
Can anybody explain with an example please.?
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please tell us where that quote comes from (it looks like a quote) and what explanation they gave and which parts of it you do not understand.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In languages such as C and C++ you have pointers, which are addresses of data in memory, and you can do pointer arithmetic, which means that you can make a pointer point anywhere, even to an invalid place in memory. Pointers and pointer arithmetic are relatively hard to understand for people (especially if you get to multiple levels of pointers: pointers to pointers, etc.) and are a major source of bugs and security flaws in C and C++ programs. It's easy to make a mistake in C or C++ and get a pointer that points to an invalid place in memory; if the program then tries to read from or write to that location in memory, the program might crash or might inadvertently expose data, or even start to execute code that a hacker has placed somewhere in memory, so that the hacker gains control over what the computer is doing.

When the Java programming language was invented, one of the goals was to make it easier for programmers to use than C++. So the language designers left out the parts that make C++ hard.

Besides that, not having pointers makes it possible to have garbage collection in Java. Because the JVM controls and manages where objects are in memory, the garbage collector has more freedom to move objects around in memory.
 
kiran madhan
Ranch Hand
Posts: 34
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Campbell Ritchie: it is not quote.I am faced an interview question.But i answered it using polymorphism concept in Java. But I do not know yet it is correct or not.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kiran madhan wrote:@Campbell Ritchie: it is not quote.I am faced an interview question.But i answered it using polymorphism concept in Java. But I do not know yet it is correct or not.


The C++ language supports both pointer arithmetic and polymorphism, so the lack of pointer arithmetic (ie. references vs pointers) isn't so that polymorphism can be supported.

So, how did you explain that it is related to the polymorphism concept in Java?

Henry
 
kiran madhan
Ranch Hand
Posts: 34
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Henry Wong: I explained based on run-time polymorphism (using method overriding) in java. Base class reference variable can refer to subclass objects and can access methods from any class. This I used to explain.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interview questions count as quotes. But interview questions do not simply have an answer; they also take into consideration how you answered them. Did you say anything about the problems of pointers, or their being a security hazard? If you give a simple straight answer like what would be required in an exam to all your questions, you are not likely to get very far with the job application.
 
kiran madhan
Ranch Hand
Posts: 34
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Campbell Ritchie: Actually, Interviewer asked me that " Will java use pointers or not?"
I said "Yes..indirectly". Then he asked me "how?" Then I explained like that using run-time polymorphism concept. But later I know Java doesn't use pointers. But why C,C++ use? or is there any problem...! and so on my question.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C was designed for writing operating systems; it is therefore necessary to have direct access to memory via pointers. Java® was intended for applications, which usually do not require direct memory access.
How much pointer arithmetic have you done?
 
kiran madhan
Ranch Hand
Posts: 34
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know how pointers work in C-language. I know the uses of pointers in C-language to access elements of an array, call by address, operations on pointers etc.
 
Stephan van Hulst
Saloon Keeper
Posts: 7962
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java language doesn't contain pointers. However, Java references may be implemented using pointers.
 
kiran madhan
Ranch Hand
Posts: 34
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
may i know that clearly...how Java references implemented using pointers.
 
Stephan van Hulst
Saloon Keeper
Posts: 7962
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's for a moment pretend that javac translates Java to C++ rather than bytecode:

The Java JIT compiler does something similar, except it translates references in bytecode to memory addresses (which is pretty much what pointers are).
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!