• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Wrappers

 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.Integer ten=new Integer(10);
2.Long nine=new Long (9);
3.System.out.println(ten + nine);
4.int i=1;
5.System.out.println(i + ten);

wats the output and why??
wat i felt that ter would be an error at line 5
we r adding primitive to an Wrapper object buti think this compiles fine why???
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
your code will not compile because (+ operator is not defined between Integer and Long, and between int an Integer). normally you'll get this compile errors :

line 3 : operator + cannot be applied to java.lang.Integer,java.lang.Long
System.out.println(ten + nine);
^
line 5 : operator + cannot be applied to int,java.lang.Integer
System.out.println(i + ten);
^
change line 3 and line 5 respectively with this :

System.out.println(ten.intValue() + nine.intValue());
System.out.println(i + ten.intValue());

hope this can help.

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

Originally posted by harish shankarnarayan:
1.Integer ten=new Integer(10);
2.Long nine=new Long (9);
3.System.out.println(ten + nine);
4.int i=1;
5.System.out.println(i + ten);

wats the output and why??
wat i felt that ter would be an error at line 5
we r adding primitive to an Wrapper object buti think this compiles fine why???



The code wont compile. You will get compilation errors like:

operator + cannot be applied to java.lang.Integer,java.lang.Long
System.out.println(ten + nine);
^

operator + cannot be applied to int,java.lang.Integer
System.out.println(i + ten);
^
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you use the jdk5.0 ,the code compile ok
and output is
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

It's related to autoboxing operations.

As of Java 5, autoboxing is a core functionality existing in Java.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It depends on the platform . For those who are taking SCJP 5.0 the above code compiles fine and the result would be 19,11.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic