Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By rdeastis

Thread: Question about a program related to a boolean return and clips statements.

  1. #1
    rdeastis is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    Question 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

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,019
    Blog Entries
    7
    Rep Power
    20

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Question about a program related to a boolean return and clips statements.

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    rdeastis is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    Default 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

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Question about a program related to a boolean return and clips statements.

    Quote Originally Posted by rdeastis View Post
    Are "try" and "finally" semantic keywords like commands?
    Java doesn't have commands. Java has statements. You can find a complete list of all Java keywords here: Chapter*3.*Lexical Structure

    Quote Originally Posted by rdeastis View Post
    Are only in java?
    Huh?

    Quote Originally Posted by rdeastis View Post
    The <return false> is not executed becuse the <return true> executed before indicates that the compilation have to stop (even if finally is forced?)?
    Compiling and executing (or running) are two separate activities. Don't confuse them.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    rdeastis is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    Default 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

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,019
    Blog Entries
    7
    Rep Power
    20

    Default Re: Question about a program related to a boolean return and clips statements.

    Quote Originally Posted by rdeastis View Post
    Ok thanks i have found "try2 and "finally" in lexical structure, then is like i believed.

    Confirm me, why <return false> is never executed?
    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.

    Have java compiling process?
    I don't know what you mean by that.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    2

    Default Re: Question about a program related to a boolean return and clips statements.

    Quote Originally Posted by rdeastis View Post
    Have java compiling process?
    Yes, Java programs are compiled into class files.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  9. #9
    rdeastis is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    Default Re: Question about a program related to a boolean return and clips statements.

    Quote Originally Posted by awinston View Post
    Yes, Java programs are compiled into class files.
    I would like thank you all for the teaching
    R
    awinston likes this.

Similar Threads

  1. Replies: 2
    Last Post: 05-21-2012, 10:59 AM
  2. Return Boolean
    By FadedAura in forum New To Java
    Replies: 2
    Last Post: 11-20-2011, 10:51 PM
  3. help with recursive return boolean
    By Yakg in forum New To Java
    Replies: 11
    Last Post: 01-11-2011, 05:54 PM
  4. Return Statement and Boolean Help
    By GhostShaman in forum New To Java
    Replies: 8
    Last Post: 03-09-2010, 11:15 AM
  5. Boolean Return Value
    By devstarter in forum New To Java
    Replies: 5
    Last Post: 03-02-2010, 07:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •