• 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
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

output

 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
System.out.println(" " + 7 + 2 );
System.out.println( 7 + 2);

why the output first line is 72 and it's 9 for next line

Please expalin

Thanks lot
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
-------------
System.out.println(" " + 7 + 2 );
System.out.println( 7 + 2);

why the output first line is 72 and it's 9 for next line
-----------

" " + 7 + 2 = 72, because the first element is a string, therefore it will treat the 7 and 2 being added to a string i.e. 72 being added to the blank string.

7+2 = 9, because the first element is an int and therefore it will add 2 as an int rather than a string.
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pay attention to the way the + operator overloads based on the types of data in println()
 
You know it is dark times when the trees riot. I think this tiny ad is their leader:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic