13 Replies - 11538 Views - Last Post: 15 June 2010 - 03:36 PM Rate Topic: ***** 1 Votes

#1 Sudhindra Prasad  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 12-March 09

get the location or address of a String variable

Post icon  Posted 17 March 2009 - 10:28 PM

Is it possible to get the address of a String variable in java ?
In c or c++ we use the pointer concept. But in java there is no pointer concept. I just want to get the address of the sting variable where it has stored?
Is This A Good Question/Topic? 0
  • +

Replies To: get the location or address of a String variable

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: get the location or address of a String variable

Posted 18 March 2009 - 12:29 AM

View PostSudhindra Prasad, on 17 Mar, 2009 - 09:28 PM, said:

Is it possible to get the address of a String variable in java ?
In c or c++ we use the pointer concept. But in java there is no pointer concept. I just want to get the address of the sting variable where it has stored?

No, it isn't possible. Why do you want it?
Was This Post Helpful? 0
  • +
  • -

#3 onlinehi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-March 09

Re: get the location or address of a String variable

Posted 18 March 2009 - 03:38 AM

View Postn8wxs, on 17 Mar, 2009 - 11:29 PM, said:

View PostSudhindra Prasad, on 17 Mar, 2009 - 09:28 PM, said:

Is it possible to get the address of a String variable in java ?
In c or c++ we use the pointer concept. But in java there is no pointer concept. I just want to get the address of the sting variable where it has stored?

No, it isn't possible. Why do you want it?


Anyway this is really not required... :-)
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: get the location or address of a String variable

Posted 18 March 2009 - 04:03 PM

There is no String variable is Java only String objects
String objects stores the String in an array of char but there is no way to directly access it
Anyway even if you could get the address of the first char how would you use it in Java ? You want to pass it to a C++ function ?
Was This Post Helpful? 0
  • +
  • -

#5 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: get the location or address of a String variable

Posted 18 March 2009 - 09:26 PM

You can get the program to tell you the location of an object (assuming it has no overrided toString() method), but you cannot use the location itself.

I stand corrected, read on.

This post has been edited by Locke: 19 March 2009 - 10:45 AM

Was This Post Helpful? 0
  • +
  • -

#6 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: get the location or address of a String variable

Posted 18 March 2009 - 10:51 PM

View PostLocke, on 18 Mar, 2009 - 08:26 PM, said:

You can get the program to tell you the location of an object (assuming it has no overrided toString() method), but you cannot use the location itself.

No, it won't even do that. :) Quoting the docs, it returns:

getClass().getName() + '@' + Integer.toHexString(hashCode())
Was This Post Helpful? 0
  • +
  • -

#7 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: get the location or address of a String variable

Posted 18 March 2009 - 11:03 PM

...You're right.

But what is the toHexString(hashCode()) part? It changes every time I execute a program with just printing out a generic Object...That's not the memory address? Hm...:hmmm:
Was This Post Helpful? 0
  • +
  • -

#8 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: get the location or address of a String variable

Posted 18 March 2009 - 11:11 PM

View Postn8wxs, on 18 Mar, 2009 - 09:51 PM, said:

View PostLocke, on 18 Mar, 2009 - 08:26 PM, said:

You can get the program to tell you the location of an object (assuming it has no overrided toString() method), but you cannot use the location itself.

No, it won't even do that. :) Quoting the docs, it returns:

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

Isn't that just the Objects Reference Address?

This post has been edited by Fuzzyness: 18 March 2009 - 11:11 PM

Was This Post Helpful? 0
  • +
  • -

#9 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: get the location or address of a String variable

Posted 19 March 2009 - 01:03 AM

Quoting Class Object

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.)

With garbage collection the object's address will change, so even if it was a pure address reference it won't be for long. :) Note that the hashcode is NOT changed when the heap is rearranged.
Was This Post Helpful? 0
  • +
  • -

#10 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: get the location or address of a String variable

Posted 19 March 2009 - 10:44 AM

Hm...Interesting. :hmmm:
Was This Post Helpful? 0
  • +
  • -

#11 sandeepslal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 15-June 10

Re: get the location or address of a String variable

Posted 15 June 2010 - 01:20 AM

View PostLocke, on 19 March 2009 - 09:44 AM, said:

Hm...Interesting. :hmmm:


It may look interesting, but actually it is not so. because when i'm creating two different string objects with the same content as follows,

// String will be created in constant pool of the heap memory.
String s1 = "abc";

// String will be created in non-constant pool of the heap memory.
String s2 = new String("abc");

Clearly, there will be two different addresses for these two string objects. But the above mentioned code is showing the same address for both the objects, which is not possible.
Was This Post Helpful? 0
  • +
  • -

#12 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2675
  • View blog
  • Posts: 11,300
  • Joined: 20-September 08

Re: get the location or address of a String variable

Posted 15 June 2010 - 02:31 AM

Quote

But the above mentioned code is showing the same address for both the objects, which is not possible.


Correct. It follows that it can't be the address, and indeed it isn't - it's the hash code
Was This Post Helpful? 0
  • +
  • -

#13 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1948
  • View blog
  • Posts: 4,048
  • Joined: 11-December 07

Re: get the location or address of a String variable

Posted 15 June 2010 - 03:15 AM

String overrides equals(), and therefore hashcode() to ensure equal objects have equal hashcodes. From the Object API:

Quote

Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.

Was This Post Helpful? 1
  • +
  • -

#14 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: get the location or address of a String variable

Posted 15 June 2010 - 03:36 PM

...Why the necro? This thread was over a year old.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1