• 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

Question about Strings and Arrays

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just finished the Chapter 3 Self test in the Java 2 Certification book by Kathy Sierra and Bert Bates and I am a little confused on question 17. Basically the question is about this:
long [] a1 = {3, 7, 5}
long [] a2 = {3, 7, 5}
System.out.print(a1[0] + a1[1] + a1[2] + " ");
System.out.println(a2[0] + a2[1] + a2[2]);
What is the result?
The answer in the book says it is 15 15. But I thought that if there is any Strings included in a System.out.print statement it would concatenate what is in the array to print this:
375 15.
Please let me know where I am making the mistake. Thanks.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Each + is a binary operation (two operands) evaluated separately, and whether it is numeric addition or string concatenation is decided for each one separately. If either operand to a particular + is a String, it is evaluated as String concatenation. In the example, this is true only of the last + in the first print statement, so the other + operators are evaluated as numeric addition operators.
[ April 13, 2003: Message edited by: Bryan Helm ]
 
Oliver Nono
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see. Thanks for the help. That one was tricky!
 
That's a very big dog. I think I want to go home now and hug this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic