• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Expression in primtives

 
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does 7 + 8 + " " evaluates. To me it is 78, but I see some questions at the ned of Chapter 3 of Kathy & Bates book giving answer like 15. Do not have book in front of me but that is what I read ... what is the right answer?
 
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
7+8+" " evaluates to 15 ;first the two numbers are added and then added to the empty string;
""+7+8 evaluates to 78 ;here because the empty string is at the begining the following 7 and 8 are treated as just string additions.
7+""+8 evaluates to 7 8; same as above but note the space between 7 and 8.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I've tried the same example but I got bit different results
1.15
2.78
3.78
in second and third expression answers are same there was no space in between.
could anyone please explain
 
piy varsh
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think I mistaken the problem earlier I've not given the space in quotes.
now it is working fine. and got the same results
1.15
2.78
3.7 8
thanks
 
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Rajesh
What does 7 + 8 + " " evaluates. To me it is 78, but I see some questions at the ned of Chapter 3 of Kathy & Bates book giving answer like 15. Do not have book in front of me but that is what I read ... what is the right answer?
It depends on different situation
If the case is string such as
String cardinality="7"+"5";
and
System.out.println("cardinality"+7+5);
//Above is 75
Differ to the case
//Following is 12
System.out.println("sample operation"+(7+5));
and
int adding=7+5;
Are you sure the book in the first case?
reply
    Bookmark Topic Watch Topic
  • New Topic