Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Gail Knight   on Jun 14 In Java Category.

  
Question Answered By: Anuja Shah   on Jun 14

String is not a mutable object.

String s ="a";
s = s+"b";

when u compile the code above, "+" operator will be changed by your compiler;

String s=new String("a");
StringBuffer sbimp = new StringBuffer(s);
sbims.append("b");
s=sbims.toString();

so it's not recomended to use "+" operator so much.

for ex:

code1
------------------------------
String a="";
for(int i=0;i<20000;i++){
a=a+i;
}



code2
-------------------------------------------
String a="";
StringBuffer sb="";
for(int i=0;i<20000;i++){
sb.append(i);
}



code2 will be 1000 times faster cause first code means.

String a="";
for(int i=0;i<20000;i++){
String a=new String("a");
StringBuffer sbimp = new StringBuffer(a);
sbims.append("b");
a=sbims.toString();
}

Share: 

 

This Question has 8 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on String Object Or get search suggestion and latest updates.


Tagged: