-3

What exactly does this mean?

String path = selectedPath.equals("/") ? "/" : selectedDir;

What I think it is saying is set path to the selectedPath if it equals "/" and if it doesn't set the path to selectedDir.

1 Answers1

1

Ternary operator

The ternary operator is a way to do an "if else" that actually returns a value so if you have a function such as:

int f(boolean a, int b, int c) {
    if (a) {
        return b;
     } else {
         return c;
     }
 }

and then you call the function like:

int y = f(b > c, b, c);

you can avoid the function by doing:

int y = b > c ? b : c;

So it means that, if you have the following expresion:

a ? b : c

it means:

if a is true, then return b; otherwise return c.

Specifically in your case, it means, as you said, that if selectedPath is equal to "/" then return "/" otherwise return the value at the right of the ':' character.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Eduardo Montoya
  • 1,520
  • 2
  • 12
  • 14