- 浏览: 80726 次
文章分类
- 全部博客 (89)
- web service (9)
- subversion (1)
- JBOSS (3)
- interview (23)
- jQery (2)
- ExtJs (0)
- Axis (0)
- Design pattern (3)
- Agile (2)
- mutithread (0)
- Core Java (24)
- programming methods (1)
- SSH (7)
- jee design (1)
- OO (4)
- books (8)
- other (1)
- JSF (7)
- seam (2)
- Weblogic (4)
- JPA (1)
- ADF (1)
- Spring (5)
- Tomcat (1)
- DWR (2)
- JEE (3)
- Servlet (1)
- EJB (1)
- JDBC (3)
最新评论
-
iloveflower:
呵呵。好好学习。。。。。。。。。。。。
java 读书 -
Eric.Yan:
看了一点,不过是电子版的……你这一说到提醒我了,还要继续学习哈 ...
java 读书
http://javarevisited.blogspot.ca/2011/07/string-vs-stringbuffer-vs-stringbuilder.html
String is one of the most important classes in Java and anyone who starts with java programming uses string to print something on console by using. No matter which kind of application you are working you will find heavy usage of java String class but if you do profiling of your application you will find that String is the one class which creates lots of garbage because of many temporary Strings created in program. In this article we will see What is String, some important properties of String in Java, What is StringBuffer in Java , When to use StringBuffer in Java , StringBuilder in Java and how it can be used in place of StringBuffer, What are differences between String and StringBuffer and StringBuilder in Java and mostly String vs StringBuilder vs StringBuffer. Now let's start with String.
String in Java
Before looking difference between String and StringBuffer or StringBuilder let’s see some fundamental properties of String Class in Java
1) String is immutable in Java: String is by design immutable in java you can check this post for reason. Immutability offers lot of benefit to the String class e.g. his hash code value can be cached which makes it a faster hashmap key; it can be safely shared between multithreaded applications without any extra synchronization. To know why strings are immutable in java see the link
2)when we represent string in double quotes like "abcd" they are referred as String literal and String literals are created in String pools.
3) "+" operator is overloaded for String and used to concatenated two string. Internally "+" operation is implemented using either StringBuffer or StringBuilder.
4) Strings are backed up by Character Array and represented in UTF-16 format.
5) String class overrides equals() and hashcode() method and two Strings are considered to be equal if they contain exactly same character in same order and in same case. If you want ignore case comparison of two strings consider using equalsIgnoreCase() method. To learn how to correctly override equals method in Java see the link.
7) toString() method provides string representation of any object and its declared in Object class and its recommended for other class to implement this and provide string representation.
8) String is represented using UTF-16 format in Java.
9) In Java you can create String from byte array, char array, another string, from StringBuffer or from StringBuilder. Java String class provides constructor for all of these.
Problem with String in Java
One of its biggest strength "immutability" is a biggest problem of Java String if not used correctly. many a times we create a String and then perform a lot of operation on them e.g. converting string into uppercase, lowercase , getting substring out of it , concatenating with other string etc. Since String is an immutable class every time a new String is created and older one is discarded which creates lots of temporary garbage in heap. If String are created using String literal they remain in String pool. To resolve this problem Java provides us two Classes StringBuffer and StringBuilder. String Buffer is an older class but StringBuilder is relatively new and added in JDK 5.
Differences between String and StringBuffer in Java
Main difference between String and StringBuffer is String is immutable while StringBuffer is mutable means you can modify a StringBuffer object once you created it without creating any new object. This mutable property makes StringBuffer an ideal choice for dealing with Strings in Java. You can convert a StringBuffer into String by its toString() method. String vs StringBuffer or what is difference between StringBuffer and String is one of the popular interview questions for either phone interview or first round. Now days they also include StringBuilder and ask String vs StringBuffer vs StringBuilder. So be preparing for that. In the next section we will see difference between StringBuffer and StringBuilder in Java.
Difference between StringBuilder and StringBuffer in Java
StringBuffer is very good with mutable String but it has one disadvantage all its public methods are synchronized which makes it thread-safe but same time slow. In JDK 5 they provided similar class called StringBuilder in Java which is a copy of StringBuffer but without synchronization. Try to use StringBuilder whenever possible it performs better in most of cases than StringBuffer class. You can also use "+" for concatenating two string because "+" operation is internal implemented using either StringBuffer or StringBuilder in Java. If you see StringBuilder vs StringBuffer you will find that they are exactly similar and all API methods applicable to StringBuffer are also applicable to StringBuilder in Java. On the other hand String vs StringBuffer is completely different and there API is also completely different, same is true for StringBuilders vs String.
Summary
1) String is immutable while StringBuffer and StringBuilder is mutable object.
2) StringBuffer is synchronized while StringBuilder is not which makes StringBuilder faster than StringBuffer.
3) Concatenation operator "+" is internal implemented using either StringBuffer or StringBuilder.
4) Use String if you require immutability, use Stringbuffer in java if you need mutable + threadsafety and use StringBuilder in Java if you require mutable + without thread-safety.
http://javarevisited.blogspot.com/2011/07/string-vs-stringbuffer-vs-stringbuilder.html#ixzz1uZg3oqwb
String is one of the most important classes in Java and anyone who starts with java programming uses string to print something on console by using. No matter which kind of application you are working you will find heavy usage of java String class but if you do profiling of your application you will find that String is the one class which creates lots of garbage because of many temporary Strings created in program. In this article we will see What is String, some important properties of String in Java, What is StringBuffer in Java , When to use StringBuffer in Java , StringBuilder in Java and how it can be used in place of StringBuffer, What are differences between String and StringBuffer and StringBuilder in Java and mostly String vs StringBuilder vs StringBuffer. Now let's start with String.
String in Java
Before looking difference between String and StringBuffer or StringBuilder let’s see some fundamental properties of String Class in Java
1) String is immutable in Java: String is by design immutable in java you can check this post for reason. Immutability offers lot of benefit to the String class e.g. his hash code value can be cached which makes it a faster hashmap key; it can be safely shared between multithreaded applications without any extra synchronization. To know why strings are immutable in java see the link
2)when we represent string in double quotes like "abcd" they are referred as String literal and String literals are created in String pools.
3) "+" operator is overloaded for String and used to concatenated two string. Internally "+" operation is implemented using either StringBuffer or StringBuilder.
4) Strings are backed up by Character Array and represented in UTF-16 format.
5) String class overrides equals() and hashcode() method and two Strings are considered to be equal if they contain exactly same character in same order and in same case. If you want ignore case comparison of two strings consider using equalsIgnoreCase() method. To learn how to correctly override equals method in Java see the link.
7) toString() method provides string representation of any object and its declared in Object class and its recommended for other class to implement this and provide string representation.
8) String is represented using UTF-16 format in Java.
9) In Java you can create String from byte array, char array, another string, from StringBuffer or from StringBuilder. Java String class provides constructor for all of these.
Problem with String in Java
One of its biggest strength "immutability" is a biggest problem of Java String if not used correctly. many a times we create a String and then perform a lot of operation on them e.g. converting string into uppercase, lowercase , getting substring out of it , concatenating with other string etc. Since String is an immutable class every time a new String is created and older one is discarded which creates lots of temporary garbage in heap. If String are created using String literal they remain in String pool. To resolve this problem Java provides us two Classes StringBuffer and StringBuilder. String Buffer is an older class but StringBuilder is relatively new and added in JDK 5.
Differences between String and StringBuffer in Java
Main difference between String and StringBuffer is String is immutable while StringBuffer is mutable means you can modify a StringBuffer object once you created it without creating any new object. This mutable property makes StringBuffer an ideal choice for dealing with Strings in Java. You can convert a StringBuffer into String by its toString() method. String vs StringBuffer or what is difference between StringBuffer and String is one of the popular interview questions for either phone interview or first round. Now days they also include StringBuilder and ask String vs StringBuffer vs StringBuilder. So be preparing for that. In the next section we will see difference between StringBuffer and StringBuilder in Java.
Difference between StringBuilder and StringBuffer in Java
StringBuffer is very good with mutable String but it has one disadvantage all its public methods are synchronized which makes it thread-safe but same time slow. In JDK 5 they provided similar class called StringBuilder in Java which is a copy of StringBuffer but without synchronization. Try to use StringBuilder whenever possible it performs better in most of cases than StringBuffer class. You can also use "+" for concatenating two string because "+" operation is internal implemented using either StringBuffer or StringBuilder in Java. If you see StringBuilder vs StringBuffer you will find that they are exactly similar and all API methods applicable to StringBuffer are also applicable to StringBuilder in Java. On the other hand String vs StringBuffer is completely different and there API is also completely different, same is true for StringBuilders vs String.
Summary
1) String is immutable while StringBuffer and StringBuilder is mutable object.
2) StringBuffer is synchronized while StringBuilder is not which makes StringBuilder faster than StringBuffer.
3) Concatenation operator "+" is internal implemented using either StringBuffer or StringBuilder.
4) Use String if you require immutability, use Stringbuffer in java if you need mutable + threadsafety and use StringBuilder in Java if you require mutable + without thread-safety.
http://javarevisited.blogspot.com/2011/07/string-vs-stringbuffer-vs-stringbuilder.html#ixzz1uZg3oqwb
发表评论
-
Java Collection summary
2012-06-16 02:40 577Collection:List、Set Map: ... -
When to use Comparable vs Comparator
2012-06-15 00:52 792I have a list of objects I need ... -
Arrays.fill with multidimensional array in Java
2012-06-15 00:09 696How can I fill a multidimension ... -
Immutable objects
2012-06-14 23:49 720Immutable objects are simply ... -
Implementing hashCode; Transaction.java
2012-06-14 23:43 826Below is the syntax highlight ... -
Lazy initialization
2012-06-14 22:48 802http://www.javapractices.com/to ... -
How to sort an array,mid of linkedlist, reverse int
2012-06-13 07:47 939A common mistake for a beginner ... -
Java各类型转换
2012-06-13 05:25 705各种数字类型转换成字符串型: String s = Str ... -
regular expression
2012-06-13 03:08 5201、Java对反斜线处理的 ... -
string functions
2012-06-13 00:09 853import java.util.*; public c ... -
String array to arraylist
2012-06-13 00:07 587There are some important thing ... -
core java interview summary
2012-06-12 04:11 386http://blog.sina.com.cn/s/blog_ ... -
programming with String
2012-06-12 01:43 555Question: 1) Write code to che ... -
Java高手必会的要点
2012-05-29 03:28 612http://developer.51cto.com/art/ ... -
How to use getClass().getClassLoader().getResource()
2012-05-29 03:13 1756This is the simplest wat to get ... -
How to override equals method in Java
2012-05-12 02:57 1543Object class holds some very in ... -
Top 30 Programming interview questions
2012-05-12 02:48 911Programming questions are integ ... -
10 example of using ArrayList in Java >>> Java ArrayList Tutorial
2012-05-12 02:37 882ArrayList in Java is most frequ ... -
How to use Comparator and Comparable in Java? With example
2012-05-12 02:21 770Read more: http://javarevisited ... -
Difference between HashMap and HashTable? Can we make hashmap synchronized?
2012-05-12 01:32 777This question oftenly asked in ...
相关推荐
在Java中,字符串是我们经常使用的数据类型,而String、StringBuffer和StringBuilder是Java中三种常用的字符串类。在这篇文章中,我们将从源码角度对String、StringBuffer和StringBuilder进行深入分析,了解它们之间...
在 Java 中,String, StringBuffer 和 StringBuilder 三个类都是用于字符操作的,但它们之间有着很大的区别。 首先,String 是不可变类,意味着一旦创建了 String 对象,就不能修改它的值。每次对 String 对象的...
在Java编程语言中,`String`、`StringBuffer`和`StringBuilder`是处理字符串的三个重要类,它们各自有特定的使用场景和优缺点。理解它们的差异对于编写高效的代码至关重要。 **String类** `String`是不可变的类,...
StringBuffer:字符创变量 StringBuilder:字符创变量 从上面的名字可以看到,String是“字符创常量”,也就是不可改变的对象。对于这句话的理解你可能会产生这样一个疑问 ,比如这段代码:
String、StringBuffer 和 StringBuilder 是 Java 语言中三种不同类型的字符串处理方式,它们之间存在着明显的性能和线程安全性差异。 String String 类型是不可变的对象,每次对 String 对象进行改变时都会生成一...
在Java编程语言中,`String`、`StringBuffer`和`StringBuilder`都是用来处理字符串的类,但它们之间存在显著的差异,主要体现在性能、线程安全性和使用场景上。 首先,`String`是最基本的字符串类,它代表的是不可...
Java 中 String, StringBuffer 与 StringBuilder 的区别 Java 中 String, StringBuffer 与 StringBuilder 三种字符串类型的区别是很多开发者经常混淆或不了解的知识点。今天,我们将深入探讨这三种字符串类型的区别...
在Java编程语言中,String、StringBuilder和StringBuffer都是用来处理字符串的类,它们之间存在一些重要的区别,主要涉及到性能和线程安全性。 首先,`String`类代表的是字符串常量,一旦创建,其内容就不能改变。...
string,stringBuffer,stringBuilder
在Java编程语言中,String、StringBuffer和StringBuilder都是用来处理字符串的重要类,它们各自有特定的使用场景和特性。理解这三个类的区别对于任何Java开发者,无论是初学者还是经验丰富的程序员,都是非常重要的...
在Java编程语言中,String、StringBuffer和StringBuilder都是用来处理字符串的对象,但它们之间存在显著的区别。String是最常见的字符串类型,它的特点是不可变性。这意味着一旦一个String对象被创建,就不能进行...
在Java编程语言中,String、StringBuffer和StringBuilder都是用来处理字符串的重要类,它们各自具有不同的特性和使用场景。下面将详细解析这三个类的区别。 首先,`String`类是最基础的字符串处理类,它被设计为不...
在Java编程语言中,`CharSequence`是一个接口,它是字符序列的抽象基类,包括`String`、`StringBuffer`和`StringBuilder`等类。理解这些类的特点和用途是优化程序性能的关键。以下是对这些概念的详细解释: 1. `...
Java面试题-每日一题:String、StringBuffer、StringBuilder的区别
"Java 中 String、StringBuffer 和 StringBuilder 的区别及用法" Java 中 String、StringBuffer 和 StringBuilder 是三个常用的字符串操作类,了解它们之间的区别对 Java 开发者来说非常重要。本文将详细介绍这三者...
在Java中,我们有三种主要的字符串类:String、StringBuffer和StringBuilder,它们各自具有不同的特性和适用场景。 首先,`String`类是Java中的一个基础且不可变的类,被声明为`final`,其内部属性也是`final`的。...
Java中的字符串处理是编程中常见的任务,涉及到三个关键类:String、StringBuffer和StringBuilder。它们在功能上相似,但有着重要的区别,主要体现在字符串的可变性、线程安全性和性能方面。 1. 可变性 - String类...
在Java编程语言中,`String`和`StringBuffer`都是用来表示和操作字符串的重要类,但它们在使用场景和性能上有显著的区别。了解这些差异对于编写高效、优化的代码至关重要。 首先,`String`类是不可变的。这意味着...
在Java编程语言中,`StringBuffer` 和 `StringBuilder` 是两个重要的类,它们主要用于处理可变的字符串。这两个类在很多方面都非常相似,但存在一些关键的区别。 **一、StringBuffer和StringBuilder的区别** 1. **...