Optional parameter in java (can be null) Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Optional parameter in java (can be null) without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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 ?


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);
We are here to answer your question about Optional parameter in java (can be null) - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji