• 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

auto boxing additions

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

The following code is giving the compilation error.When I try to add Long
and Integer ,then it is giving the error. I do not the rules for adding these
wrapper classes.Can you provide me link if it is already there and also link to read on wrapper operations.




error ::
--------
found : int

required: java.lang.Long
Long l=2;
1 error



thank you
 
kumarth ravi
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry,

the System.out.println(i+j ) is not correct.

that is System.out.println( i+l );
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think your question was
Long l=5; fails to compile and shows the below error
Type mismatch: cannot convert from int to Long
The constant 5 will be autoboxed to a new Integer object and assigned to Long which will cause a error.
You will get an errror for all bold below

All wrapper classes are child classes of Number class and as you know
you cannot assign child classes to each other.
For example we know String and Integer are child classes of Object but we
cannot assign a Integer to String and vice versa.
Hope this clears your doubt
Thanks,
Deepak
 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code runs and compiles fine the result is 9 with version 1.5.0_05.



i will be unboxed to int
j will be unboxed to long

i + j = long.

Maybe I'm wrong, but I don't understand why there was a compiling error
 
kumarth ravi
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes,it is compiling in java1.5 and it gives the result 9.

Thank you for reply.

with regards
ravikumar
 
If you like strawberry rhubarb pie, try blueberry rhubarb (bluebarb) pie. And try this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic