943,925 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 8768
  • Java RSS
Aug 19th, 2008
0

Accessing Memory address

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
crbsathy is offline Offline
5 posts
since Aug 2008
Aug 19th, 2008
1

Re: Accessing Memory address

why do you want to?
Reputation Points: 73
Solved Threads: 22
Posting Pro in Training
sciwizeh is offline Offline
423 posts
since Jun 2008
Aug 19th, 2008
0

Re: Accessing Memory address

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".
Reputation Points: 10
Solved Threads: 0
Newbie Poster
crbsathy is offline Offline
5 posts
since Aug 2008
Aug 19th, 2008
0

Re: Accessing Memory address

Click to Expand / Collapse  Quote originally posted by crbsathy ...
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.

java Syntax (Toggle Plain Text)
  1.  
  2. public static void main(String... args){
  3.  
  4. Object o = new Object();
  5. System.out.println(o.toString());
  6.  
  7. }
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Aug 19th, 2008
0

Re: Accessing Memory address

Note that it is a String representation of the memory address and not an accessible (or directly modifiable) memory address.
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Jan 5th, 2012
0

Print object ID.

If we use toString() method to print the object ID then it is only print the reference ID not the object ID.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Mohd Haider is offline Offline
1 posts
since Jan 2012
Jan 5th, 2012
0
Re: Accessing Memory address
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.
Quote ...
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() method may return something relating to the address, but it's not guaranteed
Quote ...
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.
Featured Poster
Reputation Points: 1924
Solved Threads: 952
Posting Expert
JamesCherrill is offline Offline
5,788 posts
since Apr 2008
Jan 5th, 2012
0
Re: Accessing Memory address
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.
Featured Poster
Reputation Points: 1924
Solved Threads: 952
Posting Expert
JamesCherrill is offline Offline
5,788 posts
since Apr 2008
Message:
Previous Thread in Java Forum Timeline: want to learn about sessions(servlets)!!
Next Thread in Java Forum Timeline: Java db





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC