Pointer Arithmetic- JAVA feature normally tucked away from Application Developers

Pointer Arithmetic- JAVA feature normally tucked away from Application Developers

From the very beginning, during early days of my graduation till date I found this to be a common-place myth-turned-to-concept , "Java does not support pointers" . However, this is only a part of the lunar phase one sees after a New moon!

Anyway, coming to technicality, JAVA does have provisions to pointers, which mayn't be required for most of the applications we develop everyday- but may be fruitful in some cases. Not going to details , let me illustrate one example here, reiterating from one of my old-archives -

import sun.misc.Unsafe;
import java.lang.reflect.*;


public class K
{






private static Unsafe unsafe;
private static int fieldOffset;
private static K instance = new K();


static {
try {
Field f = Unsafe.class.getDeclaredField("theUnsafe");
f.setAccessible(true);


unsafe = (Unsafe)f.get(null);


fieldOffset = unsafe.fieldOffset(K.class.getDeclaredField("obj"));
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}


private Object obj;


public synchronized static long toAddress (int o)
{
instance.obj = o;
return unsafe.getLong(instance, fieldOffset);
}


public synchronized static Object toObject (long address)
{
unsafe.putLong(instance, fieldOffset, address);
return instance.obj;
}

}

The above class is a brief example to obtain address of a variable and retrieve its value from the latter.

Now let us have a main method some where which uses functionality of the above class.

public static void main(String ... args) throws Exception{
	int a=10; // A value is assigned to a varaible
	long addr_a=toAddress(a);
	System.out.printf("value of a is =%d and the address is %s\n",a,String.valueOf(addr_a));
	System.out.println("\n========================\nNow we will recover the value from addresss,  i.e. addr_a\n========================\n");
	int attemptedToRecoverValue_a=(int)toObject(addr_a);
	long addr_a_check=toAddress(attemptedToRecoverValue_a);
	System.out.printf("value of a is =%d and the address is %s\n",attemptedToRecoverValue_a,String.valueOf(addr_a_check));
	System.out.println("\n======Now we will rchange the value of a=========\n");
	a=940;
	System.out.println("====================================\nThe value has been changed and let's repeat the steps\n====================================\n");
	long addr_a_new=toAddress(a);
	System.out.println("\n======Now we will recover the value from addresss,  i.e. addr_a_new\n=============================\n");


	int attemptedToRecoverValue_aNew=(int)toObject(addr_a_new);
    long addr_a_check_new=toAddress(attemptedToRecoverValue_aNew);
    System.out.printf("value of a is =%d and the address is %s\n",attemptedToRecoverValue_aNew,String.valueOf(addr_a_check_new));


	int previousVal=(int)toObject(addr_a);
	System.out.println("\n=====================\nFinally, the previous value of a is found out to be= "+previousVal+"\n============\n");






}

The result is shown below:

No alt text provided for this image

The full code is given below-

import sun.misc.Unsafe;
import java.lang.reflect.*;


public class K
{






private static Unsafe unsafe;
private static int fieldOffset;
private static K instance = new K();


static {
try {
Field f = Unsafe.class.getDeclaredField("theUnsafe");
f.setAccessible(true);


unsafe = (Unsafe)f.get(null);


fieldOffset = unsafe.fieldOffset(K.class.getDeclaredField("obj"));
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}


private Object obj;


public synchronized static long toAddress (int o)
{
instance.obj = o;
return unsafe.getLong(instance, fieldOffset);
}


public synchronized static Object toObject (long address)
{
unsafe.putLong(instance, fieldOffset, address);
return instance.obj;
}


public static void main(String ... args) throws Exception{
	int a=10; // A value is assigned to a varaible
	long addr_a=toAddress(a);
	System.out.printf("value of a is =%d and the address is %s\n",a,String.valueOf(addr_a));
	System.out.println("\n========================\nNow we will recover the value from addresss,  i.e. addr_a\n========================\n");
	int attemptedToRecoverValue_a=(int)toObject(addr_a);
	long addr_a_check=toAddress(attemptedToRecoverValue_a);
	System.out.printf("value of a is =%d and the address is %s\n",attemptedToRecoverValue_a,String.valueOf(addr_a_check));
	System.out.println("\n======Now we will rchange the value of a=========\n");
	a=940;
	System.out.println("====================================\nThe value has been changed and let's repeat the steps\n====================================\n");
	long addr_a_new=toAddress(a);
	System.out.println("\n======Now we will recover the value from addresss,  i.e. addr_a_new\n=============================\n");


	int attemptedToRecoverValue_aNew=(int)toObject(addr_a_new);
    long addr_a_check_new=toAddress(attemptedToRecoverValue_aNew);
    System.out.printf("value of a is =%d and the address is %s\n",attemptedToRecoverValue_aNew,String.valueOf(addr_a_check_new));


	int previousVal=(int)toObject(addr_a);
	System.out.println("\n=====================\nFinally, the previous value of a is found out to be= "+previousVal+"\n============\n");






}
}


To view or add a comment, sign in

More articles by Soham Sengupta

Others also viewed

Explore content categories