• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Wrapper class and toString()

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello.
I have a question about the toString() method in the Wrapper class but I found this following section of code a little puzzling. can anyone help please?
//in the main method
Byte x1 = new Byte("127");
boolean b0 = x1.toString()==x1.toString();
System.out.println(b0);
Boolean b1 = new Boolean("true");
boolean b2 = b1.toString()==b1.toString();
System.out.println(b2);
//
when you run this, b0 will be false and b2 will be true. why? is there a difference between Byte and Boolean's toString() methods' return types or what? please help.
thanks a lot
cw
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chun,
The only reason I can think of is the difference in how Boolean and other wrapper classes are implemented.
Here you're comparing the handles (==) of the String returned from the toString method. I'd guess that because for the Boolean class, the only 2 values they can return are either "true" or "false", it doesn't need to create a new String when the toString method is called, while the other wrapper classes do.

Class Boolean is similar to class A in their toString() methods: return literal String that is placed in the string pool and reused later.
Please look at the code above and let me know if you think I'm right.
Hungson Le
[This message has been edited by Son Le (edited January 24, 2001).]
 
Chun Wang
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Hungson.
that was my guess too. I just wonder if anyone out there knows exactly what was happening. but yeah, I think Boolean's toString() is just different from all other Wrapper's toString().
thanks for your reply.
chun
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Wonder no more. Here is the code:

So from the above code, we can see that the Boolean toString will return a literal string which the compiler will place on the string stack while Byte will create new memory location.
Regards,
Manfred.
 
Son Le
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Manfred,
Thanks for your reply. I've been looking for more info about how certain Java API classes are implemented. I'd appreciate it if you can give me a link to those kinds of docs.
Thanks again,
Hungson Le
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is what the API(jdk1.2.2) says about <Boolean>toString():
---
Returns a String object representing this Boolean's value. If this object represents the value true, a string equal to "true" is returned. Otherwise, a string equal to "false" is returned.
---
1.
It seems like that 'equal to' means '=='.
2.
Running the following code
Boolean b1 = new Boolean("true");
System.out.println("Boolean b1 = new Boolean(\"true\");");
System.out.println("(b1.toString() == \"true\"): "
+ (b1.toString() == "true"));
results (under jdk1.2.2):
Boolean b1 = new Boolean("true");
(b1.toString() == "true"): true
3. A Java program uses a single String object for all ocurrences of the literal "true".

 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Son,
If you want to see what the actual code for an API is you can do the following:
To extract source code for the Java Class files, check your JDK directory for a src.jar file. In the same directory, enter
jar tf src.jar > srcList.txt
This will create a text file listing all the .java files in the src.jar file. View the text file to locate the path name of the class you're interested in and then type:
jar xf src.jar file pathname

For example, to extract the Reader.java file
jar xf src.jar src/java/io/Reader.java
Hope that helps.
------------------
Jane Griscti
Sun Certified Java 2 Programmer
"When ideas fail, words come in very handy" -- Goethe
 
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
An easy way to see the source code is to use an IDE. I like JBuilder 4. All you have to do is create any java class and have import statements at the top. To see java classes import java packages. JBuilder will automatically bring them into a screen into the lower left corner where you can navigate packages and choose the class you want to see. Then you just double click on it and look at the actual code.
You can get the free version at: http://www.borland.com/jbuilder/foundation/
cheerios,
Yoo-Jin.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Jane,
can you explain me this, if two separate calls to x1.toString()is creating two different objects but i have checked their hashcodes and to my surprise they are same; how can two different objects have same hashcode
please explain....
mustafa
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mohammed,
The <code>hashCode()</code> method in the String class overrides <code>Object.hashCode()</code>
The hash code of a String object is based on the contents of the String; any two strings with the same Unicode values will always return the same hash code even if they are in different Objects.
This is why you can get the same hash code for two different String objects.
Hope that helps.
------------------
Jane Griscti
Sun Certified Java 2 Programmer
"When ideas fail, words come in very handy" -- Goethe
[This message has been edited by Jane Griscti (edited January 25, 2001).]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic