I solved the task so i don't want to post the correct code.
I stillt don't get the output.
The output is like:
Tiger - TIGER
Missy - MISSY
(..)
Why isn't the output something like this:
Missy - com.example.übung.Übung$Cat@681a9515
Tiger - com.example.übung.Übung$Cat@3af49f1c
(...)
I understand that the override method turns the name into uppercase letters, but not why the object description changes.
the class looks like this :
(
public static class Cat {
String name;
public Cat(String name) {
this.name = name;
}
@Override
public String toString() {
return name != null ? name.toUpperCase() : null;
}
}
the output is generated like this:
or (Map.Entry<String, Cat> pair : map.entrySet()) {
System.out.println(pair.getKey() + " - " + pair.getValue());
}
Maybe someone can help to understand. Thanks in advance.
Don't understand output.
Resolved
Comments (3)
- Popular
- New
- Old
You must be signed in to leave a comment
Gellert Varga
15 January 2021, 16:42solution
Because of: if you put an object inside the Sys.o.println(cat1) command then the following will happen:
behind the scenes, and automatically, the system will call the toString method on the cat1.
(The original toString() method, inherited from Object class.
So, you can run on cat1 such a method.
It returns the memory address of the object, like this: Übung$Cat@681a9515, and after prints it.).
But here in the Cat class you have an overriden toString() method, so the System will apply this:
if this.name doesn't equal to null then return this.name with converting to uppercase.
if this.name equals to null then returns null +6
Jurij Thmsn
16 January 2021, 08:53
Thank you, Gellert! now it really makes sense for me :)
+1
Gellert Varga
16 January 2021, 23:54
:)
+1