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...
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
So, whatever type 'user' is, its toString will be called
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Oops...never refresh my browser when typing:)
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
ASKER
thanks :)
:-)
>>public class Object
You wouldn't want to call it that of course ;-)
>>public class Object
You wouldn't want to call it that of course ;-)
Hi zizi21, thanks:)
Hi CEHJ, yea...that doesn't sound good:)
Hi CEHJ, yea...that doesn't sound good:)