switch 語句idea報錯Constant expression required

這裏有很多人的情況是因爲switch語句中沒有使用常量,也就是使用自定義常量時沒有添加final,只要加上即可。

但是我遇到的情況比較特殊,就是常量封裝是我使用了裝箱的對象而不是使用基礎數據類型。

package com.shengxi.wu.frame.util.constant;


/**
 * @author y
 */
public class TweetType {

    /**
     * 文本
     */
    public static final Integer TEXT_TYPE = 0;

    /**
     * 圖片
     */
    public static final Integer IMAGE_TYPE = 1;
    /**
     * 文章
     */
    public static final Integer ARTICLE_TYPE = 2;

    /**
     * 視頻
     */
    public static final Integer VLOG_TYPE = 3;
}

這裏只要使用基礎數據類型即可。

package com.shengxi.wu.frame.util.constant;


/**
 * @author y
 */
public class TweetType {

    /**
     * 文本
     */
    public static final int TEXT_TYPE = 0;

    /**
     * 圖片
     */
    public static final int IMAGE_TYPE = 1;
    /**
     * 文章
     */
    public static final int ARTICLE_TYPE = 2;

    /**
     * 視頻
     */
    public static final int VLOG_TYPE = 3;
}

這裏我的理解就是因爲如果使用封裝對象雖然在對象體是一致的,但是對象頭不是會有一個誤差。所以需要使用equal進行判斷,switch在1.8還不予許如此操作。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章