Forums Register Login

explain a code from k&b

+Pie Number of slices to send: Send


can any explain this code the output (as the k&b says) is 434 but i have no clue why
+Pie Number of slices to send: Send
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.

+Pie Number of slices to send: Send
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?

+Pie Number of slices to send: Send
 


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
+Pie Number of slices to send: Send
@ 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
+Pie Number of slices to send: Send
pie. tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1284 times.
Similar Threads
instanceof operator
Explanation ....
Operators and Precedence
Static oveloaded methods with different argument list
fwd or backward ,don't know which reference??
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 06:59:35.