Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ternary operator to return value- Java/Android

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";
like image 676
sumit Avatar asked Aug 01 '14 04:08

sumit


People also ask

Can you return in a ternary operator Java?

The first operand in java ternary operator should be a boolean or a statement with boolean result. If the first operand is true then java ternary operator returns second operand else it returns third operand.

Can we write return statement in ternary operator?

In a ternary operator, we cannot use the return statement.

Can we use ternary operator in Java?

Java ternary operator is the only conditional operator that takes three operands. It's a one-liner replacement for the if-then-else statement and is used a lot in Java programming. We can use the ternary operator in place of if-else conditions or even switch conditions using nested ternary operators.


4 Answers

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));
like image 160
Aniket Thakur Avatar answered Oct 17 '22 11:10

Aniket Thakur


lets say I need

  (usrname.equals(u) && passwd.equals(p)) ? return "member" : return guest";

The correct syntax is:

   return (usrname.equals(u) && passwd.equals(p)) ? "member" : "guest";

The general form of the ternary operator is

   expression-1 ? expression-2 : expression-3

where expression-1 has type boolean, and expression-2 and expression-3 have the same type1.

In your code, you were using return statements where expressions are required. In Java, a return statement is NOT a valid expression.

1 - This doesn't take account of the conversions that can take. For the full story, refer to the JLS.


Having said that, the best way to write your example doesn't uses the conditional operator at all:

   return usrname.equals(username) && passwd.equals(password);
like image 25
Stephen C Avatar answered Oct 17 '22 10:10

Stephen C


Why redundant boolean

Just use

return  (usrname.equals(username) && passwd.equals(password));
like image 4
Ruchira Gayan Ranaweera Avatar answered Oct 17 '22 11:10

Ruchira Gayan Ranaweera


By the way you can simplify:

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

To:

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

The ternary operator work similar in php than Java, I think you have made a silly mistake, maybe "username" have a space or another white character

String a1 = "a";
String b1 = "b";

String a2 = "a";
String b2 = "b";

System.out.println((a1.equals(a2)&&b1.equals(b2))?"true":"false");

it return "true"

like image 1
Troncador Avatar answered Oct 17 '22 12:10

Troncador