954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Accessing Memory address

Hi Friends,
I have a doubt in accessing the memory address .

In java there is no pointers like c . then how can we access the address of the memory variable.

crbsathy
Newbie Poster
5 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

why do you want to?

sciwizeh
Posting Pro in Training
457 posts since Jun 2008
Reputation Points: 77
Solved Threads: 23
 

Thanks sciwizeh for ur reply.

I need to fetch the memory address of the variable . In one article which i have read that "we cant say that we cant access the memory location".

crbsathy
Newbie Poster
5 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

Thanks sciwizeh for ur reply.

I need to fetch the memory address of the variable . In one article which i have read that "we cant say that we cant access the memory location".

Use the Object.toString() method to return the memory address.

public static void main(String... args){

       Object o = new Object();
       System.out.println(o.toString());

}
Alex Edwards
Posting Shark
972 posts since Jun 2008
Reputation Points: 392
Solved Threads: 109
 

Note that it is a String representation of the memory address and not an accessible (or directly modifiable) memory address.

Alex Edwards
Posting Shark
972 posts since Jun 2008
Reputation Points: 392
Solved Threads: 109
 

If we use toString() method to print the object ID then it is only print the reference ID not the object ID.

Mohd Haider
Newbie Poster
1 post since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

These posts are confusing variables and Objects. Remember that variables are either primitives (ints etc, not Objects) or reference variables (contain a reference to an Object); a variable is NOT an Object.

Object's toString() is not defined to return anything related to the address of the object, nor does it return anything related to a reference. The API doc is perfectly clear - it returns the class name and the objects hash code.
The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

getClass().getName() + '@' + Integer.toHexString(hashCode())

The hashCode() methodmay return something relating to the address, but it's not guaranteed
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)

Having said all that, there is nothing in the Java language that corresponds to a memory address, and there is no syntax for accessing memory addresses.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 
If we use toString() method to print the object ID then it is only print the reference ID not the object ID.


This thread is from 2008. I'm posting to it just to correct errors in the replies in case anyone searches for this topic.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You