Solved

Printing wrapper objects as strings

Posted on 2004-04-14
9
216 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 9

Expert Comment

by:mmuruganandam
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 9

Assisted Solution

by:mmuruganandam
mmuruganandam earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
(PrintMe uses the superclass' [Object] toString method, PrintMe2 overrides this meaningfully)
0
 

Author Comment

by:LearningJava
Comment Utility
Thanks.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
8-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

771 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now