装箱值被拆箱,然后立即重新装箱

2 java spring-mvc

我正在解决 SonarQube 问题,在那个问题中我面临一个波纹管错误,但无法理解这是什么类型的错误,

这是我的实体类

@Entity
@Cacheable
@DynamicUpdate
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@Table(name = Vendor.TABLE_NAME)
public class Vendor 
{


    @Column(name = VENDOR_MODIFIED_BY_FOREIGN_KEY, nullable = true)

    private Integer modifiedBy;



    public Integer getModifiedBy() {

        return modifiedBy == null ? 0 : modifiedBy;

//Boxed value is unboxed and then immediately reboxed in com.bostonbyte.thelift.entities.vendors.Vendor.getModifiedBy() 




    }



    public void setModifiedBy(Integer modifiedBy) {

        this.modifiedBy = modifiedBy;

    }
Run Code Online (Sandbox Code Playgroud)

我在

 public Integer getModifiedBy() {

                return modifiedBy == null ? 0 : modifiedBy;
Run Code Online (Sandbox Code Playgroud)

你能告诉我这是什么类型的错误吗?

Jen*_*ens 6

这意味着,modifiedBy它将被取消装箱,将其与 primitivv 类型进行比较,而不是装箱,因为返回值是一个对象。

用:

return modifiedBy == null ? Integer.valueOf(0) : modifiedBy;
Run Code Online (Sandbox Code Playgroud)

并且错误应该消失。


小智 0

这意味着,将原始数据类型自动转换为其等效的包装类型称为装箱,相反的操作称为拆箱。在您的代码中modifiedby,为了与 int value(0) 即原始数据类型进行比较,在您的代码中进行拆箱,在比较值后,它将再次装箱,因为它的返回类型是 Wrapper 类,我认为您应该采用@jens 的第一个答案。