Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

kathy sierra self test question?

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

3. class A { }
4. class B extends A { }
5. public class ComingThru {
6. static String s = "-";
7. public static void main(String[] args) {
8. A[] aa = new A[2];
9. B[] ba = new B[2];
10. sifter(aa);
11. sifter(ba);
12. sifter(7);
13. System.out.println(s);
14. }
15. static void sifter(A[]... a2) { s += "1"; }
16. static void sifter(B[]... b1) { s += "2"; }
17. static void sifter(B[] b1) { s += "3"; }
18. static void sifter(Object o) { s += "4"; }
19. }





What is the result?
A. -124
B. -134
C. -424
D. -434
E. -444
F. Compilation fails


can someone tell me what is happening in this program.....i couldn't understand this program at all....
 
Marshal
Posts: 76109
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where is it from?
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And please UseCodeTags for posting code. Don't use the quote tags.
 
Hennry Smith
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Where is it from?



its from kathy sierra book.....the question is from 2nd chapter(object orientation) self test.....question no 15 on page 169.....please explain the code as i couldn't understand it......
 
Hennry Smith
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Answer:
® ✓ D is correct.

In general, overloaded var-args methods are chosen last. Remember that arrays
are objects. Finally, an int can be boxed to an Integer and then "widened" to an Object.
A, B, C, E, and F are incorrect based on the above information.



Can someone please explain how i am confused..???
 
Campbell Ritchie
Marshal
Posts: 76109
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which book? Kathy Sierra has written and co-authored several books.
 
Hennry Smith
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Which book? Kathy Sierra has written and co-authored several books.




Sun Certified Programmer for Java 6 study Guide for exam 310 065

by Kathy sierra and Bert Bates
 
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is so confusing about it.
1.Remember one rule Array is an Object in Java(everything is an object).
2.widening bit boxing, and boxing bit vararg. So in general vararg is last option when no other option is present for compiler.
3.You can never widen and then box because of this IS-A test fail, But you can box and then widen it.

here in this example you have created 2 array as


Now it checks for method which takes array as an argument but for array of A it doesn't found it. so it use method which takes Object and hence execute method. (Here widening bit boxing and vararg)


If this method is not exist then it execute



In second method for sifter(ba) it found method as


so print as 3

In last method there is no matching parameter for int so it execute

 
Campbell Ritchie
Marshal
Posts: 76109
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pramod P Deore wrote: . . . In last method there is no matching parameter for int . . .

So you get boxing to an int, followed by widening to an Object.
 
reply
    Bookmark Topic Watch Topic
  • New Topic