Multiple return statements in Java

0 votes

I have a code which contains multiple returns statements. I want to know, does the language specifications define the return value of a call to any function containing multiple returns. In my system it's returning 2, is it same for all the JVMs or is it JVM dependent.

My code is:

String demo() {
    try {
        return "1";
    } finally {
        return "2";
    }
}

Jun 27, 2018 in Java by anto.trigg4
• 3,420 points
65 views

1 answer to this question.

0 votes

Yes, you have guessed it right. Language specifications do define the result of the multiple return statement. If your virtual machine does it differently, then it's not a spec-compliant.

Generally, when you try to execute this program, compilers will throw an exception, but still, you can execute it. But, this is against the norms of coding standards, so I would suggest you refrain such kind of coding practice.

answered Jun 27, 2018 by geek.erkami
• 2,320 points

Related Questions In Java

0 votes
1 answer

Multiple return types in Java

No, you don't have two return types.It's ...READ MORE

answered Feb 8 in Java by developer_1
• 3,300 points
25 views
0 votes
1 answer

I am learning looping statements. Can you tell me how 'for-each' works in Java?

While programming we often write code that ...READ MORE

answered Apr 17, 2018 in Java by Rishabh
• 3,540 points
52 views
0 votes
2 answers

Can we catch multiple exceptions in the same catch clause in Java?

In Java 6 and before versions, we ...READ MORE

answered Aug 10, 2018 in Java by sharth
• 3,320 points
136 views
0 votes
2 answers

How to return multiple objects from a Java method?

In the event the method you're calling ...READ MORE

answered Aug 30 in Java by Karan
• 1,780 points
102 views
0 votes
1 answer

What is ArrayIndexOutOfBoundsException ?

If You check System.out.print(names.length); you will get 3 ...READ MORE

answered May 11, 2018 in Java by sharth
• 3,320 points
72 views
0 votes
2 answers

How can I solve java.lang.NoClassDefFoundError in Java?

NoClassDefFoundError means that the class is present ...READ MORE

answered Sep 11, 2018 in Java by Sushmita
• 6,880 points
999 views
0 votes
1 answer

How can we resolve ClassNotFoundException in Java?

Your classpath is broken. Depending on how you ...READ MORE

answered May 22, 2018 in Java by Akrati
• 3,150 points
60 views
0 votes
1 answer

NoClassDefFoundError in Java

Can’t tell you the exact reason as ...READ MORE

answered May 25, 2018 in Java by v.liyyah
• 1,300 points
244 views
0 votes
1 answer

Sorting an ArrayList in Java

You can easily do this by simply ...READ MORE

answered May 4, 2018 in Java by geek.erkami
• 2,320 points
195 views
0 votes
1 answer

Why the main() method in Java is always static?

As you might know, static here is ...READ MORE

answered May 8, 2018 in Java by geek.erkami
• 2,320 points
342 views