简体   繁体   中英

Change return method from boolean to int

Can i convert in some way this method from boolean to int? so when i will call the method instead return true or false i can return 1 or 0:

public boolean openMode() {

return Settings.System.getBoolean(contxt.getConentResolver(), Setting.System.START_METHOD, true); 

}
public int openMode(){
    boolean value = Settings.System.getBoolean(contxt.getConentResolver(), Setting.System.START_METHOD, true);

    if(value){
        return 1;
    }
    else{
        return 0;
    }

}

Unlike other programming languages like C, java does not recognizes 1 or 0 as true or false ( or boolean in short). So the return type of this method has to be boolean it self, you can not return 1 or 0 unless you change the method's return type. However, if you can change the method signature , you can change the return type to int and return 1 for true and 0 for false. example :

public int openMode(){
 return (Settings.System.getBoolean(contxt.getConentResolver(), Setting.System.START_METHOD, true))?1:0 ;
}

A shorter form, using a "ternary operator":

public int openMode()
{
    boolean value = Settings.System.getBoolean(contxt.getConentResolver(), Setting.System.START_METHOD, true);
    return (value == true ? 1 : 0);
}

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