stringbuffer

replaceAll does not replace string [duplicate]

时光总嘲笑我的痴心妄想 提交于 2019-11-26 23:36:18
问题 This question already has an answer here: String replace method is not replacing characters 5 answers I want the text "REPLACEME" to be replaced with my StringBuffer symbols. When I print symbols, it is a valid string. When I print my query, it still has the text REPLACEME instead of symbols. Why? private String buildQuery(){ String query = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(REPLACEME)&format=json&env=store%3A%2F

(转)StringBuilder与StringBuffer和String 的区别

梦想与她 提交于 2019-11-26 22:13:15
很多人对String和StringBuffer的区别已经很了解了吧,可能还有人对这两个类的工作原理有些不清楚的地方,复习一下吧,顺便牵出J2SE 5.0( 文档 )里面带来的一个新的字符操作的类StringBuilder。那么这个StringBuilder和StringBuffer 以及我们最早遇见的 String 类有那些区别呢?在不同的场合下我们应该用哪个呢?我讲讲自己对这几个类的一点看法,也希望大家提出意见。 简要的说,String类型和StringBuffer类型的主要性能区别其实在于 String 是不可变的对象,因此在每次对String类型进行改变的时候其实都等同于生成了一个新的 String对象,然后将指针指向新的String对象,所以经常改变内容的字符串最好不要用 String,因为每次生成对象都会对系统性能产生影响,特别当内存中无引用对象多了以后, JVM 的GC就会开始工作,那速度是一定会相当慢的。 这里尝试举个不是很恰当的例子: String Str = “abc”; For(int i = 0 ; i < 10000 ; i++){ Str + = “def”; } 如果是这样的话,到这个 for 循环完毕后,如果内存中的对象没有被 GC 清理掉的话,内存中一共有上万个了,惊人的数目,而如果这是一个很多人使用的系统,这样的数目就不算很多了

【翻译】Java中String, StringBuffer, StringBuilder的区别

人盡茶涼 提交于 2019-11-26 22:13:01
String 是 Java 中最重要的类之一,并且任何刚开始做Java编程的人,都会 用String定义一些内容,然后 通过著名的 System.out.println() 语句来打印 出来。 然而,很多Java新手都不会意识到 String在Java中是 不可变的(immutable) 并且最终的(final) 的 并且每次对String结果的调整都会创建一个新的String对象。所以我们会问, 如何在Java中操作字符串又能避免创建String垃圾呢? 对于这个问题 StringBuilder 和 StringBuffer 就是答案。 StringBuffer是一个老的类,而 StringBuilder是在Java5中新加入的,并且 StringBuilder 有了新的改进,主要像是 Enum , Generics , varargs methods 和 Autoboxing in Java 。不管你是做何种应用,你都会发现应用中使用可大量的Java String类。 但是如果你切实地分析了你的应用,你将会发现由于程序中创建了大量的临时String因此产生了了大量的垃圾。在这个 Java 向导 中我们会看到在Java中 String到是什么 , Java中String的一些重要属性, 在 Java 中 StringBuffer 是真么 , 在 Java 中什么时候使用

What is the difference between String and StringBuffer in Java?

旧时模样 提交于 2019-11-26 18:27:37
What is the difference between String and StringBuffer in Java? Is there a maximum size for String? Vivin Paliath String is used to manipulate character strings that cannot be changed (read-only and immutable). StringBuffer is used to represent characters that can be modified. Performance wise, StringBuffer is faster when performing concatenations. This is because when you concatenate a String , you are creating a new object (internally) every time since String is immutable. You can also use StringBuilder which is similar to StringBuffer except it is not synchronized. The maximum size for

What is difference between mutable and immutable String in java

旧时模样 提交于 2019-11-26 15:23:40
问题 As per my knowledge, a mutable string can be changed, and an immutable string cannot be changed. Here I want to change the value of String like this, String str="Good"; str=str+" Morning"; and other way is, StringBuffer str= new StringBuffer("Good"); str.append(" Morning"); In both the cases I am trying to alter the value of str . Can anyone tell me, what is difference in both case and give me clear picture of mutable and immutable objects. 回答1: Case 1: String str = "Good"; str = str + "

Create a string with n characters

落爺英雄遲暮 提交于 2019-11-26 12:22:47
问题 Is there a way in java to create a string with a specified number of a specified character? In my case, I would need to create a string with 10 spaces. My current code is: StringBuffer outputBuffer = new StringBuffer(length); for (int i = 0; i < length; i++){ outputBuffer.append(\" \"); } return outputBuffer.toString(); Is there a better way to accomplish the same thing. In particular I\'d like something that is fast (in terms of execution). 回答1: The for loop will be optimized by the compiler

How to get address of a Java Object? [duplicate]

落爺英雄遲暮 提交于 2019-11-26 11:27:28
问题 This question already has an answer here: Is there a way to get a reference address? [duplicate] 5 answers Is there a way to get address of a Java object? Where the question comes from?: At First, I read properties file and all the data from file was placed into table. Properties file can update. So, I want to listen that file. I listen an object using PropertyChangeSupport and PropertyChangeListener. updatedStatus = new basit.data.MyString(); updatedStatus.addPropertyChangeListener(new java

What is the difference between String and StringBuffer in Java?

女生的网名这么多〃 提交于 2019-11-26 06:27:12
问题 What is the difference between String and StringBuffer in Java? Is there a maximum size for String? 回答1: String is used to manipulate character strings that cannot be changed (read-only and immutable). StringBuffer is used to represent characters that can be modified. Performance wise, StringBuffer is faster when performing concatenations. This is because when you concatenate a String , you are creating a new object (internally) every time since String is immutable. You can also use

Difference between StringBuilder and StringBuffer

六月ゝ 毕业季﹏ 提交于 2019-11-25 22:20:24
问题 What is the main difference between StringBuffer and StringBuilder ? Is there any performance issues when deciding on any one of these? 回答1: StringBuffer is synchronized, StringBuilder is not. 回答2: StringBuilder is faster than StringBuffer because it's not synchronized . Here's a simple benchmark test: public class Main { public static void main(String[] args) { int N = 77777777; long t; { StringBuffer sb = new StringBuffer(); t = System.currentTimeMillis(); for (int i = N; i --> 0 ;) { sb