Just switched to Java from php
I encountered following issue
I want to rewrite
if(usrname.equals(username) && (passwd.equals(password))){
return true;
}
else{
return false;
}
as
(usrname.equals(username) && passwd.equals(password) )? return true : return false;
it is not working(syntax error)
however,
int a=1;
int b=2;
int minVal = a < b ? a : b;
is working
Why ternary operator are not behaving correctly while returning value depending on some condition
EDIT
return (usrname.equals(username) && passwd.equals(password));
could be solution if it return boolean .
lets say i need
(usrname.equals(username) && passwd.equals(password) )? return "member": return "guest";
解决方案
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));