aspose file tools*
The moose likes Java in General and the fly likes how to create a null type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to create a null type" Watch "how to create a null type" New topic
Author

how to create a null type

yogesh srinivasan
Ranch Hand

Joined: Jun 08, 2007
Posts: 55
Please read this line from "CHAPTER 5 Conversions and Promotions" in the link "http://www.infospheres.caltech.edu/resources/langspec-1.0/5.doc.html"

it says
-------------------------------------------------------------------------
5.1.4 Widening Reference Conversions
The following conversions are called the widening reference conversions:

From any class type S to any class type T, provided that S is a subclass of T. (An important special case is that there is a widening conversion to the class type Object from any other class type.)
From any class type S to any interface type K, provided that S implements K.
From the null type to any class type, interface type, or array type.
From any interface type J to any interface type K, provided that J is a subinterface of K.
From any interface type to type Object.
From any array type to type Object.
From any array type to type Cloneable.
From any array type SC[] to any array type TC[], provided that SC and TC are reference types and there is a widening conversion from SC to TC.

-----------------------------------------------------------------------

Please explain the Bolded line of the above paragraph and if possible explain it with an example

Thanks........
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

If I'm not mistaken :
MyObject o = null;
MyInterface i = null;
MyArray[] a = null;


[My Blog]
All roads lead to JavaRanch
Jay Damon
Ranch Hand

Joined: Jul 31, 2001
Posts: 279
Many developers are unaware that you may cast a null to ANY object. To extend Satou's example, the following is perfectly legal code:

You are, in effect, casting "from the null type to any class type, interface type, or array type".

The most obvious use of this is casting a value returned from a collection class, e.g.:

This code will not fail with a NullPointerException even if the key value is null unlike the following code:

which will throw a NullPointerException if the key value is null, i.e. you cannot invoke toString() on a null value. Therefore, the first example is safer in that you only need test for null after retrieving the value from the collection.

Another potential use for this capability is if you have constructors or overloaded methods that have a like number of parameters, e.g. each takes a single parameter, either an Integer or a String. You cannot invoke either constructor or method directly with null because the compiler will not know which you mean. But you may code something like the following:

to force the compiler to invoke the constructor (in this example) that accepts a single String parameter.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to create a null type
 
Similar Threads
Need Help
Widening Reference Conversation
Forbidden Conversions
null as arg in mehods
passing an instance of Thread rather than Runnable to new Thread instance