This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Compilation Error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Compilation Error" Watch "Compilation Error" New topic
Author

Compilation Error

Faraz Alig
Greenhorn

Joined: Mar 14, 2010
Posts: 24
Hi Java Experts,

Just wanted to know why one get a compilation error for this snippet of code. Logically it looks fine.

public static int sign(int n){
if(n>0) return 1;
else if(n==0) return 0;
else if(n<0) return -1;
}

Thanks in advance!!!
Faraz
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11170
    
  16

it would be of IMMENSE help if you posted the actual error message you get when compiling.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Faraz Alig
Greenhorn

Joined: Mar 14, 2010
Posts: 24
Sorry for not posting the complete issue. If I write the below simple piece of code. It gets compiled and work fine

public class TestReturn {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(sign(1));

}
public static int sign(int n){
if(n>0) return 1;
else if(n==0) return 0;
else return -1;
}
}


BUT if I change this to the below code

public class TestReturn {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(sign(1));

}
public static int sign(int n){
if(n>0) return 1;
else if(n==0) return 0;
else if(n<0) return -1;
}
}


... then I get the compilation error that "This methods must return of result of the type int".

LOGICALLY both piece of code looks fine.

- Faraz
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Although logically speaking that method will always return something, the compiler cannot check it. The compiler does not check the logic inside the if-statements, only the syntax. Therefore, for the compiler it's possible that all three guards (n > 0, n ==0 and n < 0) are all false (we know better*), and nothing will be returned.

Drop the guard from your last if-statement:

* Actually there are two (non-int) values for which this is actually the case: Float.NaN and Double.NaN.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Faraz Alig
Greenhorn

Joined: Mar 14, 2010
Posts: 24
Thanks Rob, your explanation was indeed helpful.

Best Regards,
Faraz
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

You're welcome.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Or drop even more redundant code:Or evenJust kidding (unfortunately).
Av Vinay
Greenhorn

Joined: Oct 27, 2009
Posts: 6
Hi.,

public static int sign(int n){
if(n>0) return 1;
else if(n==0) return 0;
else return -1;
}
}
In the above method the final return in else block is mandotory, as Java is a strongly typed language, it expects the function to return the value, either from the else block or a seperate return statement.

public static int sign(int n){
if(n>0) return 1;
else return -1; // else is not mandatory
}
}

public static int sign(int n){
if(n>0) return 1; // Only this statement alone in the function body won't compile, as the fuction returns the value only
// if condition is satisfied. ..
return -1; // nothing but placing in else block
}
}

The other ways are to use java ternary operator { return(n>0?1:n==0?0:-1); } Or by assigning the value to temp variable and returning the same before returning from the function.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Av, could you please UseCodeTags in the future?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
And insistence on returning a value is not the same as "strongly typed".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Compilation Error
 
Similar Threads
Error states: This method must return a result of type int
Fibonacci Algorithms and their different manifestations. Which is the best?
A quiz for the bored
Why do i keep getting the "type expected" error
Small Java problem