Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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


Please Help Members By Posting Answers For Below Questions

I want to print “hello” even before main is executed. How will you acheive that?

1049


What does index mean in java?

927


What is a line separator in java?

929


Tell me are there implementations for sorting and searching in the java libarary?

979


What is a singleton class? Give a practical example of its usage.

1045


Is null a keyword in java?

918


How to check if a list is sorted in java?

976


Is arraylist ordered?

1006


What causes memory leaks in java?

878


what is function overloading in java?

1213


What are streams in java 8?

947


Why call by value prevents parameter value change?

1012


What is a class instance variable?

1062


Can we define private and protected modifiers for the members in interfaces?

1046


What does java stand for?

1043