• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SCJP Brainteaser (2)

 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, new one:

What is the output when the following code is compiled and run?
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Above,

"A NullPointerException gets printed out."

Change it to,

public class Main {


public static String getNumber(String label, Integer number) {
return "1: " + label + "=" + number.intValue();
}

public static String getNumber(String label, Number number) {
return "2: " + label + "=" + number.intValue();
}

public static void main(String[] args) {
try{
System.out.println(getNumber("Sum", null));
}catch(NullPointerException ss){
System.out.println("I get printed out from catch block");
}
}
}

Prints I get printed out from catch block.

When I change the method call as System.out.println(getNumber("Sum", 10));, the method with Integer argument is called as it is the most relevant one.
and prints out 1: Sum=10

Regards,
Jothi Shankar Kumar. S

[ October 30, 2006: Message edited by: Jothi Shankar Kumar Sankararaj ]
[ October 30, 2006: Message edited by: Jothi Shankar Kumar Sankararaj ]
 
joshua antony
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think we will get NullPointerException at runtime
 
Krishna Srinivasan
Ranch Hand
Posts: 1862
Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It throws Null pointer exception.
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NullPointerException will be thrown.
From Valentin's question, one should also ask themself if one pass 123, 123.2, long, or etc. instead of "null" which method is going to invoke and why.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what about this?

 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Burkhard Hassel,

It will print out Integer: 1, provided that your class Main is public and also you change the Number n = 1 to Integer = 1. It complains at the line Number = 1 as required an int.

Regards,
Jothi Shankar Kumar. S
[ October 30, 2006: Message edited by: Jothi Shankar Kumar Sankararaj ]
 
sai vargheese
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
A simple question.
Which method would get invoked???
one with Integer or Number,
Which method would be invoked.
I tried it this code, the method with Integer got invoked,
But i would like to know how this happens??

Somebody help me out.
 
Eddy Lee Sin Ti
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the code will works and print out "Number:1"

the line "Number n = 1;" is a tricky one and my understanding is that the value 1 will be boxed up into an instance of Integer and cast to the abstract class Number.
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Above,

I think the code will works and print out "Number:1"

the line "Number n = 1;" is a tricky one and my understanding is that the value 1 will be boxed up into an instance of Integer and cast to the abstract class Number.


For me the declaration Number n = 1 says,
found : int
required: javaapplication19.Number
Number n = 1;
1 error
BUILD FAILED (total time: 0 seconds)

Why is that and how come that for you it prints Number : 1? Did you try it out? Please let me know. It's a good question, a real brainteaser.

Regards,
Jothi Shankar Kumar. S
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sai Varghese,

your quote,

A simple question.
Which method would get invoked???
one with Integer or Number,
Which method would be invoked.
I tried it this code, the method with Integer got invoked,
But i would like to know how this happens??


The method with the most significant argument will be invokde. Here Interger which is a subclass of Number is the most significant. So it gets invoked.

Regards,
Jothi Shankar Kumar. S
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,

Jothi Shankar Kumar Sankararaj posted October 30, 2006 04:34 AM
I
t will print out Integer: 1, provided that your class Main is public and also you change the Number n = 1 to Integer = 1. It complains at the line Number = 1 as required an int.





That't true - for Java 1.4
But it wouldn't compile in Java 1.4 anyway because of the vararg in the main method.
I thought that it should be clear then, that it's Java5.

Classes don't have to be public to run their main methods!

Only if you like to start their main method from a different package.


Special service: the same for java 1.3 - runs 1.4 as well )-;





(and further down the thread)
The method with the most significant argument will be invokde. Here Interger which is a subclass of Number is the most significant. So it gets invoked.

Ha! Got you!


No. reference type of n is Number, so the number version of the overloaded method is called. Object type doesn't matter here. Object type only matters in overridden methods.

Bu
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bu,

I compiled the program using netBeans5.0 which works with jdk5 and gave me the error.

For me the declaration Number n = 1 says,
found : int
required: javaapplication19.Number
Number n = 1;
1 error
BUILD FAILED (total time: 0 seconds)

I did not get your statement as to how one can compile without a public class in a package? Can you please explain me.

Regards,
Jothi Shankar Kumar. S
 
anvesh charuvaka
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers

The above code gets compiled and prints Number:1

In the assignment in the main metod
Number n = 1;

the int literal is first Boxed to an Integer followed by a Reference Widening Cast which makes it possible to assign it to Number

The method with Number as argument is called because when calling a method implicit narrowcast does not Occur.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jothi,

compiled with NetBeans ... the first one (the one with the varargs) or the second one?
With Eclipse and also the original JDK15 it compiles nicely.

Don't need to be public:
One file, called DoesntMatter.java
two non-public classes
any package(doesn't matter here)
javac DoesntMatter.java
java Main

or if you manage to type
java €Number on the console ... different topic...

will produce Number:1

Maybe, (don't know) NetBeans does not allow multiple classes in one file?
Or has problems with the € symbol?
I'm guessing here

Yours,
Bu.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright guys ! It does throw a NullPointerException !

Now:
1) how about modifying the call to getNumber as follows: getNumber("Sum", new Integer(1));
2) how about modifying the call to getNumber as follows: getNumber("Sum", new Number(1));

What gets printed out in each case ? Try compiling and executing mentally before throwing it in the compiler!
 
Rancy Chadha
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Valentine,

If we change add following lines

1) getNumber("Sum", new Integer(1));
It will compile and will give output 1: sum=1

2) getNumber("Sum", new Number(1));
Addition of this line will result in COMPILATION ERROR
The reason it happens is pretty obvious Number class is abstract class
and abstract class cannot be instantiated.

Thanks,
Rancy
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good job guys !!

The key here was to know how methods get resolved when executing an overloaded method.

Relevant pointers to the JLS:
15.12.2 Compile-Time Step 2: Determine Method Signature
15.12.3 Compile-Time Step 3: Is the Chosen Method Appropriate?
15.12.4 Runtime Evaluation of Method Invocation

You might also want to have a look at an older article I wrote a couple years ago on the same subject: JLS 15.12 (Method Invocation Expressions) in Plain English

Thanks to those who participated. Stay tuned...

Previous brainteasers:
SCJP Brainteaser 1
 
sai vargheese
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jothi Shankar!!
Thanks to Valentin for starting this thread.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic