• Post Reply Bookmark Topic Watch Topic
  • New Topic

StringBuilder references  RSS feed

 
N Sam
Ranch Hand
Posts: 77
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My intention is to see the real reference values (memory locations) of 2 StringBuilder objects as below:


I was expecting a printout like
@S234853
@S984724
but all i got was 123 printed twice. How can i print out the reference values instead of what is inside the StringBuilder object ?
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

String classes override toString() method of object class that's why you see variable value instead of hash code.
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
N Sam wrote:My intention is to see the real reference values (memory locations) of 2 StringBuilder objects...


Could we back up a bit, and ask why you want to see those numbers?
 
N Sam
Ranch Hand
Posts: 77
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two intentions in my mind. First, i want to understand how to print memory locations of reference types, in JAVA. This has been an invaluable debugging tool for me in C. So knowing how to do this would be useful for me in future.
Second reason is that i want to see what a statement like StringBuilder sb3 = sb2 really does (is it shallow copy of the reference value or a deep copy of the string itself). That is the reason for my test program.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
N Sam wrote:Two intentions in my mind. First, i want to understand how to print memory locations of reference types, in JAVA. This has been an invaluable debugging tool for me in C. So knowing how to do this would be useful for me in future.

I disagree. Java is not C. This is not a useful exercise in Java.

Second reason is that i want to see what a statement like StringBuilder sb3 = sb2 really does (is it shallow copy of the reference value or a deep copy of the string itself).

You can test if they are the same object with sb2 == sb3
 
N Sam
Ranch Hand
Posts: 77
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just out of curiosity then.... Isn't there any way in Java to print out the memory location of a reference variable ? Perhaps some method somewhere, that can be used, maybe ?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no easy way to print the memory location of an object. Maybe with the debugger API that the JVM has, but that is not an API that people normally use regularly.

As already said, it's not very useful to know the memory address of an object in Java - you need to get rid of the C programming mindset when you are programming in Java. The Java virtual machine may even move an object in memory while it is doing garbage collection, without your Java program knowing about it.

And about your original question:
N Sam wrote:


I was expecting a printout like
@S234853
@S984724

There is only one StringBuilder object in your code, and two variables (sb2 and sb3) that refer to the same object. So even if you would be able to print out the memory addresses, you would see the same address twice.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote:
String classes override toString() method of object class that's why you see variable value instead of hash code.
You mean StringBuilder overrides toString.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean StringBuilder overrides toString.


Yes. Thanks for correction.

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
N Sam wrote:Just out of curiosity then.... Isn't there any way in Java to print out the memory location of a reference variable ? Perhaps some method somewhere, that can be used, maybe ?

As others have said, this really isn't very useful to you in Java. In fact, it comes under the heading of DontSweatIt - specifically, this topic (or something close).

Java is a memory-managed language, so you really don't need to know where things are; it's simply a distraction. I know it's tough to let go when you've come from something like C, where memory is very important, but trust me, in Java it really isn't.

Write good, clear code. That will solve more problems than anything - and probably prevent you having to debug in the first place. And if not, most IDEs now have very comprehensive debuggers built in.

Winston
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if you still really want to get the same toString() implementation that java.lang.Object offers, regardless of overrides, you can build that String as follows:

 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!