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
Difference between current previous versions of Java?
Explain working of call by reference function invoking.
Why java strings are immutable in nature?
what is mutual exclusion? : Java thread
Explain which of the following methods releases the lock when yield(), join(),sleep(),wait(),notify(), notifyall() methods are executed?
Can we create object of static class?
What do you mean by JVM?
If I don't provide any arguments on the command line, then what will the value stored in the string array passed into the main() method, empty or null?
Assume a thread has lock on it, calling sleep() method on that thread will release the lock?
Can an interface be final?
In java, how we can disallow serialization of variables?
What is the default size of arraylist in java?
What is java instanceof operator?
If an object is garbage collected, can it become reachable again?
What is difference between length and length() method in java ?