What is the difference between string, stringbuffer, stringbuilder?

Asked by Last Modified  

22 Answers

Learn Java

Follow 1
Answer

Please enter your answer

Computer Classes

String is immutable, whereas StringBuffer and StringBuilder are mutable
Comments

Trainer

String is immutable, i.e. once a String object is created and its value is assigned, its value can not be changed. This is because the String object is stored in Constant String Pool. If you assign a new value to a String reference variable, a new String object is created and stored in Constant String...
read more
String is immutable, i.e. once a String object is created and its value is assigned, its value can not be changed. This is because the String object is stored in Constant String Pool. If you assign a new value to a String reference variable, a new String object is created and stored in Constant String Pool. Alternatively we can use String Buffer and String Builder which are mutable. It means their values can be changed after they have been created as they are stored in heap. The difference between StringBuffer and String Builder are given below: 1) StringBuffer is synchronised whereas StringBuilder is not synchronised 2) StringBuffer is thread safe whereas StringBuilder is not thread safe. 3) StringBuffer is slow in performance whereas StringBuilder is faster. read less
Comments

Java Web Application Development and Aptitude Trainer

String object are immutable, whereas StringBuffer and String Builder are mutable. Between StringBuffer and StringBuilder, the former is synchronized, hence providing thread safety, while the later is not synchronized, therefore should not be used in case of multithreading.
Comments

Trainer

String is immutable, if you try to alter their values, another object gets created, whereas StringBuffer and StringBuilder are mutable so they can change their values. The difference between StringBuffer and StringBuilder is that StringBuffer is thread-safe where as StringBuilder is not thread-safe.
Comments

Java Trainer

String is immutable, means once you created a String you can not modify, Even if you try to it will create a new string for you and assign that to you. StringBuffer and StringBuilder are immutable, however methods of String buffer are thread safe, So when you are having multithreading in you application...
read more
String is immutable, means once you created a String you can not modify, Even if you try to it will create a new string for you and assign that to you. StringBuffer and StringBuilder are immutable, however methods of String buffer are thread safe, So when you are having multithreading in you application you should use StringBuffer otherwise StringBuilder. read less
Comments

Geek Trainer

String is an immutable object which means the value cannot be changed once created where as StringBuffer is mutable. The StringBuffer is Synchronized hence thread safe where as StringBuilder is mutable but not synchornized and suitable for only single threaded instances.
Comments

Java Tutor

String is an immutable class , whereas StringBuffer is a synchronized mutable class so it is thread safe and StringBuilder is non-synchronized mutable class.
Comments

String is immutable, once you create a String object you can not modify that object. StringBuffer and StringBuilder are mutable, after creating the object you can modify that. The difference between StringBuffer and StrinngBuilder is, The methods in StringBuilder are synchronized methods and StringBuffer...
read more
String is immutable, once you create a String object you can not modify that object. StringBuffer and StringBuilder are mutable, after creating the object you can modify that. The difference between StringBuffer and StrinngBuilder is, The methods in StringBuilder are synchronized methods and StringBuffer methods are not synchronized . So, if you are concerned about thread safety go for StringBuilder otherwise go for StringBuffer. read less
Comments

Tutor

String is immutable, if you try to alter their values, another object gets created, whereas StringBuffer and StringBuilder are mutable so they can change their values. Thread-Safety Difference: The difference between StringBuffer and StringBuilder is that StringBuffer is thread-safe.
Comments

Web technologies Trainer

String objects cannot be changed once they are created. Ex: String s1 = "abc"; s1 = s1+"def"; Now the reference s1 refers to the string "abcdef". But the string "abc" which is created earlier is not modified. It still exists in a string pool. Instead a new string is created which is "abcdef" and its...
read more
String objects cannot be changed once they are created. Ex: String s1 = "abc"; s1 = s1+"def"; Now the reference s1 refers to the string "abcdef". But the string "abc" which is created earlier is not modified. It still exists in a string pool. Instead a new string is created which is "abcdef" and its referred by s1. Now if s1 was StringBuffer or a StringBuilder, Ex: StringBuffer sb = new StringBuffer("abc"); sb= sb.append("def"); "abc" gets modified to "abcdef". Therefore its only one object which is created and that itself gets modified. The StringBuffer is thread safe and hence the operations are slow compared to StringBuilder. Hope this helps. read less
Comments

