StringBuilder s = new StringBuilder("Hello
Mom");s.append(",I mean,Mother");
Response.Write(s.ToString());


String s = "Hello Mom";
s+ = ",I mean Mom";
Response.Write(s);

Which is faster ? which uses the most memory?

Answer Posted / jimmy dean

The second one is faster.

In the first example the StringBuilder is initialized with
an initial value. Then the append method is ran, adding
another string to 's'. Then the Write method is called,
which calls the ToString() method of 's'.
Total Steps:
1.) Initialize and set initial value
2.) Append
3.) Write
4.) ToString()

In the second example the String is initialized with an
initial value. Then a string is added onto 's'. Then the
Write method is called.
Total Steps:
1.) Initialize and set initial value
2.) Add String to String
3.) Write

Therefore I would say that the second example is faster, and
uses less memory.

Is This Answer Correct ?    0 Yes 3 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Can private class be extended java?

561


What is the purpose of void?

534


What is tree in java?

485


What does percent mean in java?

586


Which method cannot be overridden in java?

589






java program with complete 4 oops concepts implemented example

2726


Explain what access modifiers can be used for methods?

564


How does predicate work in java?

529


What is a static class in java?

552


What are packages in java?

559


Why synchronization is important in java?

558


What is array command?

553


What is a stringbuilder?

504


What are the Class Libraries ?

589


What are the different ways of creating thread?

568