开发者

What does this Java return statement mean?

开发者 https://www.devze.com 2023-03-18 02:42 出处:网络
Am looking over some snippets of code and have come across a return statement which I\'ve never seen before. What does it mean?

Am looking over some snippets of code and have come across a return statement which I've never seen before. What does it mean?

return checkDB != null ? true : false;

Here's the whole method code, for reference:

private boolean checkDataBase(){
        SQLiteDatabase checkDB = null;
        try{
            String pathToDB = dbPath + dbName;
            checkDB = SQLiteDatabase.openDatabase(pathToDB, null, SQLiteDatabase.OPEN_READONLY);
        }catch(SQLiteException e){
            //database does't exist yet.
        }
        if(checkDB != null){
      开发者_如何学C      checkDB.close();
        }
        return checkDB != null ? true : false;
    }


The same as return checkDB != null

?: is a "ternary operator" which. Example: a ? b : c does the same as a method with this body: { if(a) { return b; } else { return c; } }


its a ternary statement can be read as

if(checkDB != null) {
   return true;
}
else {
    return false;
}


return checkDB != null ? true : false; is exactly the same as return checkDB != null;.


It's called a ternary operation - a nice one line variation on if else logic.

0

精彩评论

暂无评论...
验证码 换一张
取 消