aspose file tools*
The moose likes Beginning Java and the fly likes null is what type of data 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 » Beginning Java
Bookmark "null is what type of data type" Watch "null is what type of data type" New topic
Author

null is what type of data type

Mohan Vinukonda
Ranch Hand

Joined: Jul 28, 2005
Posts: 32
method passing null to the called function, compiler would take that parmer as what type of paramer???
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
If there is only one overload of the named method that matches the other parameters (the ones that aren't null at compile time), then that one is chosen. If there is more than one, you get a compiler error.

You can tell the compiler which one you want, by casting your null to an appropriate type. For example: -



Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Mohan Vinukonda
Ranch Hand

Joined: Jul 28, 2005
Posts: 32
static void method(Object obj){
System.out.println("Object");
}
static void method(String str){
System.out.println("String");
}

public static void main(String args[]){
method(null);
}


for this I am not getting compilation error with out type cast
Chris Corbyn
Ranch Hand

Joined: Jan 14, 2007
Posts: 114
I imagine that's because "Object" will always be satisfied anyway...
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18108
    
  39

Originally posted by MohanRao Vinukonda:
static void method(Object obj){
System.out.println("Object");
}
static void method(String str){
System.out.println("String");
}

public static void main(String args[]){
method(null);
}


for this I am not getting compilation error with out type cast


If there are two cases to choose from, the compiler will first try to pick the more specific case. In this case, String will be picked over Object.

Otherwise, Peter is correct. You will have to cast to help the compiler, or it will complain.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Also see this item from our SCJP FAQ: What is a most-specific method?
[ September 04, 2007: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Mohan Vinukonda
Ranch Hand

Joined: Jul 28, 2005
Posts: 32
I have understood the concept now... Thnx all of you.

What could be the data type of null ???
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by MohanRao Vinukonda:
...What could be the data type of null ???

Actually, null is a special type. According to JLS - 3.10.7 The Null Literal...
The null type has one value, the null reference, represented by the literal null... A null literal is always of the null type.


A null reference can be upcast to any type of Object.
mvan le
Greenhorn

Joined: Feb 16, 2012
Posts: 1
Mohan Vinukonda wrote:method passing null to the called function, compiler would take that parmer as what type of paramer???


Since Java 1.6, the "javax.lang.model.type.NullType" interface represents the null type. Ie. This is the type of the expression ``null''.

Example:


Would produce the following output:

    No argument constructor
    NullType constructor
    Object constructor
    NullType constructor


Notice that the nullConstructor4 instance created from "new NullConstructor(null);" (where null is not explicitly cast) selects the constructor with parameter type NullType rather than Object.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
And welcome to the Ranch mvan le
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: null is what type of data type
 
Similar Threads
What is null ?
Confused: request.getParameter("userName") returns null
incomparable types: boolean and null
Exception Error Processing JSP Page
Values of Hash Map to be 2-dimensional array