• 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

help help

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read the code below. Will be the result of attempting to compile and run the code below.

public class AQuestion
{
public void method(Object o)
{
System.out.println("Object Verion");
}
public void method(String s)
{
System.out.println("String Version");
}
public static void main(String args[])
{
AQuestion question = new AQuestion();
question.method(null);
}
}
Answers
1.The code does not compile.
2.The code compiles cleanly and shows "Object Version".
3.The code compiles cleanly and shows "String Version"
4.The code throws an Exception at Runtime.

Answer it soon with explanation
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler will choose the method that is most specific.
 
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting question. After searching through the language reference this is what I found


The null type has one value, the null reference, represented by the literal null,which is formed from ASCII characters. A null literal is always of the null type.



I believe this is what makes null to have close affinity with "String" object.
 
Sabber bhatia
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey Thambu

can u explain it more
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can suggest that you read a book about Java - the part about overloaded methods and how the compiler chooses which method to run. The information is also in the Java Language Specification. Have you read a book about Java before? You can also look in our list of Frequently Asked Questions, at the end.
[ June 28, 2006: Message edited by: Barry Gaunt ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I believe this is what makes null to have close affinity with "String" object.



No - this only about the representation in the source code. To refer to the null reference you use the null literal which is made up of the ASCII characters 'n','u','l'.
[ June 28, 2006: Message edited by: Barry Gaunt ]
 
Purushoth Thambu
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it. I completely lost touch with the basis guess I should make it a point to re-read the spec every 6 months!.
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may find answer by searching JavaRanch and checking SCJPFaq.
Here is one FAQ in SCJPFaq.
 
reply
    Bookmark Topic Watch Topic
  • New Topic