• Post Reply Bookmark Topic Watch Topic
  • New Topic

println method and toString  RSS feed

 
Sudhanshu Mishra
Ranch Hand
Posts: 238
1
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I know that toString method returns a String representation of the object,but,does it mean that toString() when used inside the println method creates a new String object.
In short,my question is ,whenever we print an object by println,its toString is called,so does the toString method creates a String object.
Because it may be asked in the question about the number of objects created,so,should we also look at the println method to check if a String object might be created there?



Thanks....
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the details for println(Object) (Java7 version). It says it uses this, so we look for it, and find it does in fact use toString (or the String "null").

So, you are right, there is a String object created. It is worthwhile checking the internal workings of the toString method used, because that may actually create several objects.That creates String objects corresponding to foo, baa and baz (3) and a StringBuilder object (4) which catenates those Strings with the primitives i j and k to form a 5th String. And who knows how many objects the foo class creates in its toString() method.

That is the point of encapsulating. You only need to know about these internal workings when you have a "how many objects?" question. For everything else, you don't need to know
 
Sudhanshu Mishra
Ranch Hand
Posts: 238
1
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
ok,i got it.
But does it means that whenever i am asked to find number of objects created,i should always check the println method too?
Ex:
class xyz
{
public static void main(String...a)
{
Integer i=90;
System.out.println(i);
}
}
Now how many objects.I guess it should be two.One wrapper and the other string.
Please have a look.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know. I don't do "how many objects?" questions if I can help it
 
Sudhanshu Mishra
Ranch Hand
Posts: 238
1
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sir then whom should i put up this question to.
I am really confused about this concept.
Please help me out.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could try searching these forums - but prepare for even more confusion when you come across this thread.
 
Jacek Garlinski
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sudhanshu Mishra wrote:In short,my question is ,whenever we print an object by println,its toString is called,so does the toString method creates a String object.


According to my knowledge - no. String is special kind of object - they are immutable so with this code:

JVM is able to take care about creating Strings - so value1 and value2 reference to the same object in JVM. This obj lives as long as JVM wants it ;)

So, go back to your question - if you call toString() the first time there is a chance that a new String is created, but when you call toString() for the second time, you'll use String object that already lives on heap.
 
Aditya Jha
Ranch Hand
Posts: 227
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jack gr wrote:
Sudhanshu Mishra wrote:In short,my question is ,whenever we print an object by println,its toString is called,so does the toString method creates a String object.


According to my knowledge - no. String is special kind of object - they are immutable so with this code:

JVM is able to take care about creating Strings - so value1 and value2 reference to the same object in JVM. This obj lives as long as JVM wants it ;)

So, go back to your question - if you call toString() the first time there is a chance that a new String is created, but when you call toString() for the second time, you'll use String object that already lives on heap.


This is not correct. The example given above uses String literals, and hence value1 and value2 do point to the same instance. However, invocation of toString() (or any other such method) almost always creates objects on the heap, which are not automatically reused. In other words, more often than not multiple invocations of toString() is going to give you 2 different objects. Please note that this completely depends on how toString() has been implemented. Consider the following example:

This class is bound to create only one instance of String for multiple invocations of toString(), simply because it uses a String literal. However, the following class:

creates a new instance every time the toString() method is called.

A fun fact :
The toString() method in class Integer has been implemented in such a manner that if the object is representing Integer.MIN_VALUE, then a String literal is used, and hence multiple invocations of toString() method return the same instance of String. However, if the Integer object contains value other than Integer.MIN_VALUE, then a new String instance is created every time this method is invoked. Although, I seriously doubt if certification exams expect one to know this sort of stuff.

I absolutely hate "how many objects created" kind of questions, precisely because of these small surprises. I mean, even creation of a String instance (literal or not) usually involves creation of a char[] object also. Again, usually, and not always.
 
Sudhanshu Mishra
Ranch Hand
Posts: 238
1
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Aditya,
comforting answer.
But i just learned from a post that toString for String returns a reference to the string itself,so it means that inside the println method,i i pass string reference then it's toString should not be called,becuase if this happens,we again have a string object in the println,and then again the toString for string rference will be called?
Please guide me through.
 
Gaurav Raje
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java uses the string memory pool to try to assign the same object to different instances of the same string literal in order to save memory. While this is not guaranteed, it is most probably true in most cases. Hence, if("a" =="a") is true, though you shouldnt use it. String Interning will talk more about it.
 
Aditya Jha
Ranch Hand
Posts: 227
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sudhanshu Mishra wrote:Thanks Aditya,
toString for String returns a reference to the string itself,so it means that inside the println method,i i pass string reference then it's toString should not be called

That's right. For a String str, println(str) does not call println(str.toString()), as doing so will basically result in an infinite recursion leading to Stack Overflow.
 
Aditya Jha
Ranch Hand
Posts: 227
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gaurav Raje wrote:Java uses the string memory pool to try to assign the same object to different instances of the same string literal in order to save memory. While this is not guaranteed, it is most probably true in most cases

As far as I know, it is guaranteed that literals are put into a memory pool to be reused. Hence, ("a" == "a") is guaranteed to return true.
 
Gaurav Raje
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aditya Jha wrote:
Gaurav Raje wrote:Java uses the string memory pool to try to assign the same object to different instances of the same string literal in order to save memory. While this is not guaranteed, it is most probably true in most cases

As far as I know, it is guaranteed that literals are put into a memory pool to be reused. Hence, ("a" == "a") is guaranteed to return true.

As I pointed out it is equal. Just that it is not a good idea to use the comparison for other reasons ...viz not so careful programmers who can make mistakes by taking this for granted
 
Gaurav Raje
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the place where this is not guaranteed is when you use a new operator in any of the code when the JVM is forced to create a new object.
 
Aditya Jha
Ranch Hand
Posts: 227
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gaurav Raje wrote:the place where this is not guaranteed is when you use a new operator in any of the code when the JVM is forced to create a new object.

I suppose what you're saying is that in general String equality should not be deduced by using ==. This is correct. What I meant to say is that equality of 2 similar String literals is bound to give you consistent results.
 
Sudhanshu Mishra
Ranch Hand
Posts: 238
1
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aditya Jha wrote:
That's right. For a String str, println(str) does not call println(str.toString()), as doing so will basically result in an infinite recursion leading to Stack Overflow.


Thanks Aditya.
That's what i was expecting.
Just one more clarification----If i pass a Integer object in println,then i guess i have two objects created,one Integer and the other its string representation?
 
Gaurav Raje
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aditya Jha wrote:I suppose what you're saying is that in general String equality should not be deduced by using ==. This is correct. What I meant to say is that equality of 2 similar String literals is bound to give you consistent results.

My point is, string pool only applies to literals and not objects created using the string constructor.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote: . . . even more confusion when you come across this thread.
Mainly because I got it wrong, I think
 
Aditya Jha
Ranch Hand
Posts: 227
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gaurav Raje wrote:My point is, string pool only applies to literals and not objects created using the string constructor.

Of course. Unless you use intern() method on 'b'.

I think we're basically saying the same thing. While it is important to realize from the programming perspective to not to use == instead of equals() unless you're sure you absolutely want it, it is also important to know the reasons behind it, and the contract of overriding equals() in a custom class.
 
Aditya Jha
Ranch Hand
Posts: 227
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sudhanshu Mishra wrote:If i pass a Integer object in println,then i guess i have two objects created,one Integer and the other its string representation?

As I explained before, I don't want to get into 'how-many-objects-created' kind of questions, as technically you can go to any lengths and it's just crazy.

However, from a simpleton view, yes you can perceive it as 2 objects, and Integer and a String.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!