• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

finally doubt

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23923
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank u
 
vivekkumar sharma
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jiju Ka and Henery,

Thanks for very good explanation
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But we have assigned the modified string to output reference variable in finally block.so why doesnt work?
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic