Override a method toString() in your MyOwnClass like this
public String toString() {
return name + ":" + age;
}
and run the program. You will observe that this time it will print
Paris 19
Why ?
It is because whenever you print object in java. It prints whatever is
present in toString() method.
Since you have toString() in your class it uses the default toString()
from object class. Checkout
what is written in toString() of Object class to learn more details.
Since Integer class already has
toString() method so it prints out 10 instead of what is present in
toString() method of Object class.
On Nov 15, 6:45 pm, "Agung Prasetyo" <[EMAIL PROTECTED]> wrote:
> Hi, the java master, i have the class like this :
> public class MyOwnClass
> {
> String name;
> int age;
>
> public MyOwnClass(String name,int age)
> {
> this.name = name;
> this.age = age;
> }
>
> }
>
> And i add this class instance object to the HashSet object --> s
> s.add(new MyOwnClass("Paris",19));
>
> when the s object is print using iterator, why the output show like this :
> [EMAIL PROTECTED]
>
> please help me ! i'm very confuse the following code.
> why when i add the object Integer ex : s.add(new Integer(8));
> why the output is 8
> Thanks very much.
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---