• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Compiler showing error: "This method must return a result of type int"

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public int retTest()
{
if (true)
{
return 10;
}
}
Compiler is showing the mentioned error for this method.
Please explain the reason behind this error
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

gauravkv gupta wrote:Please explain the reason behind this error


Basically because the compiler is too stupid to work out that if(true) is an oxymoron. If it did, it would probably tell you so.

Winston
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

gauravkv gupta wrote:Please explain the reason behind this error


Basically because the compiler is too stupid to work out that if(true) is an oxymoron. If it did, it would probably tell you so.

Winston


Ironically, compiler gives an 'unreachable code' error for while(false) - I don't know why it doesn't figure out this if(true) or if(false) things.
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anayonkar Shivalkar wrote:Ironically, compiler gives an 'unreachable code' error for while(false) - I don't know why it doesn't figure out this if(true) or if(false) things.


I believe the reason is to allow you to use a constant boolean flag to activate/deactivate a behavior:


 
Marshal
Posts: 79828
388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure if (true) is an oxymoron? It is redundant code, anyway.
The compiler is not programmed to be stupid, but there are restrictions on how one can program compilers. If for example one wishes to go through the code for changes to variables, one can tell [just by looking] that this if block will never be entered; But the performance overhead of [using a compiler for] siuch checking for such changes, in order to confirm that the block can never be entered would make compilation slower than writing the code, especially if there is much code between the declaration and that if. Now, marking i final, so it is a compile-time constant, that might change things. The compiler designers obviously came to the decision that such checking is feasible, but, “life’s too short.”
If you permit break; statements, then while (true)... is valid code, so why not if (true)?

[Edit]Add two short bits of text in [blue print] above[/edit]
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The relevant section of the JLS is this one. It agrees with Martin - that conditional compilation is the reason if(false) is treated differently to while(false) - see the example box at the bottom.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Are you sure if (true) is an oxymoron? It is redundant code, anyway...


Yeah, I was being flippant. It's probably more like a tautology.

Winston
 
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It fails to compile, because, it must have a return statement for a false option too.

Something like



I guess i am right.
 
Campbell Ritchie
Marshal
Posts: 79828
388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, yoiu are right. And if (true)... does look more like a tautology.
 
Sheriff
Posts: 22813
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The odd thing is that some compilers do notice that something is wrong. The code Kunal posted would produce a "dead code" warning for the return 0; in my Eclipse.
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, what would be the correct reason behind this error?
 
Campbell Ritchie
Marshal
Posts: 79828
388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don’t know.
Remember that warnings can be ignored, and the Eclipse compiler is much more sensitive to such errors than the Sun/Oracle one.
 
Master Rancher
Posts: 5045
80
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The correct reason is the one Martijn gave and Matthew posted a JLS link confirming. I suggest reading that link. A valid Java compiler is required to behave this way, treating "if (true)" as if it's something that may evaluate to false, even though it's obviously going to be true. Some IDEs like Eclipse may choose to perform a different (perhaps more sensible) analysis - but they can only issue warnings; they can't prevent compilation here. Not without violating the JLS.
 
Those are the largest trousers in the world! Especially when next to this ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic