Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

explain a code from k&b

 
Ronald Ross
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


can any explain this code the output (as the k&b says) is 434 but i have no clue why
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
UseCodeTags
 
Chad Michaels
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
aarish sumem
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
sumit kothalikar
Ranch Hand
Posts: 91
Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ 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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic