What does the following operator - (? do something : do something) mean?

69 Views Asked by At

What does the following code do?

Tracker t = (trackerId == TrackerName.APP_TRACKER) ? analytics.newTracker(PROPERTY_ID)
                : (trackerId == TrackerName.GLOBAL_TRACKER) ? analytics.newTracker(R.xml.global_tracker)
                : analytics.newTracker(R.xml.ecommerce_tracker);
2

There are 2 best solutions below

2
Dave On

condition ? //block1 : //block2 is the same thing as if (condition) { //block1 } else { //block2 }

0
paolo On

Your expression shall be a ? b : c. This means: If a, then do b. Otherwise do c.

What you have here is a nested version of this syntax.