This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

null is what type of data type

 
Mohan Vinukonda
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
method passing null to the called function, compiler would take that parmer as what type of paramer???
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: -

 
Mohan Vinukonda
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I imagine that's because "Object" will always be satisfied anyway...
 
Henry Wong
author
Marshal
Pie
Posts: 21117
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also see this item from our SCJP FAQ: What is a most-specific method?
[ September 04, 2007: Message edited by: marc weber ]
 
Mohan Vinukonda
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have understood the concept now... Thnx all of you.

What could be the data type of null ???
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48949
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch mvan le
 
Jaspreet Saran
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21117
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21117
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic