Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Method with infinite loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Method with infinite loop" Watch "Method with infinite loop" New topic
Author

Method with infinite loop

Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
Hi

If I have a method with an infinite loop, is it not necessary for the method to return what its signature says?



Above code compiles just fine even though the method has no return statement.
Logically it makes sense because a return after the loop would be unreachable. But I feel that even the above code should not compile as the method says it returns 'int' but actually does not.


Thanks and Regards
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14338
    
  22

Originally posted by Satya Maheshwari:
Logically it makes sense because a return after the loop would be unreachable. But I feel that even the above code should not compile as the method says it returns 'int' but actually does not.

Well, the Java compiler doesn't care how you feel...

The compiler also checks for unreachable code, and as you say yourself, a return statement after the infinite loop would be unreachable code. It would be strange if the compiler would first say "you must add a return statement" and then "the return statement is unreachable". That would make the code, which in principle works without problems, impossible to compile.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39784
    
  28
Try changing while (true) to while (3 > 2) and see what difference that makes.
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
Hi Jesper,Campbell

Thanks for clarifying. Apparently I did not see any change after changing from while(true) to while(3>2). Please point me if I am missing something.
Bupjae Lee
Ranch Hand

Joined: May 14, 2007
Posts: 107
It looks stange, but it is legal perfectly.

Maybe another example which has similar oddity maybe help you



In the viewpoint of method main(), it only knows that getValue() will return int value.
It doesn't know that getValue() will *always* throw RuntimeException.
[ September 02, 2008: Message edited by: Bupjae Lee ]
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3849


Bupjae Lee:
It doesn't know that getValue() will *always* throw RuntimeException


Not sure about that statement. This is because RuntimeExceptions are not checked by the compiler.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Isn't that the point Bupjae Lee is making?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3849


Paul Sturrock:
-----------------------------------------------------------
Isn't that the point Bupjae Lee is making?


Yes. By considerring the code he has posted. But I was thinking in general
I mean had the getValue() method declared to be as throwing any checked exceptions:

Bupjae Lee:
In the viewpoint of method main(), it only knows that getValue() will return int value.


main method has to consider that also.
[ September 02, 2008: Message edited by: Vijitha Kumara ]
Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245
JLS, section 8.4.7
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.


section 14.21
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.


Condition expression (true) is constant expression with value true and there is no break statement. So the while statement and the method body cannot complete normally.

[ September 02, 2008: Message edited by: Vlado Zajac ]
[ September 02, 2008: Message edited by: Vlado Zajac ]
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
Originally posted by Campbell Ritchie:
Try changing while (true) to while (3 > 2) and see what difference that makes.


I agree.



Here 'return' is mandatory even though its unreachable.
Gaurav Chauhan
Greenhorn

Joined: Apr 14, 2008
Posts: 27
I noticed that while(true) need not to have a return statement but while(3>2) should have one.

But the question is why is this happening ? Both give true value inside the while condition.

Kindly describe.


Don't tell me there is nothing beyond sky, There are footprints on moon.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14338
    
  22

That's because the value "true" is a compile-time constant, which means that the compiler can check for it, so that it knows that it's an infinite loop. The expression "3 > 2" is evaluated at runtime (not by the compiler at compile-time), so the compiler doesn't know in advance that the result of the expression is always true.
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3849


Gaurav Chauhan:
I noticed that while(true) need not to have a return statement but while(3>2) should have one.


Did you try compiling it? Neither while(true) nor while(3>2) required return statement. If you have one compiler complains saying unreachable statement.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39784
    
  28
Originally posted by Gaurav Chauhan:
I noticed that while(true) need not to have a return statement but while(3>2) should have one.
This has already been described; I think you mean while (a > b) rather than while (3 > 2); look at the previous posts.
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3849


Jesper Young
----------------------------------------------------------------------------
The expression "3 > 2" is evaluated at runtime (not by the compiler at compile-time),
--------------------


I'm just checking my understanding about this. Any expression will be evaluated at runtime or one with constants will evaluate at compile time ? Since expression 3 > 2 contains both constants isn't it evaluated at compile time? If 3 > 2 evaluated at runtime then compiler should have complains about it. May be Jesper Young means a > b.
Gaurav Chauhan
Greenhorn

Joined: Apr 14, 2008
Posts: 27
Sorry i meant while(a>b) needs a return statement not 3>2.

But the thing that comes out here is that true is a constant and while evaluating with 3>2 we are dealing with constants again so the compiler knows at compile time that any code beyond this will be unreachable.

In case of a>b the value of a and b can be changed inside the while loop or somewhere in our code and then the result might be false so the compiler checks for return statement.


Any more inputs RANCHERS ?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method with infinite loop