• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

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

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • 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: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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: 3838
66
Netbeans IDE Oracle Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 67464
257
  • Mark post as helpful
  • send pies
  • 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
  • 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: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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: 67464
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, yoiu are right. And if (true)... does look more like a tautology.
 
Sheriff
Posts: 21842
105
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
So, what would be the correct reason behind this error?
 
Campbell Ritchie
Marshal
Posts: 67464
257
  • Mark post as helpful
  • send pies
  • 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.
 
Rancher
Posts: 3419
34
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
It was the best of times. It was the worst of times. It was a tiny ad.
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!