-3

can someone explain these code? why does it have ? in there?

package course.examples.theanswer;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class TheAnswer extends Activity {

public static final int[] answers = { 42, -10, 0, 100, 1000 };
public static final int answer = 42;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.answer_layout);

    TextView answerView = (TextView) findViewById(R.id.answer_view);


    int val = findAnswer();
    String output = (val == answer) ? "42" : "We may never know";
    answerView.setText("The answer to life, the universe and everything is:\n\n"
                    + output);
}

private int findAnswer() {
    for (int val : answers) {
        if (val == answer)
            return val;
    }
    return -1;
}
}
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428

6 Answers6

7

Its conditional / ternary operator.

String output = (val == answer) ? "42" : "We may never know";

effectively same as :

if(val==answer)
{
 output = "42" 
}

else
{
output = "we may never know"
}
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
1

This is called a Ternary operator. It's a kind of Replacement for if-else statement.

(Condition) ? {if true execute this} : {else do this}
Ninad Pingale
  • 6,801
  • 5
  • 32
  • 55
1

It's a ternary operator..

this line:

String output = (val == answer) ? "42" : "We may never know";

is the same as this:

String output;
if (val == answer) {
  output = "42";
} else {
  output = "We may never know";
}

It is usually used for simple conditions and assignments...a one-liner.

Alécio Carvalho
  • 13,481
  • 5
  • 68
  • 74
1

?: is operator for if-then-else behavior

(val == answer) ? "42" : "We may never know";

this means

if(val==answer) {
    output = "42"
} else { 
     output = "We may never know";
} 
Sanjeev
  • 9,876
  • 2
  • 22
  • 33
1

If you mean this line:

String output = (val == answer) ? "42" : "We may never know";
    answerView.setText("The answer to life, the universe and everything is:\n\n"
                    + output);

This ? is a short form of (if...then...else).

if (val == answer) then output = "42" else output = "We may never know"; answerView.setText("The answer to life, the universe and everything is:\n\n" + output)

Jens
  • 67,715
  • 15
  • 98
  • 113
1

On this line

String output = (val == answer) ? "42" : "We may never know";

the ? represents the ternary operator: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html

if val == answer is true, the value assigned to output will be "42". If it equates to false, it will be assigned "We may never know"

val = answer;
...
String output = (val == answer) ? "42" : "We may never know";
System.out.println(output);

would output:

42

whereas:

val = answer + 1;
...
String output = (val == answer) ? "42" : "We may never know";
System.out.println(output);

would output:

We may never know

JamesB
  • 7,774
  • 2
  • 22
  • 21