File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt in double and Integer

 
Sivakumar Janardhanan
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 142
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Widening goes before boxing.
 
Tom Johnson
Ranch Hand
Posts: 142
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It does indeed, just tried it there!!

 
Campbell Ritchie
Sheriff
Pie
Posts: 47244
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://java.sun.com/docs/books/jls/third_edition/html/conversions.html; check for section 5.3
 
Campbell Ritchie
Sheriff
Pie
Posts: 47244
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Rob. I was looking somewhere like section 8.4 where it said "overloading."
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic