Ternary operator to return value- Java/Android

后端 未结 5 702
情深已故
情深已故 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 02:10

    You can do

    return (usrname.equals(username) && passwd.equals(password) )?  true : false;
    

    true and false can be replaced by any return value you want. If it is just boolean then you can avoid ternary operator altogether. Just do

    return  (usrname.equals(username) && passwd.equals(password));
    

提交回复
热议问题