Comparing two null objects from two different types

public void m1(Integer f) { ... } public void m1(Float f) { ... } public void main() { m1(null); // error: the method m1(Integer) is ambiguous for the type Main m1((Integer) null); // success } 

Given the above example, we can to some extent recognize that null is typed. So why do the following lines print true ? Of course, o1 and o2 both have no value (i.e. null ), but they are not of the same type ( Integer vs Float ). At first I thought that false would be printed.

 Integer i = null; Object o1 = (Object) i; Float f = null; Object o2 = (Object) f; System.out.println(o1 == o2); // prints true // in short: System.out.println(((Object) ((Integer) null)) == ((Object) ((Float) null))); // prints true 
+6
source share
6 answers

All null values ​​are untyped and equal. You can pass it to other types of links, but this does not make any difference for comparison purposes.

This is not a null value that is typed, but a reference to null that you can enter.

A common question is what happens here

 class A { public static void hello() { System.out.println("Hello World"); } public static void main(String... args) { A a = null; a.hello(); System.out.println("a is an A is " + (a instanceof A)); // prints false. } } 

The compiler sees type a as a , so the static method is called. But the specified value is null and untyped.

The only operations you can perform using null without throwing a NullPointerException are to assign or pass it without examining or comparing with another link.

By the way

In short: the compiler will choose a method based on the type of reference; at runtime, execution is based on the class of the object reference. At run time, null treated as a type of any type or type, or you get a NullPointerException if you try to dereference it.

+8
source

"==" in Java checks to see if it is the same instance, and not just "are they equal?". In Java, there is no concept of multiple null instances. If you compare null to null, you will always get true regardless of type.

The reason you cannot pass null as an argument to a method with the same name as another with different types of parameters is because any of the methods can be a candidate that will be called without an additional type context. Instead of guessing which one might be, it correctly indicates an error.

+1
source

see http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.1

null is a null type. The "null type" has only one value - null .

The null type is a subtype of each reference type. Therefore we can do

 Integer i = null; (Integer)null 

In another word, null is a valid value in each reference type.

(Think of a type as a set of values; value types are the sets to which it belongs; a subtype means "subset".)

+1
source

Given the above example, we can admit that null DO NOT FIND: when you call m1(null) , the compiler cannot determine the type of the actual parameter and cannot decide which method to call. All zeros are equal and not typed, so (null==null)==true .

0
source

Null has no type, but a reference (to null or something else) has a type. We can declare two control variables with different types, but the null they refer to is the same in both cases:

 Integer a = null; Double b = null; 

In your example

 m1((Integer) null); 

the compiler uses the type of reference that it passes to work out an overloaded method to call, not the type of the null value itself.

0
source

In your example, this proves that the compiler cannot identify the type (null) and decide which method to call. So you must explain the type. Also null == null will always be true; no matter what you do, it does not change or give a null type.

post has a long null description.

0
source

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


All Articles