Hi,
We no that their no support for Pointer in Java as in C and C++. So how can we print the exact location of a varaiable in java?

Shahab

Recommended Answers

All 5 Replies

Why would you want to do that?

I want to create a Table of Variables and their values regarding their Memory location. In simple words I am going to implement Linklist (My Assignment) and wants to know wether my current node holds the memory address of the next node?

Shahab

If you are not aware then there is a soft form of pointers in Java only they are called "references".

Whenever you create an object in Java as :-

Alpha var1 = new Alpha();
Alpha var2 = var1;

"var1" and "var2" are actually references to the same object of class "Alpha". Try some small programs you will figure it out.

Thanks for reply.
No I am not confused in referencing. As we use

cout<<(&someVar);

in C++ which will print the memory address of the someVar. then how we can do it in Java?

Shahab

in C++ which will print the memory address of the someVar. then how we can do it in Java?

Why are you obsessed with printing memory addresses, when you can accomplish your linked list with "references" in Java.
<EDIT>

I willl just put a simple example here of how a simple node of a linked list can be implemented:-

class Node {
  int data;
  Node next;
}

Hopefully this will make it more clear. You can also check out this and this for some more explanation on how "references" are handled in Java.

commented: It will sink in eventually :) +18
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.