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 Spring in Action this week in the Spring 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: 18896
    
  40

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: 39409
    
  28
And welcome to the Ranch mvan le
Jaspreet Saran
Greenhorn

Joined: Mar 01, 2014
Posts: 8
Sir i want to ask one question related to this...
like int, Boolean,etc comes under category of primitive types in java, does nullType come under reference type?
Or its just the third category of Java Data Types.
I have read somewhere that Java data types are classified into three categories: primitive, reference and nullType...
i do not agree with it. Boolean is also a special type, which can hold only two special literal true or false. but we cannot say its not primitive but some other special type..
similarly null type is a reference type but is special which can hold only one literal null.
and as you all said it can be upcast to any other reference type.
please clear my concept...
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Jaspreet Saran wrote:Sir i want to ask one question related to this...
like int, Boolean,etc comes under category of primitive types in java, does nullType come under reference type?
Or its just the third category of Java Data Types.
I have read somewhere that Java data types are classified into three categories: primitive, reference and nullType...
i do not agree with it. Boolean is also a special type, which can hold only two special literal true or false. but we cannot say its not primitive but some other special type..
similarly null type is a reference type but is special which can hold only one literal null.
and as you all said it can be upcast to any other reference type.
please clear my concept...


An int variable is of a primitive type. A Boolean variable is of a reference type. And since only objects cam implements interfaces, a NullType variable is also of a reference type.

Henry
Jaspreet Saran
Greenhorn

Joined: Mar 01, 2014
Posts: 8
Henry Wong wrote:
An int variable is of a primitive type. A Boolean variable is of a reference type. And since only objects cam implements interfaces, a NullType variable is also of a reference type.

Henry


A boolean variable is of a reference type? ....please explain me how?.... A variable declared as a boolean does not hold reference to some object...instead it hold primitive literal value..true or false.
whatever, may be i am wrong here... i have started java just 3- 4 months ago...so please explain.... your line
A Boolean variable is of a reference type.
.
And thanks sir one question is clear that null type is a reference type.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Jaspreet Saran wrote:
Henry Wong wrote:
An int variable is of a primitive type. A Boolean variable is of a reference type. And since only objects cam implements interfaces, a NullType variable is also of a reference type.

Henry


A boolean variable is of a reference type? ....please explain me how?.... A variable declared as a boolean does not hold reference to some object...instead it hold primitive literal value..true or false.
whatever, may be i am wrong here... i have started java just 3- 4 months ago...so please explain.... your line



I think you need to get your capitalizations correct -- Boolean and boolean are two different things. From your response (when compared to your previous question), you seem to be mixing them.

Henry
Jaspreet Saran
Greenhorn

Joined: Mar 01, 2014
Posts: 8
hmm I am sorry sir, actually I was thinking of int, char, boolean, etc....so I didn't noticed it.....so sorry.
But thankful to you and to the JavaRanch.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: null is what type of data type