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

finally doubt

 
xie li
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


the outputis

b
b
bab
ba
who can explain why?

Edited by Corey McGlone: Added CODE Tags
[ January 03, 2006: Message edited by: Corey McGlone ]
 
brij tiwari
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the first call with foo(0), the finally block prints the output value then returns the output value which is then printed by the system.out call in main method.so value 'b' is printed two times.

For second call with foo(1),the catch block make output as 'ba' and returns 'ba' to main method, but before returning finally block is called always...so before printing the returned value i.e. 'ba' first finally block prints the 'bab' value...after that since the return value is 'ba' only thats y main prints 'ba'

so the sequence is
b
b
bab
ba
 
vivekkumar sharma
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by brij tiwari:
For the first call with foo(0), the finally block prints the output value then returns the output value which is then printed by the system.out call in main method.so value 'b' is printed two times.

For second call with foo(1),the catch block make output as 'ba' and returns 'ba' to main method, but before returning finally block is called always...so before printing the returned value i.e. 'ba' first finally block prints the 'bab' value...after that since the return value is 'ba' only thats y main prints 'ba'

so the sequence is
b
b
bab
ba


Why the modification done to output in finally gets lost?
I mean why output is not
b
b
bab
bab
 
Henry Wong
author
Marshal
Pie
Posts: 21367
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why the modification done to output in finally gets lost?
I mean why output is not
b
b
bab
bab


The code in the finally clause runs *last* !! It runs after the return value of the method has already been calculated.

Henry
 
jiju ka
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Why the modification done to output in finally gets lost?

Excellent question. Answer : Because output which is of type String is immutable.

See the code below. Just replaced String with Stringbuffer which is mutable.



Here you will get the following output

b
b
bab
bab


In both code snippets return happens first; then the finally executes.

In the first code snippet the instance of the String referred by variable output changed with each '+' operation.
In the second code snippet the instance of the String referred by variable output didn't change with append.

In the first code snippet, the object referenced by output contained ba and that refrence is returned. Then when finally executed a new String instance is created (because String is immutable) and value of Output is changed to refer to new instance. Hence the print statement from finally had the most recent instance which contained 'bab' and the print statement from main had the second most recent instance which contained 'ba'.

Please let me know if this explanation make sense.
 
xie li
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank u
 
vivekkumar sharma
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jiju Ka and Henery,

Thanks for very good explanation
 
Vikas Roy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,

I have one doubt about the given explanation.
How come the catch block construct the String as " ba", actually it is only appending "a" in output object.
I have not tried the code, but according to me the output should be " ab" becouse whenever try executes and throws some exception, catch block executes before finally block.

Please do correct me or explain me.

Regards,
 
sivaram omprakash
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But we have assigned the modified string to output reference variable in finally block.so why doesnt work?
 
venkataramana raju
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vikas Roy ,

quote:
----------------------------------------------------------------
Originally posted by Vikas Roy :

How come the catch block construct the String as " ba", actually it is only appending "a" in output object.

----------------------------------------------------------------
why because output variable is static (class variable)
 
Aleksander Zielinski
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vikas Roy:
Hi Ranchers,

I have one doubt about the given explanation.
How come the catch block construct the String as " ba", actually it is only appending "a" in output object.
I have not tried the code, but according to me the output should be " ab" becouse whenever try executes and throws some exception, catch block executes before finally block.

Please do correct me or explain me.

Regards,


Field output has a static modifier, after first invoking of method foo() String output is set to "b", catch block add "a" and finally block adds "b", so output is "bab".
 
jiju ka
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vikas Roy ,
Your question

How come the catch block construct the String as " ba", actually it is only appending "a" in output object.


Is answered below.
From the method main the method foo is called twice. In the first call '0' is passed as argument which will not get into exception. When finally executes variable output gets a value b. In the second call to foo '1' is passed as argument which will get into an exception. Since output is static variable both calls to foo is accessing the same string referenced by output. When catch executes "a" is appended to output which already have "b". Try debugging this program using eclipse, netbeans, etc.. for more details
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic