Link to home
Start Free TrialLog in
Avatar of zizi21
zizi21

asked on

toString

Hi,

I have a loop  like below where the name is printed, I dont need to typecast it or even use toString.

  I find this strange because,it is an object, we need to typecast, is automatic typecasting done for System.oout.println()??
 
 // accounts map
         for (Object user: users)
         {

            // get the value (quota) corresponding to the current
            // key (name) from the quota map

            Object quota = quotaMap.get(user);

            // print the key (name) - note that the typecase is optional
            System.out.print("Name: " + (String)user);

thanks...
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So, whatever type 'user' is, its toString will be called
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of william007
william007

Oops...never refresh my browser when typing:)
Avatar of zizi21

ASKER

basically, every object would have a toString that automatically converts to string ? sorry if this is obvious. just wish to reconfirm ..studying for exams..
Yes. Since everything is a child of Object and Object defines toString
Avatar of zizi21

ASKER

thanks :)
:-)

>>public class Object

You wouldn't want to call it that of course ;-)
Hi zizi21, thanks:)

Hi CEHJ, yea...that doesn't sound good:)