What is the Java ?: operator called and what does it do?

前端 未结 16 1693
轮回少年
轮回少年 2020-11-21 05:27

I have been working with Java a couple of years, but up until recently I haven\'t run across this construct:

int count = isHere ? getHereCount(index) : getAw         


        
16条回答
  •  隐瞒了意图╮
    2020-11-21 06:23

    Actually it can take more than 3 arguments. For instance if we want to check wether a number is positive, negative or zero we can do this:

    String m= num > 0 ? "is a POSITIVE NUMBER.": num < 0 ?"is a NEGATIVE NUMBER." :"IT's ZERO.";
    

    which is better than using if, else if, else.

提交回复
热议问题