Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

StringBuildier

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

Bit confused here on output.
So I create this StringBuilder object:



The output is 12SUN45. Why not 12SUN9?
 
author & internet detective
Posts: 40913
840
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kamila,
Good question. Java sees the String "SUN" in the expression. Which means it says "lets use String concatenation for the + operator. If you wanted the 9, you'd need to use parenthesis.
 
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
  • 10 + 2 + "SUN" + 4 + 5
  • Evaluation starts from left side.

  • Steps:
  • 10 + Now it looks at right side to check, is there any high precedence operator than +? answer is no so (All are additive operators so same precedence so left associative)
  • ( 10 + 2 ) result = 12

  • 12 + again it looks at right side to check, is there any high precedence operator than +? answer is no so
  • ( 12 + "SUN" )
    Explanation:
    String conversion is performed because one operand is String i.e. "SUN" so 12 whose default primitive type is int is converted to Integer like new Integer(12); then this reference value is converted to type String by String conversion. Concatination of String using + operator uses StringBuilder's append method to append them and then converts to String using toString method of StringBuilder.
    Now result =  "12SUN"

  • ( "12SUN" + 4 )  same like above String conversion process result = "12SUN4"
  • ( "12SUN4" + 5 ) same like above String conversion process result = "12SUN45"
  • Final result = "12SUN45"

  • Edit: Then at last passes this String to parameterized constructor of StringBuilder i.e  public StringBuilder(String str)
     
    Kamila Bertran
    Ranch Hand
    Posts: 47
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    so the moment there's change of type StringBuldier just 'switches' to that new type? Therefore as 10 and 2 are int it just sums it up, but than goes 'hey, I can't add 'SUN' to that, let's trat the rest as a String' ??
     
    Kamila Bertran
    Ranch Hand
    Posts: 47
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Ganish Patil wrote:

  • 10 + 2 + "SUN" + 4 + 5
  • Evaluation starts from left side.

  • (...)



    That was awesome. Couldn't get a better explanation! Thanks for taking your time!
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Eclipse IDE MySQL Database Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You're welcome
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic