Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 515529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:38:15+00:00 2026-05-13T07:38:15+00:00

Given the following code sample: public class WeirdStuff { public static int doSomething() {

  • 0

Given the following code sample:

public class WeirdStuff {

    public static int doSomething() {
        while(true);
    }

    public static void main(String[] args) {
        doSomething();
    }
}

This is a valid Java program, although the method doSomething() should return an int but never does. If you run it, it will end in an infinite loop. If you put the argument of the while loop in a separate variable (e.g. boolean bool = true) the compiler will tell you to return an int in this method.

So my question is: is this somewhere in the Java specification and are there situation where this behavior might be useful?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T07:38:15+00:00Added an answer on May 13, 2026 at 7:38 am

    I’ll just quote the Java Language Specification, as it’s rather clear on this:

    This section is devoted to a precise explanation of the word “reachable.” The idea is that there must be some possible execution path from the beginning of the constructor, method, instance initializer or static initializer that contains the statement to the statement itself. The analysis takes into account the structure of statements. Except for the special treatment of while, do, and for statements whose condition expression has the constant value true, the values of expressions are not taken into account in the flow analysis.

    …

    A while statement can complete normally iff at least one of the following is true:

    • The while statement is reachable and the condition expression is not a constant expression with value true.
    • There is a reachable break statement that exits the while statement.

    …

    Every other statement S in a nonempty block that is not a switch block is reachable iff the statement preceding S can complete normally.

    And then apply the above definitions to this:

    If a method is declared to have a return type, then every return statement (§14.17) in its body must have an Expression. A compile-time error occurs if the body of the method can complete normally (§14.1).

    In other words, a method with a return type must return only by using a return statement that provides a value return; it is not allowed to “drop off the end of its body.”

    Note that it is possible for a method to have a declared return type and yet contain no return statements. Here is one example:

    class DizzyDean {
      int pitch() { throw new RuntimeException("90 mph?!"); }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following sample code; class Program { static void Main(string[] args) { var
Here is a sample code: public class TestIO{ public static void main(String[] str){ TestIO
Given the following method: public static void SetPropertyValue(object target, string propName, object value) {
Given the following code: final class retVal { int photo_id; } Gson gson =
Consider the following sample code: #include <iostream> using namespace std; class base { public:
Given the following classes: public class Class1<TObject> { protected void MethodA<TType>(Expression<Func<TObject, TType>> property, ref
Considering the following sample code: // delivery strategies public abstract class DeliveryStrategy { ...
Given the following code sample: uint8_t i, in, ni; i = in = 2;
Given the following code. EventLoopScheduler scheduler = new EventLoopScheduler(ts => new Thread(ts)); BehaviorSubject<int> subject
Given the following code: string source = Some Unicode String; foreach( char value in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.