1. Overview
1.概述
In this tutorial, we’ll take a closer look at the Java error, “int cannot be dereferenced”. First, we’ll create an example of how to produce it. Next, we’ll explain the leading cause of the exception. And finally, we’ll see how to fix it.
在本教程中,我们将仔细研究Java错误 “int不能被解读”。首先,我们将创建一个如何产生它的例子。接下来,我们将解释导致该异常的主要原因。最后,我们将看到如何修复它。
2. Practical Example
2.实例
Now, let’s see an example that generates a compilation error, “X cannot be dereferenced”.
现在,让我们看一个例子,它产生了一个编译错误,”X不能被解读”。
Here, X represents one of the eight Java primitives, namely int, byte, short, long, float, double, boolean, and char.
这里,X代表八个Java 基元中的一个,即int、byte、short、long、float、double、boolean和char。
First, let’s create a class Test and compare an int to some other value:
首先,让我们创建一个Test类,将一个int与其他值进行比较。
int x = 10;
System.out.println(x.equals(10));
When compiling the code from the terminal, we’ll get the error:
当从终端编译代码时,我们会得到这样的错误。
$ javac Test.java
Test.java:8: error: int cannot be dereferenced
System.out.println(x.toString());
^
1 error
Also, modern IDEs like Eclipse and IntelliJ will show an error without even compiling:
另外,像Eclipse和IntelliJ这样的现代IDE甚至不需要编译就会显示错误。
3. Cause
3.原因
In Java, a reference is an address to some object/variable. Dereferencing means the action of accessing an object’s features through a reference. Performing any dereferencing on a primitive will result in the error “X cannot be dereferenced”, where X is a primitive type. The reason for this is that primitives are not considered objects — they represent raw values:
在Java中,引用是某个对象/变量的地址。解除引用是指通过引用访问一个对象的特征的行为。在一个基元上执行任何解除引用的操作都会导致错误 “X不能被解除引用”,其中X是一个基元类型。这是因为primitives不被视为对象 – 它们代表原始值。
int x = 10;
System.out.println(x.equals(10));
When building the code from the terminal, we’ll get the error “int cannot be dereferenced”.
当从终端构建代码时,我们会得到错误”int不能被解读”。
However, with Object, it works fine:
然而,使用Object时,它可以正常工作。
Object testObj = new Object();
testObj.toString();
Here, testObj is an object, and dereferencing happens on calling toString() with the . operator on testObj. This will not give any error as testObj is an object and, thus, dereferencing will work.
这里,testObj是一个对象,解除引用发生在对testObj.使用.操作符调用toString()时。这不会产生任何错误,因为testObj是一个对象,因此,解除引用将会成功。
4. Solution
4.解决办法
In our example, we need to check the equality of the two values.
在我们的例子中,我们需要检查两个值的相等。
The first solution to our problem is to use == instead of equals() for primitive types:
我们问题的第一个解决方案是对原始类型使用==而不是equals()。
int x = 10;
System.out.println(x == 10);
When we run the code, it will print “true”.
当我们运行该代码时,它将打印出 “true”。
The second solution is to change the primitive to a wrapper class.
第二种解决方案是将基元改为包装类。
Java provides wrapper class objects for each primitive type.
Java为每个原始类型提供包装类对象。
For instance, we can convert primitive types to a wrapper object if we must use equals():
例如,如果我们必须使用equals(),我们可以将原始类型转换为一个封装对象。
Integer x = 10;
System.out.println(x.equals(10));
This error does not have a one-size-fits-all solution. Depending on the use case, we may use either of the above two solutions.
这个错误没有一个放之四海而皆准的解决方案。根据不同的使用情况,我们可以使用上述两种解决方案中的任何一种。
5. Conclusion
5.总结
We’ve explained Java’s “int cannot be dereferenced” error. Then, we discussed how to produce the error and the cause of the exception. Lastly, we discussed a solution to resolve the error.
我们已经解释了Java的“int cannot be dereferenced”错误。然后,我们讨论了如何产生该错误以及该异常的原因。最后,我们讨论了解决该错误的方案。