• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

sending 'null' literal to and from methods

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From Dan Chisholm Casting questions:
class A {}
class B {
static void m(Object x) {System.out.print("Object");}
static void m(String x) {System.out.print("String");}
public static void main(String[] args) {
m(null);
}
}
What is the result of attempting to compile and run the above program?
a. Prints: Object
b. Prints: String
c. Compiler error.
d. Runtime error.
e. None of the Above
ANSWER= B
Explanation:
Section 15.12.2.2 of the Java Language Specification states the following. If more than one method declaration is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen. The informal intuition is that one method declaration is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error. End of quote. In this case, the String type is more specific than the Object type.
-------------------
How can you send a 'null' into a method expecting a String? I know that when a method returns String a compile error results from return null;
Also, if you did:
RunTimeError rt = null;
throw rt; // NullPointerException
//must throw Throwable object, null is not a Throwable
Also, it is odd that null == Object returns true.
From my experience, I have concluded the following:
(1) you can send 'null' as a method parameter
(2) you cannot return 'null' from a method
Any insight on if I am right/wrong will be greatly appreciated.
Thank you.
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At compile time, you can legally use null in any context where an Object is expected. So yes, you can return null from a method that is declared to return an Object, a String, or even an array.
Of course, if the code isn't expecting to see a null at runtime, this may cause a NullPointerException.
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ron:
Can you explain why it has invoked String method
and not Object method?
Thanks
Barkat
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
null is compatible with any Object type. String is more specific than Object, so the String method was called.
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

...(2) you cannot return 'null' from a method


Define a method as:
String m() {return null; }
...
String s = m();
System.out.println("s="+s); //will print 's=null';
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic