• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

doubt in double and Integer

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 22656
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Widening goes before boxing.
 
Tom Johnson
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It does indeed, just tried it there!!

 
Marshal
Posts: 75869
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22656
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://java.sun.com/docs/books/jls/third_edition/html/conversions.html; check for section 5.3
 
Campbell Ritchie
Marshal
Posts: 75869
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Rob. I was looking somewhere like section 8.4 where it said "overloading."
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic