Solved

Printing wrapper objects as strings

Posted on 2004-04-14
9
220 Views
Last Modified: 2010-03-31
Hi:
    The following program prints obj1 and obj as string equivalents. How does the program accomplish this task? What implied class and methods are used?

class DisplayString2
{  public static void main(String [] args)
   {  Boolean obj1 = new Boolean("false");
      System.out.println("Boolean equals " + obj1);

     Integer obj2 = new Integer("3");
     System.out.println("Integer is equal to  " + obj2);
           
     
     
   }  

}
0
Comment
Question by:LearningJava
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 10822486
This uses overloaded methods in the PrintStream class that causes the object's toString method to be called, thus printing a String
0
 
LVL 9

Expert Comment

by:Nick_72
ID: 10822488
Every object has a toString() method which is used when having to transform an object to a String.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10822496
See

http://java.sun.com/j2se/1.5.0/docs/api/java/io/PrintStream.html

It's partly for that reason that any well-written class has a toString method, allowing this to happen
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10822542
toString() method is at Object class level.

All the objects are extended from Object.  So whenever it needs to be printed, the toString() method would be get called automatically.


Regards,
Muruga
0
 
LVL 9

Assisted Solution

by:mmuruganandam
mmuruganandam earned 250 total points
ID: 10822562
if you want to have your own implementation for toString() method,

then you need to override the method.

say

class A
{
        int b = 10;
        int c = 20;

        ----
        ----

       public String toString()
       {
            StringBuffer buffer = new StringBuffer();
            buffer.append("b = ");
            buffer.append(b);
            buffer.append("And c = ");
            buffer.append(c);

            return buffer.toString();
       }
}

So that whenever you call toString() method, b = value And c = value will be get printed.

0
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 10822584
You can tell the difference produced by a proper implementation of toString, as opposed to the default (in Object) by compiling and running this:

public class PrintMe {

      protected int someVal;

      public PrintMe() {
            someVal = 10;
      }

      public static void main(String[] args) {
            PrintMe pm1 = new PrintMe();
            PrintMe pm2 = new PrintMe2();
            System.out.println("pm1: " + pm1);
            System.out.println("pm2: " + pm2);

      }

}

class PrintMe2 extends PrintMe {
      public String toString() {
            return "someVal=" + someVal;
      }
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10822596
(PrintMe uses the superclass' [Object] toString method, PrintMe2 overrides this meaningfully)
0
 

Author Comment

by:LearningJava
ID: 10822632
Thanks.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10822644
8-)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
configure dependency in POM for new database 3 43
jboss wildfly 10.1 10 405
junit as external jar or library 7 37
import as existing maven project 3 34
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question