Solved

Printing wrapper objects as strings

Posted on 2004-04-14
9
221 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Technology Partners: 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
login jsp example 24 101
throw exception 21 75
jmeter usage 4 56
JDBC performance 8 32
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…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

738 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