Well, you might have heard of pointers in C and C++, but not ‘Pointers in Java’. Sounds right?
Hope you heard it wrong that Java does not allow using pointers. For your information, Java also supports pointers!
Surprised?
Well, read on!
Java allows or does not allow the usage of pointers, or in other terms, direct memory access, is still a debatable topic. But to be faithful to the design, and the so called features, pointers should not be there in Java.
Let’s open up a debate, and if you think that Java does not have pointers, keep up your spirits and fire away the following arguments!
a) Java does not have pointers.
b) Java does not allow pointer arithmetic.
c) You can, no way, directly access your computer memory. It’s all the job of the JVM used by you, be it Sun HotSpot or Oracle JVM or AegisVM. The programmer doesn’t care.
d) Java has been designed to keep away pointers, to allow security and portability, because, pointers allows the programmer to go low level and access memory in his own style and manner.
e) In Java, can you access uninitialized or deallocated memory? No, right! Well, that's because of the lack of pointers.
f) Can you do memory management with Java? No, because you need to have pointers to access memory directly.
g) Even though Java has no pointers, it has reference types, which allows indirect access to memory. But, trust me; it stands for the features of Java such as portability and security. Reference does not allow you to break such features.
Enough said; now it's the time for those who think that Java has pointers and pointer arithmetic embedded in a sarcastic fashion. Let those love pointers support their views with the following justifications.
a) Java has pointers. Reference:- http://java.sun.com/docs/books/jls/t...es.html#106237
b) It says: - An object is a class instance or an array. The reference values (often just references) are pointers to these objects, and a special null reference, which refers to no object.
c) To be specific, Java 1.5 really supports pointer arithmetic.
d) It's all about the sun.misc.Unsafe class, which you shouldn't use incode you intend to be portable.
Now Unsafe class has become the talk of the town, let's peep into what's that all about!
Get set, go!
The public class sun.misc.Unsafe provides a wide variety of “dangerous” utility functions, including direct memory access (which can be used to simulate pointer arithmetic) and object access which is much faster than reflection.
Please run the following program to see what happens, when programmers have direct access to memory.
import sun.misc.Unsafe;
import java.lang.reflect.Field;
public class TestPointers {
public static void main(String[] args) throws Exception {
Unsafe unsafe = getUnsafe();
System.out.println("Unsafe = " + unsafe);
System.out.println(" addressSize() = " + unsafe.addressSize());
System.out.println(" pageSize() = " + unsafe.pageSize());
System.out.println(" pageSize() = " + unsafe.pageSize());
try {
unsafe.putByte((long) 0xa000, (byte) 47);
} catch(Throwable e) {
System.out.println("IN THE CATCH BLOCK");
e.printStackTrace();
} finally {
System.out.println("IN THE FINALLY BLOCK");
}
System.exit(0);
}
public static Unsafe getUnsafe() {
Unsafe unsafe = null;
try {
Class uc = Unsafe.class;
Field[] fields = uc.getDeclaredFields();
for(int i = 0; i < fields.length; i++) {
if(fields[i].getName().equals("theUnsafe")) {
fields[i].setAccessible(true);
unsafe = (Unsafe) fields[i].get(uc);
break;
}
}
} catch(Exception ignore) {
}
return unsafe;
}
}
Congrats, you have just crashed your JVM.
Also check out this bug: - http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4420961
Open question:-
If Java does’nt supports pointers or pointer arithmetic, why does Mr. NPE, aka Null Pointer Exception exists, why not it is called as NRE (Null Reference Exception)?
Best regards,
Ajith Joseph | Virtusa | +91 44 42002700 Extn: 3513 +91 98842 55029 ajith.joseph/yahoo POD: ajith.joseph/Thomson
Tuesday, January 29, 2008
Pointers in Java
Subscribe to:
Post Comments (Atom)
5 comments:
watermelon viagra viagra liver damage cheapest uk supplier viagra viagra cialis women taking viagra lowest price viagra viagra and hearing loss viagra rrp australia cost generic viagra online viagra vs cialis viagra generic soft tab viagra overdose cheap generic viagra womens viagra
[url=http://www.blingforfun.com]hip hop jewelry[/url],[url=http://www.blingforfun.com/pendants/cat_9.html]hip hop pendants[/url],hip hop watches,[url=http://blingforfun.com/belts/cat_18.html]bling bling[/url] ,hip hop,[url=http://blingforfun.com/chains/cat_7.html]hip hop chains[/url],hip hop bling,[url=http://blingforfun.com/chains/cat_7.html]iced out chains[/url],[url=http://www.blingforfun.com/chains/cat_7.html]wholesale chains[/url]
hip hop jewelry
wholesale hip hop watches
hip hop rings
Amiable dispatch and this mail helped me alot in my college assignement. Thank you on your information.
good afternoon people. I'm actually into shoes and I have been digging for the sake of that exact brand. The prices as regards the shoes are approximately 310 dollars everwhere. But for all I bring about this location selling them as a remedy for half price. I really want those [url=http://www.shoesempire.com]prada sneakers[/url]. I will probably purchase those. what is your opinion?
black christian dating site [url=http://loveepicentre.com/]women report on men met dating sites[/url] older women personals http://loveepicentre.com/ midlands dating
Post a Comment