• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

I can't understand these code!

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
October 15, 2002
Tech Tips Quiz
[http://developer.java.sun.com/developer/Quizzes/techtips3/index.html]
public class OverDemo1 {
// overload methodA between short/int
static void methodA(short s) {
System.out.println(
"methodA(short) called");
}
static void methodA(int i) {
System.out.println("methodA(int) called");
}
// overload methodB between float/double
static void methodB(float f) {
System.out.println(
"methodB(float) called");
}
static void methodB(double d) {
System.out.println(
"methodB(double) called");
}
public static void main(String args[]) {
// call methodA with char argument
methodA('a'); //methodA(int) called
// call methodB with int argument
methodB(Integer.MAX_VALUE); // methodB(float) called
}
}
Why does methodA('a') call methodA(int i)?
How about char and int and short?
int is 32bit,char and short is 16bit.
 
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There was an implicit numeric promotion from char to int that's why the amethod(int i) was called. amethod(short s) can't be called coz you need an explicit cast when converting a char to a short or a short to a char.
P.S one more thing amethod(int i) is the most specific method and that's explain why amethod(float f) wasn't called
 
Sam Wang
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.
Maybe you miss the word 'methodB';
methodB(float f){...}
methodB(double d){...}
methodB(Integer.MAX_VALUE);
//will call methodB(float f) why?
 
Shishio San
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because methodB(float f) is more specific than methodB(double d)
I suggest you read This
 
Sam Wang
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic