• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Printing order when there is a return statement

 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, I think I still have problems understanding the order in which statements are executed when there is a return and a print statement. Let me give you an example (taken from Glenn, Mitchell. OCAJP Oracle Certified Associate Java SE 8 Programmer Practice Exams (Kindle Locations 11106-11116). Enthuware. Kindle Edition. ):


this prints 111333333 but I don't quite get why the numbers are printing in that order
Method parse returns value, whose value is 333. Global, inside the parse method but outside the try statement is 111. So obviously prints 111333, but there is an extra 333 which is probably printed because of the return statement in parse, but I don't understand the order. Perhaps there could be a simpler example, so that I understand the principle?
thanks
 
Narayana Bojja
Ranch Hand
Posts: 130
4
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jason,



Here , In this statement there is a method call , so this  statement doesn't print anything till parse method returns some value. Here your passing 333 in string format to parse method.


This statement  prints first . Here  global is 111(because global in try block is out of scope at this time) and value is 333.So,  It prints 111 333 . It keeps the cursor in the same line because you have used print method (It keeps the cursor in the same line after printing something).

Next this statement will be executed, it returns 333 to the calling method .

Finally this statement will be executed because parse method returned value. It prints 333 . So the final output is 111 333 333. Observe whole output is in same line because you have used print method.

Hope it helps !
 
Ganesh Patekar
Bartender
Posts: 694
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you notice parsing String " 333"  ?
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Narayana Bojja, that is now crystal clear!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic