• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

toString()........

 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
source: http://www.geocities.com/skmajji/Main.html


The following code will give



A) Compilation error, toString() is not avialable for Byte.
B) Prints "True".
C) Prints "False".

my answer is B

but answer is C)

toString() method returns the object which is already exists

so here object for Byte is returned

== operator is used tocheck whether both are pointing the same object or not..

why the answer is C

can anyone explain?
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Really good question Ganesh

I got the answer.
It depends on Byte.toString() method.


public String toString() {
return String.valueOf(value);
}



It calls String.valueOf(value) method.... that is:

public static String valueOf(int i) {
return Integer.toString(i, 10);
}



and String.valueOf(int) calls Integer.toString(i,10); that is:

public static String toString(int i, int radix) {


/* Use the faster version */
if (radix == 10) {
return toString(i);
}
....



and finally Integer.toString(int)

public static String toString(int i) {
if (i == Integer.MIN_VALUE)
return "-2147483648";
int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
char[] buf = new char[size];
getChars(i, size, buf);
return new String(0, size, buf);
}



and you can see it is returning new String(0, size, buf);.

Now you know everything.
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Edited : Punit explanation clears all doubt, He actually post the implementation of Of Byte#toString() object !! Great work !]

Originally posted by Ganeshkumar cheekati:


toString() method returns the object which is already exists



Nope, toString() returns an meaningful string about an object .
[See Java Docs, for toString() default implementation]

Originally posted by Ganeshkumar cheekati:


so here object for Byte is returned



No, Its return the String object..

Originally posted by Ganeshkumar cheekati:


== operator is used tocheck whether both are pointing the same object or not..

why the answer is C



Right, so as both returned string represent the same object, b1, they print "True"..

[== operator checks for equality of object reference, byte by byte , and it returns String from String Constant Pool]

HTH,
[ December 19, 2008: Message edited by: Sagar Rohankar ]
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi punit...

Is this happens only for Byte wrapper class or all of the wrapper classes?
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It happens for all wrapper except Boolean.
For boolean


public String toString() {
return value ? "true" : "false";//it is not creating new String();
}



so



so for boolean it will return true.
 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Punit, Can you please tell me from where did you get this information about toString()?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Abhi vijay:
Punit, Can you please tell me from where did you get this information about toString()?



The source code for the Java classes are available in the top level directory of the JDK home, in a file called src.zip.

Henry
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Henry.
 
reply
    Bookmark Topic Watch Topic
  • New Topic