Java autoboxing and ternary operator madness

后端 未结 4 826
感动是毒
感动是毒 2020-11-30 07:41

Just spent a frustrating couple of hours debugging this code:

    LinkedHashMap rsrqs = new LinkedHashMap();
           


        
4条回答
  •  被撕碎了的回忆
    2020-11-30 08:09

    Well, Integer.valueOf(String) returns an Integer and -1 is a primitive int. The first example is forced to unbox because one term is a primitive. You could also have used

    Integer boxedRsrq = boxedPci != null ? 
        rsrqs.get(boxedPci.toString()) : (Integer) -1;
    

    which would have boxed the -1.

提交回复
热议问题