Results 1 to 9 of 9
- 08-11-2012, 01:54 PM #1
Member
- Join Date
- Aug 2012
- Posts
- 4
- Rep Power
- 0
Question about a program related to a boolean return and clips statements.
Hi, i have a question about the following program:
public boolean strCvt(String s)
{
try {
factor = Float.valueOf(s).floatValue();
return true;
}
catch (NumberFormatException e) {
System.out.println(“Bad number ” + s);
factor = Float.NaN;
}
finally {
System.out.println(“Finally”);
}
return false;
}
it seems that the following answers are true:
(a) Input = “0.234” – Result: factor = 0.234, “Finally” is printed, true is returned.
(d) Input = null – Result: factor = unchanged, “Finally” is printed, NullPointerException is thrown.
about (a);
I have a question, if true is returned the function continue anyway until "finally" printed? The function does not stop when a return is made?i ask this because the true return is before the "finally" printed.
Another question, if the function continue until "finally" printed, how possible it return true if the <return false> is immediatly after "finally" printed?
Thanks in advance for any answers..
R
- 08-11-2012, 02:05 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,589
- Blog Entries
- 7
- Rep Power
- 17
Re: Question about a program related to a boolean return and clips statements.
The finally block is always executed last, e.g. it executes after an exception was caught and handled or before your method wanted to finish its try block (either with a return statement or when your try block wanted to fall through).
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 08-11-2012, 02:17 PM #3
Re: Question about a program related to a boolean return and clips statements.
Why do they call it rush hour when nothing moves? - Robin Williams
- 08-11-2012, 02:33 PM #4
Member
- Join Date
- Aug 2012
- Posts
- 4
- Rep Power
- 0
Re: Question about a program related to a boolean return and clips statements.
Thank you Jos and Darry.
I undestand that finally is always executed. Are "try" and "finally" semantic keywords like commands? Are only in java?
The <return false> is not executed becuse the <return true> executed before indicates that the compilation have to stop (even if finally is forced?)?
Kind regards
R
- 08-11-2012, 02:38 PM #5
Re: Question about a program related to a boolean return and clips statements.
Java doesn't have commands. Java has statements. You can find a complete list of all Java keywords here: Chapter*3.*Lexical Structure
Huh?
Compiling and executing (or running) are two separate activities. Don't confuse them.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 08-11-2012, 02:56 PM #6
Member
- Join Date
- Aug 2012
- Posts
- 4
- Rep Power
- 0
Re: Question about a program related to a boolean return and clips statements.
Ok thanks i have found "try2 and "finally" in lexical structure, then is like i believed.
Confirm me, why <return false> is never executed?
Have java compiling process?
Thanks in advance
R
- 08-11-2012, 05:07 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,589
- Blog Entries
- 7
- Rep Power
- 17
Re: Question about a program related to a boolean return and clips statements.
But in your code it can be executed: when an exception has been thrown (so that the return in the try block wasn't reached) and the catch block as well as the finally block have executed (and none have returned), the rest of your code is executed.
I don't know what you mean by that.Have java compiling process?
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 08-11-2012, 05:49 PM #8
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
- 08-11-2012, 07:18 PM #9
Member
- Join Date
- Aug 2012
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Class and return statements question, not updating correctly
By killmenow in forum EclipseReplies: 2Last Post: 05-21-2012, 10:59 AM -
Return Boolean
By FadedAura in forum New To JavaReplies: 2Last Post: 11-20-2011, 10:51 PM -
help with recursive return boolean
By Yakg in forum New To JavaReplies: 11Last Post: 01-11-2011, 05:54 PM -
Return Statement and Boolean Help
By GhostShaman in forum New To JavaReplies: 8Last Post: 03-09-2010, 11:15 AM -
Boolean Return Value
By devstarter in forum New To JavaReplies: 5Last Post: 03-02-2010, 07:45 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks