aspose file tools*
The moose likes Beginning Java and the fly likes doubt in double and Integer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "doubt in double and Integer" Watch "doubt in double and Integer" New topic
Author

doubt in double and Integer

Sivakumar Janardhanan
Ranch Hand

Joined: Dec 11, 2008
Posts: 30
Hi All this is sivakumar. Actually i did one small program that same method with different parameter.

this is my program



output is
Double : 1.0
Double : 2.0

how and why if i passed int value to the something method it denotes the double parameter method.

double is one of the data type.

And Integer is the wrapper class type. what is the difference?

can any body tell me the accurate reason.



bye and waiting for your reply
siva.

[edit]Add code tags. CR[/edit]
Tom Johnson
Ranch Hand

Joined: May 11, 2005
Posts: 142
Hi,
firstly, please use code tags, it makes your code so much easier to read.

Anyway, Integer is the wrapper class for int. It is an object. double and int are primitive types. Hence when you pass 2.0 it invoked the method (double ival) as expected. When you pass 1, it does not match the (Integer ival) method as 1 is a primitive. 1 will fit into a double so it calls the (double ival) method instead. You should change the method signature to be


to get it to invoke it when

is used.

Alternatively, you could leave the method as it is and invoke it with


I assume this is using JDK1.4, as I assume in 5.0 autoboxing should kick in here and make the original example work as expected?


<a href="http://faq.javaranch.com/java/UseCodeTags" target="_blank" rel="nofollow">Use Code Tags!!</a>
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19556
    
  16

Widening goes before boxing.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Tom Johnson
Ranch Hand

Joined: May 11, 2005
Posts: 142
It does indeed, just tried it there!!

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36592
    
  16
Rob Prime wrote:Widening goes before boxing.
Can you remember where there is a reference to that, please Rob? I looked at the overloading section of the Java Language Specification, but I couldn't find that precedence mentioned there.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19556
    
  16

http://java.sun.com/docs/books/jls/third_edition/html/conversions.html; check for section 5.3
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36592
    
  16
Thank you, Rob. I was looking somewhere like section 8.4 where it said "overloading."
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt in double and Integer
 
Similar Threads
Final Methods & Overloading
Converting a Double to an Int
type casting
A question about passed by value
Method Overloading Examples