java - Comparison of two null objects from two different types -
public void m1(integer f) { ... } public void m1(float f) { ... } public void main() { m1(null); // error: method m1(integer) ambiguous type main m1((integer) null); // success }
given above example, can admit in ways null
typed. why following lines print true
? sure o1
, o2
both have no value (i.e. null
), aren't same type (integer
vs float
). firstly thought false
have been printed.
integer = 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
all null
values untyped , equal. can pass different reference types makes no difference comparison purposes.
it not null
value typed reference null can typed.
a common question happens here
class { public static void hello() { system.out.println("hello world"); } public static void main(string... args) { a = null; a.hello(); system.out.println("a " + (a instanceof a)); // prints false. } }
the compiler sees type of a
a
static method called. value referenced null
, untyped.
the operations can perform null
without causing nullpointerexception assign or pass without examining or comparing reference.
btw
in short: compiler select method based on type of reference, @ runtime execution based on class of object referenced. @ runtime null
treated type or no type or nullpointerexception if try dereference it.
Thank you for sharing this powerful article, your explanation is clear and very easy to understand. Please kindly visit our site to get more information about IT solution.
ReplyDeleteOnline Business