Solved

Printing wrapper objects as strings

Posted on 2004-04-14
9
217 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What is the latest versions eclipse neon 2 139
Java JRE greater than 1.6 5 49
how to add new optional parameter to JSP 1 26
table example 4 20
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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…

930 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

12 Experts available now in Live!

Get 1:1 Help Now