This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String

 
cyril vidal
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,
I submit you something I find quite intersting, about String, toString, ...

As you may expect, the result is something of the following form:
AAAZ@1ea2dfe.
Indeed, System.out.println invokes automatically the toString() method inherited from Object on instance Z, which prints out the name of the object's class + some identifying value - typically its reference value, separated by iy the at symbol.
What's happening now with this code?

Output:
Z
AAAZ1ea2dfe
Waht is worth noting here is that "Z" is printed out on its own line before the toString() method would be happened to AAA on its own line also.
The same logic applies in the following case, when overriding the toString method:

output:
###
AAAZ
Hoe this can help,
Cyril.
 
Miki Muzsi
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed, and this is beacuse first the operands are evaluated (BOTH of them) and then the operations is completed. In Both examples evaluating the right operand, means to print "Z" or "###".
Miki
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic