Pointers in Java

Accessing memory from a Java program.

This article deals with pointers in Java.

The article is an approach to describe about various design insights of Java and also about writing a program in Java with which direct memory access is made possible.


Pointers in Java

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)?

Comments

Untitled

dont wanna think abt pointers at all!
Had a tiugh time trying to understand C pointers..

Last edited Nov 7, 2008 7:12 AM
Report abusive comment
Article rating:
Your rating:

Categories

Based on community consensus.

Activity for this knol

This week:

59pageviews

Totals:

1690pageviews
3comments