• 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

Using charAt() and adding it to a "result" String

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys, I'm currently going through simple practice problems on CodingBat and ran into a problem; the goal is to copy each character in a string twice so that every /character is doubled. Here is the code:


So when I use the top piece of code, the result returns a string of numbers, rather than doubling each character, but the bottom piece of code works correctly. Can anyone explain to me why the top piece of code does this? Thank you
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The character datatype is a actually a numeric data type, between integer and byte, that represents a code point for a character. As a number you can add them and subtract them. So in the first section of code, the plus operator has a higher precedence than the += operator, this means that the two characters get added together first. Unless forced to do otherwise, the + operator converts its operands to integers before performing its action the chars are promoted to ints and the ints are added. After the addition, the += operator is evaluated and since the Left Hand side of the operator is a String, so the Right Hand side is converted to a String and String concatenation occurs instead of addition.

In the second code snippet, the two + operators have the same precedence so they are evaluated Left to Right. The first one has a String on its Left Hand side, so String concatenation occurs. The Right Side of the first operator is a char, and the conversion to String is the conversion to the character the char represents. Then the result of this String concatenation becomes the Left Hand side of the second + operation and the same thing occurs. Finally the result is assigned to the result variable.
 
henry xiao
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot! I don't recall ever reading about operator precedence in the book - I'll look more into it now
 
reply
    Bookmark Topic Watch Topic
  • New Topic