• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Compilation Error

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rob, your explanation was indeed helpful.

Best Regards,
Faraz
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or drop even more redundant code:Or evenJust kidding (unfortunately).
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Av, could you please UseCodeTags in the future?
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And insistence on returning a value is not the same as "strongly typed".
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic