Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

StringBuildier  RSS feed

 
Kamila Bertran
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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



The output is 12SUN45. Why not 12SUN9?
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37222
519
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Likes 3
  • Mark post as helpful
  • send pies
  • 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
    • 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
    • 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
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You're welcome
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!