Hi guys ,
Assume the following is my program, is there a way to see how the memory is being allocated by the java program ? How do I use jconsole to find out where in main memory that Integer i = 10 is being saved?

public class IntegerCmp{
    public static void main(String [] dummy){
        Integer i = 10;
        Integer j = 11;
        Integer k = ++i;
        System.out.println("K == i ?" + (k == i));
        System.out.println("k.equals(j): "+k.equals(j));

Varun Krishna. P

James Cherrill,

So from the time when I had actually started learning Java it has been told something like this, String hello = new String("hello");. When the java compiler sees this line it instantiates an object in the name of hello in the main memory and assigns a memory address and puts the value there in the address, so I am curious in seeing this in action. I just wanted to learn/know how the memory address looks like.

Varun Krishna. P

Edited 1 Year Ago by Varunkrishna: More description added.

Guess it is not possible in Java. The only thing close to it is this:

There is no way to access addresses of variables in Java. However, the default hashCode() method defined in the Object class, "is typically implemented by converting the internal address of the object into an integer". Therefore, in some Java implementations at least, the hash code returned by Object.hashCode() reflects at least part of the address of an object. For objects whose classes have overridden the hashCode() method, you can still access the original hash code through the System.identityHashCode() function.

I found that here.

Yes, that code creates a new String object with the value "hello" and it also creates a new variable called hello that contains a reference to the String. The String will be created in the JVM's heap(s) somewhere. The variable may be in the heap (if it is a static or instance variable) or on the stack if it is local.

The language definition refers to references, not to addresses. Yes, the simplest sort of reference is just the address of the object being referred to. But that's not necessarily so and there are good reasons why you might do it differently (eg use an offset into a table of object addresses, which then allows you easily to move objects to optimise memory usage). The design of the Java language is very careful never to say anything about actual memory addresses.

Bottom line: in some implementations you may be able to interpret the default hash as an object address, but this is not required or guaranteed. In general there is no way to get an object's actual memory address.

No my question was, is there any tool or something that lets me see what is there in the memory ? Then what will JavaVisualVm does ? does it let me access the memory ?

Java does not give you direct memory access, that's one of the main reasons they designed the language in the first place, do away with all that messy malloc, calloc, etc. etc. you're stuck with in C.

And no, your program doesn't allocate any memory at all. It requests storage for an object from the JVM which puts it somewhere and hands back a reference through which the program can then retrieve the object again.

This question has already been answered. Start a new discussion instead.