Why can incompatible types be compared by reference in Java?

Check out this snippet:

List<Integer> c = new ArrayList<>(); Map<String,Boolean> m = new HashMap<>(); if( c == m ) //no error here! WHY? { c = m; //"Incompatible types" error, as expected. m = c; //"Incompatible types" error, as expected. } 

Why does c == m not give an error?

I am using javac from jdk1.8.0.20, and I have no reason to suspect that it is ignoring the specification of the java language, so this is a pretty absolute level of confidence in the specification, therefore:

What is the purpose / purpose / usefulness of something similar to be permitted by the specification?

+6
source share
5 answers

This is specifically allowed because List and Map are interfaces.

We could imagine some class

 // (please only imagine) class ListMap implements List, Map {...} 

The legality of the validity of reference equality ( 15.21.3 ) is the same as for the reference type ( 5.5.1 ). In short, since you can usually use some kind of reference type and interface, you can also compare reference equality of any type with an interface.

Resolution seems more useful in the context of smaller interfaces such as Comparable , Serializable , Iterable , etc., where a class is more likely to implement more than one.

+2
source

Just because types are irreversible does not mean that they are not equal objects. If the types are "Not Convertible", this means that casting is actually converted to check the type.

 interface Outputer extends Consumer<String>, Serializable { } Outputer out = System.out::println; Consumer<String> cs = out; Serializable s = out; System.out.println(s == cs); // prints true // s = cs; // Inconvertible types, doesn't compile s = (Serializable) cs; // compiles and runs fine. 

cs and s are non-convertible types, but they point to the same object and this prints true

+4
source

The error Incompatible types appears because the Assignment conversion operation starts when the destination starts. What does he do:

Assignment conversion occurs when the value of an expression is assigned (ยง15.26) to a variable: the type of the expression must be converted to the type of the variable.

If the conversion fails, then a compile-time error is generated.

However, a compile-time error does not occur when == operator is executed.

+1
source

In Java, when you have objects, the system uses pointers. And when you use == on two objects, it compares their pointers. In other words, it checks to see if two pointers point to the same object in memory. This is always a safe check.

In addition, it should be noted that when inheritance (and polymorphism ) is involved, it is possible to have several pointers of different types, this indicates the same object. Of course, this was not the case in your example. But, as I said earlier, it is harmless to check whether two pointers point to the same object, since this check does not make any assumptions about the corresponding classes.

+1
source

When you write

 if(c==m) 

you are simply checking @hashcode, which might be the same for two objects under certain circumstances so that you cannot get any error on this line!

-5
source

Source: https://habr.com/ru/post/983108/


All Articles