Please tell me the meaning of the appended method? in the if sentence.

Asked 2 months ago, Updated 2 months ago, 1 views

I am having trouble understanding the following expression return and subsequent notation.

private String fm(float value){
    return(value<=0)??"+value:"+"+value;
}

Is it a kind of if sentence?I think so, but I can't search the book with this code because there's no explanation.
? I don't know much about it, so I would appreciate it if you could explain it to me.

java

2022-09-30 14:18

2 Answers

It is called a trinomial operator.

(value<=0)??"+value:"+"+value in one lump,

If (value<=0) is a conditional statement, and if it is true, it returns ? and : (where "+value), and if it is false, : or later (where +value).


2022-09-30 14:18

The Java specification is "conditional operator" in 15.25.Conditional Operator?:.In Japanese, is it conditional operator?The term triplet operator may be better.

Equivalent to the following if statement:

private String fm(float value){
    if(value<=0){
        return""+value;
    } else{
        return"+"+value;
    }
}


2022-09-30 14:18

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.