It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes explain a code from k&b Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "explain a code from k&b" Watch "explain a code from k&b" New topic

explain a code from k&b

Ronald Ross

Joined: Jul 06, 2010
Posts: 3

can any explain this code the output (as the k&b says) is 434 but i have no clue why
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Chad Michaels
Ranch Hand

Joined: Jun 25, 2010
Posts: 42
Hello Ronald,

By the way, I think you intended to use (Object o) and not (Object... o).

Anyway, here's my guess. In terms of method overriding, the "least expensive" method always wins. A perfect match is cheaper than upcasting. Upcasting is cheaper than converting to a var-args.

So, when you pass an object of type "A[ ]" to two overloaded methods of: shifter( A[ ]... a2 ) and shifter( Object o ), your argument of "A[ ]" simply needs to upcast. Remember, "A[ ]..." is not a perfect match because a perfect match would be only "A[ ]". So now, does it upcast or convert to a var-args? It's cheaper to upcast because "A[ ]" IS-A Object. If I had to guess, you could remove the method "shifter( Object o )", the code will still compile and this time invoke (A[ ]... a2). In any case, that is your first 4.

If you pass "B[ ]", the obvious perfect match is: shifter( B[ ] b1 ), which is your "3". That probably comes as no suprise.

Finally, you are passing "int" 7. The only logical choice is an upcast to "Object". Therefore, shifter( Object o ) wins out.

That's 434. Again, I'm not an expert... just a guess.
Deepak Bala

Joined: Feb 24, 2006
Posts: 6662

Hi Ron,

Please do not create duplicate topics. Instead edit the post you made using the EDIT button and put the code inside code tags

SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
aarish sumem

Joined: Jul 05, 2010
Posts: 3
Hi Ronald,

The correct code is below as described by Micheal...

see the commented code.
code commented 'Call1' passing an argument 'aa' which is an array, there are two option we have for this argument
1)static void shifter(A[]... a2){s+="1";} //keep remeber that 'A[]... a2' is var-args and var-args has the lowest priority.
//so compiler will select the second option.
2)static void shifter(Object o ){s+="4";}//this is eligible because array are object in java. so 4 will be appended to result.

code commented 'Call2' passing an array 'bb', there are two possible methods signatures that can receive the call.

1)static void shifter(B[]... b1){s+="2";} //this will not be selected here because var-agrs will be choosen last, if no option is there.
2)static void shifter(B[] b1){s+="3";} //prefered choice, append 3 to result.

code commented 'Call3' passing 7 to method, and 7 is int by default in java.
We dont have any method that can receive an int argument, compiler will autobox this int to Integer then widen the Integer to Object and call the method with Object argument, that results in '-434'.
I think it is enough for you.

Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446

The output of this program is as follows

Is there any problem with my JVM?

this is looking like it is printing the hashCode of the string object s
what does this output mean?

SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
sumit kothalikar
Ranch Hand

Joined: Apr 15, 2010
Posts: 91

Prasad Kharkar wrote:
1. F:\Java\Javaranch problems\QOTD>java ComingThru
2. [Ljava.lang.String;@1729854

F:\Java\Javaranch problems\QOTD>java ComingThru [Ljava.lang.String;@1729854

That is because he is printing public static void main(String [] s) main method s rather than String s ="-"

@Ronald Ross: please check the argument of your main method

correct me if i am wrong

Thanks & Regards
Sumit Kothalikar
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446

@ sumit
thank you dear
that cleared the doubt
I was out of mind,could not notice the main method String array variable is also s
I wanted to share another thing here
here in the output
[L Sysmbol means that the object we are printing is Array object
java.lang.String is the type of the array
and the
@1729854 means that the object is At the location 1729854 i.e. that is the hashcode of the object
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

I agree. Here's the link:
subject: explain a code from k&b
It's not a secret anymore!