Ternary operator to return value- Java/Android

sumit

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";
Aniket Thakur

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));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Return throwing ternary operator?

From Dev

Complex return in Ternary Operator?

From Dev

Why is return skipping the value produced in my ternary operator?

From Dev

Why does using "0" with the ternary operator return the first value?

From Dev

Can we write an ternary operator without a return value

From Dev

Can we write an ternary operator without a return value

From Dev

Ternary operator with return statements JavaScript

From Dev

js: Multiple return in Ternary Operator

From Dev

ternary operator issue in React return

From Dev

Return ternary operator in JavaScript reduce

From Dev

Ternary operator inside a return statement

From Dev

ternary operator in php with echo value

From Java

Why and when does the ternary operator return an lvalue?

From Java

How is the return type of a ternary operator determined?

From Dev

Return a Collections.emptyList() fails with ternary operator

From Dev

Ternary operator to set value based on value of variable

From Dev

Perl - increment value within ternary operator

From Dev

Pandas: Ternary conditional operator for setting a value in a DataFrame

From Dev

Return value of operator++

From Dev

operator return value issue

From Dev

How to return value by ternary condition in a stream?

From Dev

How to correctly return the path to an image from the ternary operator (and display it)

From Dev

Why can't we have return in the ternary operator?

From Dev

Javascript ternary operator (?:) change and return object in single block

From Dev

return template interpolation within ternary operator in angular 5

From Dev

+ operator in a ternary operator

From Dev

Ruby ternary operator (or) or operator

From Dev

Ternary operator and increment operator

From Dev

Ternary Operator(Elvis Operator) ?:

Related Related

  1. 1

    Return throwing ternary operator?

  2. 2

    Complex return in Ternary Operator?

  3. 3

    Why is return skipping the value produced in my ternary operator?

  4. 4

    Why does using "0" with the ternary operator return the first value?

  5. 5

    Can we write an ternary operator without a return value

  6. 6

    Can we write an ternary operator without a return value

  7. 7

    Ternary operator with return statements JavaScript

  8. 8

    js: Multiple return in Ternary Operator

  9. 9

    ternary operator issue in React return

  10. 10

    Return ternary operator in JavaScript reduce

  11. 11

    Ternary operator inside a return statement

  12. 12

    ternary operator in php with echo value

  13. 13

    Why and when does the ternary operator return an lvalue?

  14. 14

    How is the return type of a ternary operator determined?

  15. 15

    Return a Collections.emptyList() fails with ternary operator

  16. 16

    Ternary operator to set value based on value of variable

  17. 17

    Perl - increment value within ternary operator

  18. 18

    Pandas: Ternary conditional operator for setting a value in a DataFrame

  19. 19

    Return value of operator++

  20. 20

    operator return value issue

  21. 21

    How to return value by ternary condition in a stream?

  22. 22

    How to correctly return the path to an image from the ternary operator (and display it)

  23. 23

    Why can't we have return in the ternary operator?

  24. 24

    Javascript ternary operator (?:) change and return object in single block

  25. 25

    return template interpolation within ternary operator in angular 5

  26. 26

    + operator in a ternary operator

  27. 27

    Ruby ternary operator (or) or operator

  28. 28

    Ternary operator and increment operator

  29. 29

    Ternary Operator(Elvis Operator) ?:

HotTag

Archive