Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

null is what type of data type

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
method passing null to the called function, compiler would take that parmer as what type of paramer???
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I imagine that's because "Object" will always be satisfied anyway...
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 75637
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch mvan le
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
This guy is skipping without a rope. At least, that's what this tiny ad said:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic