Search on this blog

Monday, June 25, 2007

JAVA pointer ;)

Pointer in java? Some in telling there is no pointer concept in java. Is it so? What is null pointer exception then? Don't tell there is no pointer concept, you can say that java does not provide devs to access memory directly. In java we basically call pointer as reference. More details regarding this,
All variables that refer to objects (values of a class type) are reference variables. They refer to the object indirectly and are implemented with something like a machine address (though it could be more sophisticated). From an implementation viewpoint, objects are just "cells" on the "free store" and are always allocated with the new operator. They are deallocated automatically by the garbage collector when no active variable in the program references them any longer.
Pointer and reference variables give the programmer great flexibility, but they can lead to difficult to find and correct errors in your programs. In this section we will examine the common problems and give hints about how to avoid them. This should be useful to novice Java programmers.
In the following we will suppose the following types and variables
abstract class Animal
{...
}
class Lion extends Animal
{...
}
class Tiger extends Animal
{
public Tiger() {...}
public void growl(){...}
}
Tiger first = null;
Tiger second = new Tiger();
Tiger third;
Dereference null
It is an error to dereference null. This is the safest error to make, since the system will catch the error at run time.
first.growl(); // ERROR, first is null.
A null pointer does not point to anything. Dereferencing obtains the thing the pointer points to. This error is caught and will result in a NullPointerException being thrown. If it not caught the program will be halted with an error message pointing to the line with the error.
Note that
third.growl(); // ERROR, third has not been initialized.
is a different kind of error. We haven't initialized third with any value, not even null. The Java compiler will insist that we initialize each variable before we use it. This error would be caught by the compiler.
Aliasing Problems
Having more than one name for the same cell can also add flexibility to our programs, but there are some dangers. For example, after executing
third = new Tiger();
first = third;
we will eventually want to send messages to the object referred to by first. As long as we are aware that variables third and first reference the same object, all will be well. However, if we send third a message which results in the object changing its internal state, then we need to be aware that these state changes are visible via all variables that reference that object.
This is actually a benefit of object-oriented programming. For example, if you have a vector that contains Tigers and you need to modify a tiger, you don't need to remove it from the vector, then modify it, and then replace it. Any reference to it can be used to modify it and all references will be able to then take advantage of the changes.
Losing Cells
It may be an error to give a new value to a pointer that references an object unless you either first (a) create an alias of the cell, or (b) are willing to abandon the cell.
This is an error that is not caught by the system and may not even be an error, depending on the logic of the program. It simply results in free store cells that are reclaimed by the garbage collector. The program may make no further use of these cells.
second = third; // Possible ERROR. The old value of second is lost.
You can make this safe by first assuring that there is no further need of the old value of second or assigning another pointer the value of second.
first = second;
second = third; //OK
Note that giving second a value in other ways (NULL, new...) is just as much a potential error.
The Java system will throw an exception (OutOfMemoryError) when you call new and the allocator cannot allocate the requested cell.
Note that, from a language point of view, these kind of errors are not errors at all. It is just something that the programmer needs to be aware of. The same variable can point to different objects at different times and old values will be reclaimed when no pointer references them.
Novices often make the following error.
Tiger tony = new Tiger();
tony = third; // Error, the new object allocated above is reclaimed.
What you probably meant to say was:
Tiger tony = null;
tony = third; // OK.
Improper Casting
It is illegal to cast a reference variable to a class when the object to which the variable then points is not a member of that class or a subclass. It is illegal to cast a reference to an interface type when the object to which the variable then points does not implement that interface.
It is possible to cast a variable of one reference type to another reference type if the cast is valid. The cast is checked.
Lion leo = new Lion();
Tiger tony = (Tiger)leo; // Always illegal and caught by compiler.
The above is never legal since a Tiger can never refer to a Lion object and a cast doesn't change the type of an object, just affirms that it has a type.
Animal whatever = new Lion(); // Legal.
Tiger tony = (Tiger)whatever; // Illegal, just as in previous example.
Lion leo = (Lion)whatever; // Legal, object whatever really is a Lion.
The first statement above is legal since a variable of a superclass type can refer to a variable of any subclass type. The second is illegal, but won't be caught until runtime (by most compilers) since the compiler may not do enough analysis of the program to determine that whatever isn't a Tiger. The third is legal, but a run time check will be used to verify the fact. The variable whatever really is a Lion.
An improper cast caught at runtime will result in a ClassCastException being thrown.

0 comments:

Post a Comment

Featured Video

Featured Article

Poems