Yassine Faradi Yassine Faradi - 4 months ago 9
Android Question

A line of code is incomprehensible for me


newTilt = (newTilt > 90) ? 90 : newTilt;



What does this line of code mean? It's the first time that I see something like this in Android.

This is the full method that contains the line above:

public void onTiltMore(View view) {
if (!checkReady()) {
return;
}

CameraPosition currentCameraPosition = mMap.getCameraPosition();
float currentTilt = currentCameraPosition.tilt;
float newTilt = currentTilt + 10;

newTilt = (newTilt > 90) ? 90 : newTilt;

CameraPosition cameraPosition = new CameraPosition.Builder(currentCameraPosition)
.tilt(newTilt).build();

changeCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}

Answer

It is a ternary expression. It says that if the newTilt at that point is above 90, it sets it to 90, otherwise it leaves it unchanged. You could kind of think of it in the way of an if-else statement.

 if(newTilt > 90) {
    newTilt = 90;
 } else {
    newtilt = newTilt; // which does nothing useful 
 }
Comments