Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing primitive values at method call and at constructor call

 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I jst want 2 know wat happens wen i call ny method with primitive value passed to it. Say if i overloaded a method like below:

public void aMethod(byte b){
// implementation hre.
}

public void aMethod(char c){
// implementation hre.
}

public void aMethod(int i){
// implementation hre.
}

If i call somObj.aMethod(12); hre which method will be called.. I mean how call get binded to right implementation???

I hav a second doubt. why dis line is not compiling..

Byte byteObj=new Byte(10);

Thanx

Bye

Naseem Khan
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Naseem Khan:
...why dis line is not compiling...
Byte byteObj=new Byte(10);

There is no Byte constructor that takes an int argument. You can use String instead (by enclosing the value in quotes)...

Byte byteObj = new Byte("10");
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general, method calls are resolved to the "most specific" argument type(s).

If there is a method that matches the supplied argument types exactly, then that method is used. Otherwise, the method with the "most direct" type conversion is used.

For example, the following are legal primitive conversions:
* byte to short
* short to int
* char to int
* int to long
* long to float
* float to double

So if you have overloaded methods, meth(byte b), meth(char c), and meth(int i), and you call meth(shortValue), then meth(int i) will be invoked because a short can be converted to an int.
 
Kartik Mahadevan
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I think that compiler would issue an error bcos it is not very specific what sort of argument is 12 ie wether it should be taken as int or byte.
Regards
M.Kartik
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this particular case, you think wrong, my friend. The Java language specification is quite clear on this point. All integer literals are of "int" type, and all floating-point literals are "double". In the example we're looking at, the "int" version is called. You could use "aMethod((byte) 12)" to call the byte version (or pass a variable, of course.)

Now, the original poster didn't ask this, but I'll tell him anyway: this is a very bad idea. This sort of wonton use of overloading can be really confusing. In general, overloading a method on multiple different integral types this way is a bad idea, and should be avoided.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic