-1

String key = offset == -1 ? keyStroke : keyStroke.substring( offset + 1 ); I found an example of some code online and it contained this line that I do not understand.

This is a piece of code found in a key binding example. Specifically, "KeyboardAnimation.java" line 37.

This may come across as a dumb question, but I have no idea what is happening here. I could spend a long time doing research, but I feel like this could be fairly easily explained by someone (I can go into more detail about my reasoning, but it's beside the point).

I get that it's setting the variable key of type String to something. keyStroke is a String that is formatted the way a keystroke would be:

Parses a string and returns a KeyStroke. The string must have the following >syntax:
* ( | )

modifiers := shift | control | ctrl | meta | alt | altGraph

typedID := typed

typedKey := string of length 1 giving Unicode character.

pressedReleasedID := (pressed | released) key

key := KeyEvent key code name, i.e. the name following "VK_".

If typed, pressed or released is not specified, pressed is assumed. Here are >some examples:

"INSERT" => getKeyStroke(KeyEvent.VK_INSERT, 0);
"control DELETE" => getKeyStroke(KeyEvent.VK_DELETE, InputEvent.CTRL_MASK);
"alt shift X" => getKeyStroke(KeyEvent.VK_X, InputEvent.ALT_MASK | InputEvent.SHIFT_MASK);
"alt shift released X" => getKeyStroke(KeyEvent.VK_X, InputEvent.ALT_MASK | InputEvent.SHIFT_MASK, true);
"typed a" => getKeyStroke('a');

(from: KeyStroke javax.swing.KeyStroke.getKeyStroke(String s))

Now, to get into what I actually don't know: Why does it appear to have a Boolean operator in there? What does the question mark do? What does the colon do?

I feel like this is stuff I should have learned before but haven't...

Aragorn Crozier
  • 364
  • 2
  • 12
  • 1. The conditional operator `? :` (or ternary) and 2. A bitwise `or` on two `int` constants (which represent bitmasks). – Elliott Frisch Jun 24 '17 at 05:06
  • A ternary operator is a shorthand of if else statement it like: if(String key = offset == -1){ keyStroke } else { keyStroke.substring( offset + 1 ); } – Fady Saad Jun 24 '17 at 05:08
  • Sorry, but I still don't really understand what either of you mean... As far as @Elliott's comment, what's a bitmask? A bitwise or on two int constants? And Fady, what would that kind of if statement do? I kind of get the picture, but I'm still a bit confused on the details... I've only been doing Java for a semester, so I apologize in advance for ignorant questions... – Aragorn Crozier Jun 24 '17 at 05:18
  • Possible duplicate of [How do you use the ? : (conditional) operator in JavaScript?](https://stackoverflow.com/questions/6259982/how-do-you-use-the-conditional-operator-in-javascript) – Alfabravo Jun 24 '17 at 05:44

1 Answers1

1
String key = offset == -1 ? keyStroke :  keyStroke.substring( offset + 1);

This is a variable declaration and initialization. It defines a variable:

String key

and initializes it immediately:

String key = ...;

Now, what it the value on the right side, that is used to initialize the string? It's a ternary statement, which is an expression of the form:

condition ? valueIfTrue : valueIfFalse

This expression thus has the value valueIfTrue if the boolean expression condition is true, and the value valueIfFalse if the boolean expression condition is false.

In your case, the key variable is thus initialized with the value of keyStroke if offset == -1 is true, or with the value of keyStroke.substring( offset + 1 ) if offset == -1 is false.

The whole thing could be rewritten as

String key;
if (offset == -1) {
    key = keyStroke;
} 
else {
    key = keyStroke.substring(offset + 1);
}
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255