missing return statement compile error

Discussion in 'Java' started by Matt, Jul 17, 2004.

  1. Matt

    Matt Guest

    In case1, it has the "missing return statement" compile error. But if
    I do case 2 or case 3. Then no more compile errors.

    What's the rationale? please advise. thanks!!

    //case 1
    public String returnTest(String s)
    {
    //compile error: missing return statement
    if (s.equals("test"))
    return "test";
    }

    //case 2
    public String returnTest(String s)
    {
    if (s.equals("test"))
    return "test";
    else
    return "non-test";
    }

    //case 3
    public String returnTest(String s)
    {
    if (s.equals("test"))
    return "test";
    return "non-test";
    }
     
    Matt, Jul 17, 2004
    #1
    1. Advertising

  2. Matt

    Woebegone Guest

    "Matt" <> wrote in message
    news:...
    > In case1, it has the "missing return statement" compile error. But if
    > I do case 2 or case 3. Then no more compile errors.
    >
    > What's the rationale? please advise. thanks!!
    >
    > //case 1
    > public String returnTest(String s)
    > {
    > //compile error: missing return statement
    > if (s.equals("test"))
    > return "test";
    > }
    >
    > //case 2
    > public String returnTest(String s)
    > {
    > if (s.equals("test"))
    > return "test";
    > else
    > return "non-test";
    > }
    >
    > //case 3
    > public String returnTest(String s)
    > {
    > if (s.equals("test"))
    > return "test";
    > return "non-test";
    > }


    In case (1), no return statement exists for the case !s.equals("test"), so
    not all potential paths are covered. As written, the other two cases are
    equivalent. The rationale would be something along the lines of "a method
    declared to return a value of type T must return a value of type T."

    HTH,
    Sean.
     
    Woebegone, Jul 17, 2004
    #2
    1. Advertising

  3. Matt

    Roedy Green Guest

    On 16 Jul 2004 21:23:17 -0700, (Matt) wrote or
    quoted :

    >//case 1
    >public String returnTest(String s)
    >{
    > //compile error: missing return statement
    > if (s.equals("test"))
    > return "test";
    >}


    what value is it supposed to return if s is not equal test? You have
    not specified.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Jul 17, 2004
    #3
  4. (Matt) wrote in
    news::

    > public String returnTest(String s)
    > {
    > //compile error: missing return statement
    > if (s.equals("test"))
    > return "test";
    > }


    AClass a = new AClass();
    String x = a.returnTest("foo");
    // in your opinion, what the value of x should be?


    > public String returnTest(String s)
    > {
    > if (s.equals("test"))
    > return "test";
    > else
    > return "non-test";
    > }

    String y = a.returnTest("foo");
    // now y is "non-test"

    > public String returnTest(String s)
    > {
    > if (s.equals("test"))
    > return "test";
    > return "non-test";
    > }

    String y = a.returnTest("foo");
    // now y is "non-test"

    Do you see the difference now???

    If you don't, you are up shit creek
    [from _The Blues Brothers_]

    Andrea

    --
    Andrea Spinelli - IT&T srl
    Via Sigismondi, 40 - 24018 Villa d'Alme' (BG)
    tel: +39+035636029 - fax: +39+035638129
    http://www.imteam.it/
     
    Andrea Spinelli, Jul 21, 2004
    #4
  5. Matt

    Shishir19

    Joined:
    Mar 19, 2007
    Messages:
    1
    hello everyone. I m new to java and specially to EJB.was working on a piece of code but i m getting the same missing return statement error.can anyone please help


    public String ins_Deposit(String userid,int amount,String branch,String bank) throws SQLException
    {
    Statement ps = null;
    try
    {
    ps = conn.createStatement();
    String query="INSERT INTO BANK_TRANSACT VALUES('"+userid+"',sys_date,"+amount+",'D','"+branch+"','"+bank+"')";
    int result = ps.executeUpdate(query);
    if(result>0)
    return "Transaction successfully completed,amount will be credited to your account within 24hrs. ";
    else
    return "Bank timings are between 8 AM to 8 PM IST,Record not inserted";
    }
    catch(SQLException e)
    {
    System.out.println("Unable to get Data: " + e);
    }
    }
     
    Shishir19, Mar 19, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Seong-Kook Shin
    Replies:
    1
    Views:
    495
    Richard Bos
    Jun 18, 2004
  2. Nagaraj
    Replies:
    1
    Views:
    872
    Lionel B
    Mar 1, 2007
  3. macracan
    Replies:
    3
    Views:
    609
    Jack Klein
    Feb 8, 2008
  4. sweety5236

    Java - missing return statement..

    sweety5236, Apr 9, 2008, in forum: Java
    Replies:
    0
    Views:
    736
    sweety5236
    Apr 9, 2008
  5. X X
    Replies:
    4
    Views:
    358
    red floyd
    Jul 19, 2010
Loading...

Share This Page