Optional parameter in java (can be null)

Pythorogus :

I have a function with a boolean parameter, but it could be null too.

If it's true or false, it makes a query with a "WHERE" clause. If it was null, the query has no "WHERE" clause, only the select clause.

How can I do that in Java ?

Roshana Pitigala :

Use Boolean class

public String foo(Boolean addWhere){
    String query = "SELECT * FROM tbl";
    if(addWhere != null){
        query += (addWhere? " WHERE true" : " WHERE false");
    }
    return query;
}

Call it as following,

String q = foo(false);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=86366&siteId=1