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 28, 2018 in Java by anto.trigg4
• 3,440 points
1,942 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 28, 2018 by geek.erkami
• 2,680 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, 2019 in Java by developer_1
• 3,320 points
6,866 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,620 points
663 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,370 points
1,184 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, 2019 in Java by Karan
• 19,610 points
8,197 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,370 points
761 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,910 points
33,231 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,190 points
1,003 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
2,284 views
0 votes
1 answer

Sorting an ArrayList in Java

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

answered May 5, 2018 in Java by geek.erkami
• 2,680 points
1,922 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 9, 2018 in Java by geek.erkami
• 2,680 points
1,882 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP