• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Urgent Help from anyone

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi guys
please explain how the answer is correct
QUESTION : 6
Consider the following class
1. class Tester {
2. void test (int i) { System.out.println ("int version"); }
3. void test (String s) { System.out.println ("String version"); }
4.
5. public static void main (String args[]) {
6. Tester c = new Tester ();
7. char ch = 'p';
8. c.test (ch);
9. }
10. }
ans print "int version"
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it's simple.
If u know abt automatic conversion, char field can be assigned to int field (Widening case). so java converts char to int and called appropriate method.
Thanks
Ashish
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because char is also an integral data type (which does not take -ive integers). When compiler tries to lookup a test method that takes a char, it doesn't find one so it looks for a closest (but eligible!) match, it finds the int version. So it promotes the char to int and calls that method!
HTH,
Paul.
------------------
http://pages.about.com/jqplus
Get Certified, Guaranteed!
 
reply
    Bookmark Topic Watch Topic
  • New Topic