0

Can someone explain the question mark in the following code? Also INITIAL_PERMANCE is a static final constant in the code but what is the last line of synatax called?

Synapse(AbstractCell inputSource, float permanence) {
    _inputSource = inputSource;
    _permanence = permanence==0.0 ? 
        INITIAL_PERMANENCE : (float)Math.min(1.0,permanence);
}
randominstanceOfLivingThing
  • 16,873
  • 13
  • 49
  • 72
letter Q
  • 14,735
  • 33
  • 79
  • 118
  • 2
    Look up the Java ternary operator. – Hovercraft Full Of Eels Sep 15 '12 at 02:24
  • 1
    I've always found it stupid that the [Java Tutorial from Oracle](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html) actually _does_ call this THE ternary operator. So lame. It should be called the conditional operator, which just _happens_ to be a ternary (three operand) operator. – Ray Toal Sep 15 '12 at 02:28
  • @Ray: agree. But there we have it, THE ternary operator. – Hovercraft Full Of Eels Sep 15 '12 at 02:29
  • You will want to check out the [Java Language Specification on this operator](http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.25) because there can be some funky behaviors (at least to my jaundiced eye) when the second and third operands of of different type. – Hovercraft Full Of Eels Sep 15 '12 at 02:34
  • @hovercraft-full-of-eels last time I checked it's actually a compile failure if both parts don't return the same type. – EdC Sep 15 '12 at 02:37
  • 1
    @EdC: check again. type promotion can occur, and this can lead to unusual (at least to my eye) behavior. – Hovercraft Full Of Eels Sep 15 '12 at 02:37
  • possible duplicate of [What is a Question Mark "?" and Colon ":" Operator Used for?](http://stackoverflow.com/questions/10336899/what-is-a-question-mark-and-colon-operator-used-for) – Brendan Long Nov 04 '14 at 23:42

5 Answers5

10

The ? and : are part of the java conditional operator. Sometimes called the ternary operator because it is the only operator in Java that takes 3 arguments.

This is essentially an inline IF / THEN / ELSE block.

_permanence = permanence==0.0 ? 
    INITIAL_PERMANENCE : (float)Math.min(1.0,permanence);

Can be rewritten as follows:

if (permanence == 0.0)
    _permanence = INITIAL_PERMANENCE;
else
    _permanence = (float) Math.min(1.0,permanence);

The general form of the conditional operator is

<Test returning a boolean> ? <value for if test is true> : <value for if test is false>
EdC
  • 2,309
  • 1
  • 17
  • 30
0

This is equal to an if else statement in an inlined manner.Equivalent to

   _permanence = 
    {// A kind of anonymous routine for assignment
      if(permanence==0.0)
      { INITIAL_PERMANENCE } 
      else
      { (float)Math.min(1.0,permanence)}
    }

A good explanation is on oracle site about ternary operators

user1655481
  • 376
  • 1
  • 10
  • 1
    @Jon: yours was the only with correct syntax and logic. Too bad it was deleted at the moment I clicked upvote. – BalusC Sep 15 '12 at 02:27
  • 1
    Down-voted because this code is actually incorrect. You're assigning the boolean (permanence==0.0) to _permanence and both blocks of the if else do nothing. – EdC Sep 15 '12 at 02:29
  • @BalusC Sorry, just didn't think the world needed the same answer 3 times. – Jonathan Henson Sep 16 '12 at 20:39
0

This is the ternary operator. It works like an if-else statement.

Decomposed, the statement is similar to this:

if(permanence == 0.0) { 
    _permanence = INITIAL_PERMANENCE;
} else {
    _permanence = (float)Math.min(1.0,permanence);
}

Its use is limited in situations in which the meaning is very clear. Ternary operators can confuse, so use them sparingly.

The last statement:

(float)Math.min(1.0, permanence)

is called a type cast. You're casting the result of Math.min(), which returns a double, to that of a float. You'll have to read up more on what floating point numbers are to see the value of doing that is.

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • The logic of your initial code example was just wrong. No surprise, really. – BalusC Sep 15 '12 at 02:30
  • You've missed the `_permanence =` in the else block of the if statement. Also, just for pedantry's sake, the correct name is the conditional operator. It just happens to be the only ternary operator in java as no other operator takes three arguments. – EdC Sep 15 '12 at 02:30
  • There is a good point here missed in my answer explaining that using the conditional operator can be a bad thing and explaining the cast. – EdC Sep 15 '12 at 02:34
  • 1
    @BalusC what exactly do you mean by "No surprise, really"? – obataku Sep 15 '12 at 02:46
0

It's called the Java ternary operator (as Hovercraft said), and is used like this:

type variableName = (statement) ? value if statement is true: value if false;
JCOC611
  • 19,111
  • 14
  • 69
  • 90
0

This is the most common way it is used.
[Optional Variable] = ( Boolean Test ) ? (Execute this if True) : (Execute this if false)

EdQ3
  • 31
  • 1
  • 7