• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

mock exam question

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I came across this question in one of the mock exams.
I marked 2 as my ans, can anyone pls explain why the ans is 3 and not 2.
question from mock exam
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.
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I checked the null instnaceof with String and Object . It says it is neither String nor Object; what is the difference between the null passed to a method call and when checking directly for its runtime type ? This code prints "String version null Not String, Not Object" ?? why ?
class AQuestion{
public void method(Object o){
System.out.println("Object Verion"+o);
}
public void method(String s){
System.out.println("String Version"+s);
}
public static void main(String args[]){
AQuestion question = new AQuestion();
question.method(null);
if( null instanceof String ){
System.out.println("String Version");
}
if( null instanceof Object){
System.out.println("Object Version");
}
else{
System.out.println("Not String, Not Object");
}
}
}
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Dan,
if there were a String conversion from every other type to String type, even a null... then how could this code below prints.. "int version"
it should print "string version" right,... pls explain... am i missing something..
 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dan, I think you�re wrong. Just because there is a conversion from any other type to String, it doesn�t mean that it will be used all the time. What happens in bani�s problem is that the compiler chooses the most specific method to run, like this:
JLS 15.12.2.2:


The precise definition is as follows. Let m be a name and suppose that there are two declarations of methods named m, each having n parameters. Suppose that one declaration appears within a class or interface T and that the types of the parameters are T1, . . . , Tn; suppose moreover that the other declaration appears within a class or interface U and that the types of the parameters are U1, . . . , Un. Then the method m declared in T is more specific than the method m declared in U if and only if both of the following are true:

  • T can be converted to U by method invocation conversion.
  • Tj can be converted to Uj by method invocation conversion, for all j from 1 to n.


  • here�s an example from the same section of the jls that shows this exact thing:

    Consider the example:

    This example produces an error at compile time. The problem is that there are two declarations of test that are applicable and accessible, and neither is more specific than the other. Therefore, the method invocation is ambiguous.
    If a third definition of test were added:
    static void test(ColoredPoint p, ColoredPoint q) {
    System.out.println("(ColoredPoint, ColoredPoint)");
    }
    then it would be more specific than the other two, and the method invocation would no longer be ambiguous.


    So since String is more specific than Object, because String is a sub-class of Object, the compiler will choose the String version method.
    In srinivas� example, since the argument is an int it chooses the int version, following the same logic.
    Hope that helps, if you have anymore doubts please let me know.
    Francisco
    [ July 16, 2002: Message edited by: Francisco A Guimaraes ]
     
    bani kaali
    Ranch Hand
    Posts: 42
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you Francisco and Dan,for the detailed explainations.
    bani
     
    Ranch Hand
    Posts: 1865
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Yes, my previous two posts were definitly wrong. I think the easiest way to verify that java just picks the lowest subclass is with the following program.

    The output is B.
    The following code illustrates that Java has no preference for promoting null to a String over any other type.

    The above code will not compile due to the ambiguity.
    Of course, if you remove the ambiguity then it compiles and prints String.
     
    Ranch Hand
    Posts: 104
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    i found another example in which the most specific method is called.

    in this case "java.io.FileNotFoundException version" is printed since FileNotFoundException class is subclass of IOException which in turn is subclass of Object. hence the method with FileNotFoundException is called which the more specific.
    -zarina
     
    There are no more "hours", it's centi-days. They say it's better, but this tiny ad says it's stupid:
    Smokeless wood heat with a rocket mass heater
    https://woodheat.net
    reply
      Bookmark Topic Watch Topic
    • New Topic