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
42 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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,220 points
15 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,520 points
27 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,310 points
95 views
0 votes
1 answer

How to return multiple objects from a Java method?

Yes, we can return multiple objects from ...READ MORE

answered Jul 12, 2018 in Java by Mohini
• 260 points
57 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,310 points
37 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,820 points
138 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
25 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,290 points
59 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
137 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
194 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.