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
What does yield method of the thread class do?
Can we make the abstract methods static in java?
What is a treeset in java?
What is the use of set in java?
What is lambda programming?
What is the biggest integer?
What is the difference between equals() and == in java?
What is functional interface in javatpoint?
What does java edition mean?
How to Sort Strings which are given in List and display in ascending order without using java api.
Can a class have more than one object?
What is parseint?
What do you understand by abstract classes?
Is alive in java?
What is the integer of 16?