int类型在接收null会报错,需要使用Java包装类型Integer,且Integer不能equal String字符串

代码语言:javascript代码运行次数:0运行复制int类型在接收null会报错,需要使用Java包装类型Integer,且Integer不能equal String字符串代码语言:javascript代码运行次数:0运行复制packa

int类型在接收null会报错,需要使用Java包装类型Integer,且Integer不能equal String字符串

代码语言:javascript代码运行次数:0运行复制
int类型在接收null会报错,需要使用Java包装类型Integer,且Integer不能equal String字符串

代码语言:javascript代码运行次数:0运行复制
package com.example.core.mydemo.json2;

/**
 * int类型在接收null会报错,需要使用Java包装类型Integer
 */
public class IntegerNullTest {
    public static void main(String[] args) {
        Integer aaa = null;
        //output: total=100
        System.out.println("total=" + calc(aaa));

        //Exception in thread "main" java.lang.NullPointerException
        //    at com.example.core.mydemo.json2.IntegerNullTest.main(IntegerNullTest.java:10)
//        System.out.println("total2=" + calc2(aaa));

        //unsame  字符串与Integer比较,JAVA规范错误写法
        Integer bbb = 100;
        if("100".equals(bbb)){
            System.out.println("same");
        }else{
            System.out.println("unsame");
        }

    }

    private static Integer calc(Integer aaa) {
        return 100;
    }

    /**
     * int类型在接收null会报错
     * @param aaa
     * @return
     */
    private static Integer calc2(int aaa) {
        return 100;
    }

}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。 原始发表:2023-06-05,如有侵权请联系 cloudcommunity@tencent 删除字符串javaintintegernull

发布者:admin,转转请注明出处:http://www.yc00.com/web/1754978191a5223702.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信