• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Compilation Error

 
Faraz Alig
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it would be of IMMENSE help if you posted the actual error message you get when compiling.
 
Faraz Alig
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Faraz Alig
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob, your explanation was indeed helpful.

Best Regards,
Faraz
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or drop even more redundant code:Or evenJust kidding (unfortunately).
 
Av Vinay
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Av, could you please UseCodeTags in the future?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And insistence on returning a value is not the same as "strongly typed".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic