Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wats the output..... Explanations needed....

 
Shiaber Shaam
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given:
1. public class Test {
2. public static String output ="";
3.
4. public static void foo(int i) {
5. try {
6. if(i==1) {
7. throw new Exception();
8. }
9. output += "1";
10. }
11. catch(Exception e) {
12. output += "2";
13. return;
14. }
15. finally {
16. output += "3";
17. }
18. output += "4";
19. }
20.
21. public static void main(String args[]) {
22. foo(0);
23. foo(1);
24.
25. }
26. }
What is the value of the variable output at line 23?
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
output = "134" before calling foo(1);
output = "13423" after calling foo(1);
 
Wally Valters
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouldn't you get a compile error? I thought you could'nt have code in between a try{} and catch{} block.

Or is it ok since all the code between try and catch in this instance is in the same scope?
[ June 16, 2006: Message edited by: Wally Valters ]
 
Swapnil Trivedi
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Wally, if u give a closer look to the code you will see that the statement is inside Try block. So there's no problem


Regards
swapnil
[ June 16, 2006: Message edited by: Swapnil Trivedi ]
 
srilatha rao
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

When foo(0) is executed, the output value is "134", since catch block is not executed.
When main method calls the foo(1) method, the output value continues with the "134" value, since output is static string variable.Only one copy of output is created.
When foo(1) is executed, the output value is "1342", since in the catch block, there is a return statement. 3 and 4 values cannot be appended to the output variable. Here, return statement stops the execution of finally and outside the finally block code and returns to the main method.

So, the value of output variable is "134" before calling foo(1) and "1342" after calling foo(1).

As per my knowledge, the above analysis is correct.
If there is any mistake, sorry for the reply.
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by srilatha rao:

So, the value of output variable is "134" before calling foo(1) and "1342" after calling foo(1).


What about finally. finally will always be executed even at second time.

Output given by Wise is correct.



Naseem
[ June 16, 2006: Message edited by: Naseem Khan ]
 
srilatha rao
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I am sorry, yes, the output is "13423".
finally will be executed in the second time also.
Thanks
 
S Thiyanesh
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
output is 13423
 
krish bajaj
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all!
tell me srilathawhy the statement afeter finally block not execute
i understand upto 13423 but why not 4 i mean why not 134234
please help me
 
Shiaber Shaam
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi bajaj...
if there occurs a return stmt in catch or finally block... the code in finally gets executed and the code after finally gets skipped.... I hope it will make sense....
 
krish bajaj
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello shiaber

just try this program without using return statement in catch block then after finally block statement execute easily
Do not use number simply use write statement for understanding
i did ur program and somewhere my concept is clear
if u like my answer then well and good otherwise sorry because i am new player in this field
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic