aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes toString() returns String ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "toString() returns String ?" Watch "toString() returns String ?" New topic
Author

toString() returns String ?

Raghu Shree
Ranch Hand

Joined: Mar 18, 2005
Posts: 143
public class ByteTest{
public static void main(String args[]){
Byte b1 = new Byte("127");
if(b1.toString()==b1.toString()) //line 4
System.out.println("True");
else
System.out.println("False");
}
}

b1.toString() returns 127. but line 4 returns false. I don't know why it is print like.
could any one explain me?


Raghu J<br />SCJP 1.4<br /> <br />The Wind and waters are always<br />on the side of the ablest navigators.<br /><a href="http://groups.yahoo.com/group/scjp_share" target="_blank" rel="nofollow">SCJP Group</a><br /><a href="http://groups.yahoo.com/group/JavaBeat_SCWCD" target="_blank" rel="nofollow">SCWCD Group</a>
Ram Naresh
Greenhorn

Joined: Mar 28, 2005
Posts: 11
This is what the Api has to say.
toString() Returns a String object representing this Byte's value.
So it returns a String Object, and to compare 'VALUES' of the two String objects use .equals(_), == compares the referance but .equals(_) compares values.
Ram
[ April 09, 2005: Message edited by: Ram Naresh ]
Raghu Shree
Ranch Hand

Joined: Mar 18, 2005
Posts: 143
Hi Ram,
I know == compare the two object reference. But this is not my question. I ask why b1.toString() returns different reference.
Ram Naresh
Greenhorn

Joined: Mar 28, 2005
Posts: 11
Raghu,

I think this is the answer. Only "StringVar.toString()" returns "this", other wise all toString() s return new String Object.

Ram.
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
hi ram
are u saying that the toString method will always return the new string object.i.e invoking the method twice by the same reference variable will result in the creation of the two new object.
if so is that will return only the string values of the reference variable.
levani dvalishvili
Ranch Hand

Joined: Mar 01, 2005
Posts: 99
default return of toString() is objectName@HashCode, unless overvriden
but I am thinking it should still return true comparing, since Hashcode should be same.


SCJP 1.5(Done) SCJA 1.0(Done)<br />SCWCD(in Progress...)
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
---------------------------------------------
public class ByteTest
{
public static void main(String args[]){
Byte b1 = new Byte("127");
if(b1.toString()==b1.toString()) //line 4
System.out.println("True");
else
System.out.println("False");
}
}

b1.toString() returns 127. but line 4 returns false. I don't know why it is print like. could any one explain me?

---------------------------------------
The return type of toString() is String.
whenever a String is return, A new String object in created in heap.
In our case it return 2 String objects with value 127, so when we comparing references,normally it will be false.
levani dvalishvili
Ranch Hand

Joined: Mar 01, 2005
Posts: 99
since String object is immutable everytime you call toString() method it will create a different String object, even if values are the same, so b1.toString() will create different String objects with same value but different object reference over and over again. and since == checks object reference equality, it will not be same
that is what I think
[ April 10, 2005: Message edited by: levani dvalishvili ]
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
To confirm what levani says.
The source for Byte.toString()


String.valueOf() ends up calling Integer.toString(int, int) which creates a new String object.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[levani]: since String object is immutable everytime you call toString() method it will create a different String object, even if values are the same

Well, it's true that there is a new String created here, as Steven has shown. But the reasoning here is incorrect - the fact that String is immutable does not require that a new String be created each time. In fact, it makes it easier to cache and reuse objects, if desired. It would have been perfectly possible for Sun to implement Byte's toString() in such a way that it reused String objects. Some classes do this sort of thing; most don't. In general the exam will not ask you questions that depend on this, because it's impossible to determine unless you view the source code for the class under discussion. The API does not (in general) say anything about whether the String to be returned will be new or a reused object; that's deliberately left unspecified because it really shouldn't matter.


"I'm not back." - Bill Harding, Twister
Raghu Shree
Ranch Hand

Joined: Mar 18, 2005
Posts: 143
Hi
Thanks for your response. I am finally come to conculsion of

since String object is immutable everytime you call toString() method it will create a different String object, even if values are the same, so b1.toString() will create different String objects with same value but different object reference over and over again.


could I anything misunderstand
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Hi Raghu,

Wrapper classes like Byte, Integer and so forth are immutable, i.e. its value (e.g. 127) cannot be modified once it's set. However, for some reasons, Sun developers decided to return a new String object whenever b1.toString() is called even though it's possible for Sun, as Jim explains, to cache and reuse the same String object. The conclusion of whether to return a new or reused String object when b1.toString is called is up to the Sun developers to decide, the API doc for Byte's toString does not specifically mention it'll return the same String object or not.

Joyce
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
In fact, for JDK 1.4.2, the following code returns true for values between -3 and 10,



This issue was recently discussed in this thread.

Joyce
levani dvalishvili
Ranch Hand

Joined: Mar 01, 2005
Posts: 99
I think that overall for small reasons caching is a bad idea I agreee with sun on not caching. here is what I think
for example

String s1 = "hello";
String s2 = "hello";
String s3 = "hello";

my question is should all these strings be using one "hello" or should they be three different objects? if answer is no than its a overhead of JVm before creating an object to make sure that object with that specific value does not already exists .....
same thing with altering a Byte to cache the String...
this is just what I think
very truly
levani
Jack Gold
Ranch Hand

Joined: Feb 04, 2005
Posts: 85
Originally posted by levani dvalishvili:
I think that overall for small reasons caching is a bad idea I agreee with sun on not caching. here is what I think
for example

String s1 = "hello";
String s2 = "hello";
String s3 = "hello";

my question is should all these strings be using one "hello" or should they be three different objects? if answer is no than its a overhead of JVm before creating an object to make sure that object with that specific value does not already exists .....
same thing with altering a Byte to cache the String...
this is just what I think
very truly
levani

When created using the method you describe, they are created in the string pool and will satisy both == and equals().

If you used String s = new String("abcd"); syntax, the strings would be created on the heap and would not satisfy ==.


SCJP 1.4<br />SCJD <br />SCWCD (Studying)
levani dvalishvili
Ranch Hand

Joined: Mar 01, 2005
Posts: 99
I agree.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: toString() returns String ?
 
Similar Threads
toString()
toString Question
toString( ) method
strange results
comparison