View 20 more Answers

Related Questions

I completed my graduation in 2017, now working as an HR Executive in a Consultancy. I want to move to IT Sector. Which course is best for me to learn and get success in life? Please Suggest me
Dear Kumar, My suggestion is to - become good in one programming language - preferably Java and one O/S preferably Linux. Be aware of Open Source systems. Try to identify the opportunities in your existing...
Kumar
Which one is faster, Python or Java?
The speed of Python and Java can vary depending on the specific task. Java is generally considered faster for low-level operations and large-scale applications, while Python is often valued for its simplicity...
Chetana
0 0
5
Is it really needed to do Java training before graduation, to get a job?
Yes, It's must to get better opportunities and to standard alone in IT field. Not only core Java but also learn Spring,Hinernat ,Struts. ANGULARJS add an advantage to get job easily.
Udaybhan
Can Anybody explain Internal code of HashMap?
Now we can synchronize map also by using collections. Synchronize method need to pass map Object as parameter...
Ramakanth
Do I need to import java.lang package any time? Why?
No, you don't need to import it at any time. As this is part of java.lang package is a part of \jdk1.6.0_11{this is the location f your jdk installation directory}\jre\lib\rt.jar and this rt.jar (called...
Anitha

Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com

Ask a Question

Related Lessons

What Would Be Life Cycle Of A Fresher After Campus In An IT Company?
1. Basic Technical Training: Since freshers are not subject matter experts so gone through 3 - 6 months basic technical training within Organization. 2. Technical Assessment: HR sends freshers to various...

How To Setup development Environment Of Java?
After installation of JAVA, we need to setup 2 environment variables. First one is JAVA_HOME and second is Path. Below steps to set up Java environment to compile & execute Java programs from command...

Finding a Majority Element
Problem Description Task. The goal in this code problem is to check whether an input sequence contains a majority element. Input Format. The first line contains an integer, the next one contains a sequence...

jOOQ 3.10 Supports JPA Attribute Converter
One of the cooler hidden features in jOOQ is the JPADatabase, which allows for reverse engineering a pre-existing set of JPA-annotated entities to generate jOOQ code. For instance, you could write these...

1.1. Reverse an array in Java.
public class Main { public static void main(String args) { int arr = {1, 2, 3, 4, 5}; for (int i = 0; i < arr.length / 2; i++) { int temp = arr; arr = arr; arr = temp; } System.out.println(Arrays.toString(arr)); }}

Recommended Articles

In the domain of Information Technology, there is always a lot to learn and implement. However, some technologies have a relatively higher demand than the rest of the others. So here are some popular IT courses for the present and upcoming future: Cloud Computing Cloud Computing is a computing technique which is used...

Read full article >

Before we start on the importance of learning JavaScript, let’s start with a short introduction on the topic. JavaScript is the most popular programming language in the world, precisely it is the language - for Computers, the Web, Servers, Smart Phone, Laptops, Mobiles, Tablets and more. And if you are a beginner or planning...

Read full article >

Java is the most famous programming language till date. 20 years is a big time for any programming language to survive and gain strength. Java has been proved to be one of the most reliable programming languages for networked computers. source:techcentral.com Java was developed to pertain over the Internet. Over...

Read full article >

Designed in a flexible and user-friendly demeanor, Java is the most commonly used programming language for the creation of web applications and platform. It allows developers to “write once, run anywhere” (WORA). It is general-purpose, a high-level programming language developed by Sun Microsystem. Initially known as an...

Read full article >

Looking for Java Training Classes?

Learn from the Best Tutors on UrbanPro

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you