Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

StringBuilder Methods called from left to right?

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

in this code,

what would be the output? if on StringBuilder methods are called from left to right here delete() is called first. and why it's output is not 12SUN4512S5?
Why it is appending out put of delete method twice?

output is 12S512S5 ?

TIA
Shweta
 
Ranch Hand
Posts: 383
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

shweta patiljadhav wrote:Hi,

in this code,

what would be the output? if on StringBuilder methods are called from left to right here delete() is called first. and why it's output is not 12SUN4512S5?
Why it is appending out put of delete method twice?

output is 12S512S5 ?

TIA
Shweta



I think you're confusing chaining methods (ie calling methods on an object from left to right), and passing methods as arguments

ejg.append("abc").delete(3,6); vs ejg.append(//argument that happends to be a method);
 
Sheriff
Posts: 7610
522
Mac OS X VI Editor BSD Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

shweta patiljadhav wrote:what would be the output? if on StringBuilder methods are called from left to right here delete() is called first. and why it's output is not 12SUN4512S5?
Why it is appending out put of delete method twice?

output is 12S512S5 ?


Well, StringBuilder is mutable object at first. Second, when you call a method, you need to pass arguments to it (or not). In order to pass arguments, you need to know what these are.
1. ejg.append(ejg.delete(3, 6)); // evaluates ejg.delete(3, 6) first. And you get 12S5
2. ejg.append(evaluated argument from 1st step) // StringBuilder is mutable, think about it as 12S5.append(12S5).
3. And you get what you get, two times by 12S512S5
 
shweta patiljadhav
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

shweta patiljadhav wrote:what would be the output? if on StringBuilder methods are called from left to right here delete() is called first. and why it's output is not 12SUN4512S5?
Why it is appending out put of delete method twice?


Chained methods will definitely be called from left to right. And that's not only limited to StringBuilder's methods, but applies to any object. Here's an example of a StringBuilderOutput: !skcor avaJ

And you can do the same with strings as wellOutput: abc rOcks? Jabcvabc rocks!

Remember: there is one very important difference between the first and second code snippet. In the first code snippet you are using a StringBuilder which is a mutable object, so that means that every method call will manipulate the same object. In the second code snippet you are using a String and as you know a String object is immutable. So every method call results in another String object being created. Only the result of the last method call replace("a", "abc") is assigned to the reference variable res and thus is still reachable; all other objects are unreachable and thus eligible for garbage collection. It would be a nice exercise to list all unreachable String objects from the second code snippet

Now in your code snippetyou didn't use chained methods at all. And that's why these methods are not executed from left to right. In order to call the append method, you need of course to know the String you want to append. That's why the delete method is executed before the the append method: the result of the delete method is required to be able to execute the append method.

Why this code snippet prints 12S512S5 is already well explained by Liutauras, so I won't explain it again. But I'll do a little pop quiz with you to see if you fully understood everything explained in this topic

What will be the output of the above code snippet if we replace the first line of the main method toIf you think the code snippet doesn't compile, just say so (and explain why).

Hope it helps!
Kind regards,
Roel
 
It's fun to be me, and still legal in 9 states! Wanna see my tiny ad?
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic