• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Tim Cooke
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
Saloon Keepers:
  • Piet Souris
Bartenders:

Why Pointers Are Not Required In Java?

 
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why pointers are not required in Java?
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The java language does a good job of hiding the complexity of memory and object management.
Objects are manipulated by references, which act like pointers in other languages.
Object myObject = new Object();
myObject is a reference variable that now contains a reference to a new object of type Object.
Example of manipulating references:
String myStringObject = new String("I am a string");
System.out.println(myStringObject.length());//this is a method invocation, ie, calling a method in the String object referenced by the myStringObject reference variable.
Here I will make a class declaration, then a variable that contains an instance of that class
class MyDataClass {
int i = 5;
}
MyDataClass newInstance = new MyDataClass();
System.out.println(newInstance.i); //references the member 'i' in the object
When you are done working with an object you can set it to a null reference with the null literal...
newInstance = null; //this makes this object eligible for garbage collection.

Basically, with object references you have all the benifits of using pointers without the problems of memory management, and pointer arithemetic to cause you problems.
You won't miss them!
Rob
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pointers were explicitely left out of Java, because they are the number one cause of bugs in C++ programs.
It is just TOO easy when you start doing arithmatic on pointers to mess up and shoot yourself in the foot. References allow you to locate an object without allowing the math on them that causes the problems.
 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob and Cindy, Thanks a lot.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic