简体   繁体   中英

Return int as boolean

I am a beginner so please bear with me. I decompiled the source code of a professional application. When I copied the source code into eclipse, an error came up with the following code( the error is with the return type):

public boolean method(){
...
...
   for(int i = 0; ; i = 1){
       return i;
}

How can I change the code to keep it correct but keep the functionality?

You can cast i as a boolean , or change return i; to return i != 0; , which will be true for all non-zero values of i , and false if i == 0 .

As a side note, I really see no reason to wrap a return in a for loop. In this case, you might as well just replace both of those lines with return 0; (or return false; , to match the method signature).

Change the return type from boolean to int .

As you can see, your method is returning i which is declared as an int in the for loop, so the return type of your method has to be the same as the type of the variable it returns.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM