Contact Us
  
 
 
    SearchSearch   RegisterRegister   Log inLog in 

String vs StringBuilder

String vs StringBuilder

 
Post new topic   Reply to topic     Forum Index -> Java,Struts framework, Velocity, JSF, Hibernate, Servlet, JSP, javaBeans
View previous topic :: View next topic  
Author Message
juniormember



Joined: 28 Jun 2008
Posts: 154

PostPosted: Tue Nov 25, 2008 11:50 am    Post subject: String vs StringBuilder Reply with quote

Hi
How String is different from StringBuilder
Back to top
View user's profile Send private message
hotjava



Joined: 25 Mar 2007
Posts: 14

PostPosted: Wed Dec 24, 2008 6:24 am    Post subject: Reply with quote

String is immutable class mean content of String class cannot changes once it is initialized. StringBuilder is mutable class mean content of StringBuilder can be change after initialized.

String a="content";
a=a+"new content";




StringBuilder sb=new StringBuilder("content") ;
sb.append("new content");

String append at the end
StringBuilder append at any where

String content cannot modify
e.g
String a ="content";
if we want to change "t" character from String we cannot directly
we have to use substring() method of String

Quote:

int i=a.indexOf("t");
String result=a.substring(0,i);
String newResult=a.substring(i);
a=result+"O"+newResult;


StringBuilder content can be modified

Quote:
StringBuilder sb=new StringBuilder("content");
nt i=a.indexOf("t");
sb.setCharAt(i,"O");


or can use insert method

Quote:
sb.insert(i,"O");


Instances of StringBuilder are not safe for use by multiple threads.
StringBuilder is not synchronized and this results StringBuilder is fast
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> Java,Struts framework, Velocity, JSF, Hibernate, Servlet, JSP, javaBeans All times are GMT
Page 1 of 1

 
Jump to:  
  Quick Post a Reply Topic
     
  Name (required)  
     
  Email Address (will not be published) (required)  
     
  Subject of topic (required)  
     
   
     
   
     
  Advertisement  
     
 
     


  More Links  
 
JSP Introduction
JSP Setup
Simple JSP Example
JSP Variable
Array Declaration in JSP
JSP Forms and User Input
Form Validation
JSP Cookies
JSP Session Object
Loop, content collection, iterator, conditional check in JSP
JSP Application Object
JSP Include
JSP Response Object
JSP Request Object
JSP Error Handling
JSP File Handling Object
JSP Action Tag
JSP Implicit Objects
JSP Declaration
JSP Directive
JDBC Introduction
JDBC Driver
JDBC Connection
JDBC Prepared/Statement
JDBC ResultSet
JDBC Close Connection object
JDBC Insert
JDBC Display Records
JDBC Update
JDBC Delete
JDBC Sorting
JDBC Query
JDBC Exception
JDBC Add Batch
JDBC Transaction/RollBack