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
I want to print “hello” even before main is executed. How will you acheive that?
What does index mean in java?
What is a line separator in java?
Tell me are there implementations for sorting and searching in the java libarary?
What is a singleton class? Give a practical example of its usage.
Is null a keyword in java?
How to check if a list is sorted in java?
Is arraylist ordered?
What causes memory leaks in java?
what is function overloading in java?
What are streams in java 8?
Why call by value prevents parameter value change?
What is a class instance variable?
Can we define private and protected modifiers for the members in interfaces?
What does java stand for?