无法检查int是否为空 [英] Can't check if int is null

查看:110
本文介绍了无法检查int是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用字典。每当我想检查字典中是否存在元素时,我都这样做:

  int value = results.get( aKeyThatMayOrMayNotBePresent); 

if(value!= null)
// ...

但是编译器说我无法将 int < nulltype> 进行比较。在这种情况下,检查 null 的正确方法是什么?

解决方案

您将原始值(int)与null进行比较。由于原语不能为null,因此在这种情况下,应使用相应的对象,例如Integer。所以,你应该写

 整数值= results.get(aKeyThatMayOrMayNotBePresent); 


I'm trying to use a dictionary. Whenever I want to check if an element is present in the dictionary, I do this:

int value = results.get("aKeyThatMayOrMayNotBePresent");

if (value != null)
  // ...

But then the compiler says I can't compare an int to a <nulltype>. What's the correct way to check for null in this case?

解决方案

You're comparing a primitive value (int) to null. Since primitives cannot be null, you should use a corresponding object, such as Integer in this case. So, you should write

Integer value = results.get("aKeyThatMayOrMayNotBePresent");

这篇关于无法检查int是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