aspose file tools*
The moose likes Beginning Java and the fly likes Passing primitive values at method call and at constructor call Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Passing primitive values at method call and at constructor call" Watch "Passing primitive values at method call and at constructor call" New topic
Author

Passing primitive values at method call and at constructor call

Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
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


Asking Smart Questions FAQ - How To Put Your Code In Code Tags
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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");


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Feb 16, 2005
Posts: 47
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

Joined: Jul 08, 2003
Posts: 24184
    
  34

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.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing primitive values at method call and at constructor call