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?
Answers were Sorted based on User's Feedback
Answer / debapriya maity
The first one is faster
Second code snippets:3 objects are created
1:s = "Hello Mom"
2:,I mean Mom
3:Hello Mom,I mean Mom
Is This Answer Correct ? | 5 Yes | 1 No |
Answer / debapriya maity
The first one is faster
Second code snippets:3 objects are created
1:s = "Hello Mom"
2:,I mean Mom
3:Hello Mom,I mean Mom
Is This Answer Correct ? | 5 Yes | 2 No |
Answer / parameswaran m
The StringBuilder one is slightly more efficient and uses
less memory. In practice, it is unlikely to make enough of a
difference to matter either way.
Is This Answer Correct ? | 3 Yes | 1 No |
Answer / prasanta khandai
Actually what happens if u add two strings like second one,
then internally it uses the String Builder to call the
append method to add and it converted into the strings again.
so first one is the faster as it appended right ways.
Is This Answer Correct ? | 0 Yes | 1 No |
Answer / 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 |
what r callable statement and give their proper use
what is web.xml?and its use?
What is your platform?s default character encoding and how to know this?
Explain what is encapsulation?
What is the difference between getCodeBase and getDocumentBase methods?
1 Answers CS Business Services,
How does abstract modifier work?
When finalize method is called?
Which sorting is used in arrays sort in java?
What is the difference between throw and throws?
int a=10,b=20,c=30 a= b+c;b=a+c;c=a+b; System.out.println("The value is"+a+b+c;
What are constructors in java?
java is pure object-oriented programming language?why it is platform independent.