-2

I was looking through a java book and i remember reading something about using the ? operator for if statements an I cannot find the reading material anymore. I tried googling the topic but it yielded nothing. So how does the below piece of code

if( itemA ? itemB) : itemC

work

Nicholas K
  • 15,148
  • 7
  • 31
  • 57
ewrvdc
  • 23
  • 3

2 Answers2

0

The "?" ":" correspondans to the ternary operator

I.e : a ? b : c means "if a is true, return b, else return c"

0

Its called ternary operator. Syntax will be like

(Condition) ? (Value to be assigned if the condition is true) : (Value to be assigned if the condition is false)

Look at the examples given below for understanding the usage

Eg.

public String getGender(String genderCode){

   return gender.equals("M")?"Male":"Female";

}