Solved

Printing wrapper objects as strings

Posted on 2004-04-14
9
222 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.

729 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