Ternary operator to return value- Java/Android

后端 未结 5 701
情深已故
情深已故 2021-02-07 01:13

Just switched to Java from php

I encountered following issue

I want to rewrite

if(usrname.equals(username) && (passwd.equals(password))){         


        
5条回答
  •  终归单人心
    2021-02-07 01:59

    Java ternary operator is an expression, and is thus evaluated to a single value. As per the Javadocs, for below given expression

    result = someCondition ? value1 : value2;
    

    if the boolean condition is true then assign the value of value1 to the result, else assign value2 to the result. Both value1 and value2 should be of same value type.

    Hence the correct syntax of a return statement would be

    return boolean_condition ? value_when_true : value_when_false
    

提交回复
热议问题