admin管理员组文章数量:1794759
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本文标签: int类型在接收null会报错,需要使用Java包装类型Integer,且Integer不能equal String字符串
版权声明:本文标题:int类型在接收null会报错,需要使用Java包装类型Integer,且Integer不能equal String字符串 内容由林淑君副主任自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.xiehuijuan.com/baike/1754978196a1708953.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论