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



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

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

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

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

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

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

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

Answer / ravi jain

First one is faster

because in 1st only one object is created,
but in 2nd one three objects are created.

Is This Answer Correct ?    1 Yes 0 No

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

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

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

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

Post New Answer

More Core Java Interview Questions

Why is stringbuffer not immutable?

0 Answers  


Does chrome use java?

0 Answers  


What are the rules for naming an array?

0 Answers  


What is dot operator?

0 Answers  


What is the difference between object oriented programming language and object based programming language?

0 Answers  






What happens to the Exception object after handling an exception?

0 Answers  


How many bits are used to represent unicodde,ASCII,UTF-16 and UTF-8 characters?

2 Answers  


State the difference between creating string as new () and literal.

0 Answers  


take any 4 input numbers. like 2345.. wanted out put is All 16 combinations of the number 2345.. for example- taking input as 4565 output- 5654 4556 4655..

2 Answers   Emphasis,


What is java used for on a computer?

0 Answers  


i need example for java abstraction. where we use abstraction and why we need abstraction.

2 Answers   Oracle,


Urgent requirement of JAVA/.NET/QA in a CMMI level3 MNC for Pune location.

0 Answers  


Categories