明确将NULL值设置为数组元素 [英] Explicitly setting NULL value to an array element

查看:65
本文介绍了明确将NULL值设置为数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我为什么将显式设置 NULL 值设置为数组元素时会出现编译错误吗?

Can someone tell me why am I getting compilation error for explicitly setting NULL value to an array element?

int[] a = new int[5];

a[0] = 1;
a[2] = 'a';
a[3] = null; //Compiler complains here


for (int i : a) System.out.println(i);

我假设是因为它是一个int数组,允许的字面值是 0 ,而不是 NULL .我说的对吗?

I am assuming because its an int array and the literal value allowed is 0 and not NULL. Am I right?

推荐答案

正确. int 是原始类型,这意味着它包含一个显式值(从-2 ^ 31到2 ^ 31-1的数字),而不是引用,因此不能为>空.如果您确实需要 null 值,请使用 Integer .

Correct. int is a primitive type, which means that it contains an explicit value (a number from -2^31 to 2^31-1), and not a reference, so it can't be null. If you really need null values, use Integer.

这篇关于明确将NULL值设置为数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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