StringBuffer 1 « StringBuffer « Java Data Type Q&A

1. StringBuilder and StringBuffer in Java

What is the main difference between StringBuffer and StringBuilder? Is there any performance issues when deciding on any one of these?

2. How is a StringBuffer passing data through voids with no fields in the Class?

Given: Class has no fields, every variable is local. littleString was created by refactoring bigString in Eclipse:

public String bigString()
        StringBuffer ...

3. How to get adress of a Java Object?

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. ...

4. Strings are immutable - that means I should never use += and only StringBuffer?

Strings are immutable, meaning, once they have been created they cannot be changed. So, does this mean that it would take more memory if you append things with += than if you ...

5. what is the difference between String and StringBuffer in java?

what is the difference between String and StringBuffer in java? is there maximum size for String?

6. How to Reassign value of StringBuffer?

How can we re assign the value of a StringBuffer or StringBuilder Variable?

StringBuffer sb=new StringBuffer("teststr");
Now i have to change the value of sb to "testString" without emptying the contents. I am looking ...

7. what is the difference between StringBuilder and Stringbuffer?

Possible Duplicate:
StringBuilder and StringBuffer in Java
what is the difference between StringBuilder and Stringbuffer?

8. String, StringBuffer, and StringBuilder

Please tell me a real time situation to compare String, StringBuffer, and StringBuilder?

9. java StringBuffer and String Comparision

Why String.equls() returns true but Stringbuilder.equals() returns false?

    StringBuffer sb1 = new StringBuffer("Amit");
    StringBuffer sb2= new StringBuffer("Amit");
    String ss1 = "Amit";

10. remove certain lines from a StringBuffer

A legacy app program has a huge String Buffer (size sometimes upto an Mb) and it is processed sequentially for modifying the contents. I have to implement a change wherein I ...

11. Using String or StringBuffer in Java: which is better?

I read a lot about using StringBuffer and String especially where concatenation is concerned in Java and whether one is thread safe or not. So, in various Java methods, which should be ...

12. StringBuffer Object Problem

StringBuffer sb = new StringBuffer("abc");
StringBuffer sb1 = sb;

StringBuffer sb2 = new StringBuffer("abc");
How many objects are created?

13. StringBuffer vs StringBuilder Vs StringTokenizer

what is the difference between the StringBuffer vs StringBuilder Vs StringTokenizer on the internal implementation. when to use these . kindly waiting for the answer.


I am also going through the source ...

14. Java StringBuffer questions

class MyStringBuffer {
    //TODO explain: why you would need these data members.
    private char[] chars; //character storage.
    private int length;   ...

15. String or StringBuffer

private void doShareEmp(pageBean UTIL, HttpServletRequest request, String page)
        throws Exception
    doAction(request, UTIL, page);
    String action = pageBean.getSafeRequestOrNullParameter(request, "DO");

16. What's a real-world example of using StringBuffer?

I'm using Java 6. I've only written a couple of multi-threaded applications so I've never encountered a time when I had several threads accessing the same StringBuffer. Could somebody give me a real ...

17. Difference between StringBuffer and StringBuilder class

I could get more on the internet and from Sun Java. But needed to get the clear difference with the help of an example? StringBuffer or StringBuilder Whats the difference and when to ...

18. String, StringBuffer, StringBuilder. What were they thinking? What are we thinking?

Important: I know the differences between them, is a question related to the architecture of the plataform and some design decisions. Have you ever thought about the "semantic" implications in this changes ...

19. Storing StringBuffer

I have a StringBuffer in my Class which deletes certain characters if the conditions are met in the IF statement and prints the statement. However, this could be repeated many times. ...

20. Can java.lang.StringBuffer be replaced by the "regular" string manipulation logic in ColdFusion?

I am not an expert in Java; I am hoping that someone on this list who are more proficient in Java can help me out. I have the following codes on my ...

21. StringBuffer is obsolete?

In the book "Effective Java", Josh Bloch says that

StringBuffer is largely obsolete and should be replaced by the non-synchronized implementation 'StringBuilder'
. But in my experience, I've still ...

22. What is the complexity of this simple piece of code?

I'm pasting this text from an ebook I have. It says the complexity if O(n2) and also gives an explanation for it, but I fail to see how. Question: What is the ...

23. fixed-length StringBuffer in java

what is the best practise to hold a stringbuffer length fixed in java ? That is if the fixed value is 10 and stringbuffer holds ABCDEFGHIJ, when we append K ...

24. Storing in String

Possible Duplicate:
What's the most elegant way to concatenate a list of values with delimiter in Java?
I have 3 different words listed in property file ...

25. StringBuffer behavior for NULL objects

I am not able to understand the following behavior of StringBuilder when NULL objects are appended to an instance:

public class StringBufferTest {

     * ...

26. String and StringBuffer

Hi, As we all know that String class creates immutable strings and if we modify a string then a new string object is created that contains the modifications But creating new objects everytime uses more memory. I want to know that then why do we always use String class and not StringBuffer? thanx.. Nidhi

27. String Vs Stringbuffer

28. StringBuffer problem

29. About String & StringBuffer

Howdy! Yeah, it looks like you found yourself a one-way ticket to compiler hell! I'd say tryin' this instead: public class AQuestion{ public void method(StringBuffer s){ if(s.toString().equals("")) System.out.println("StringBuffer is null"); else System.out.println("StringBuffer is not null"); } public void method(String s){ if(s.equals("")) System.out.println("String is null"); else System.out.println("String is not null"); } public static void main(String args[]){ AQuestion question = new AQuestion(); question.method(new ...

30. Ya got String, got StringBuffer - and now ya got StringBuilder!

StingBuffer is like Vector. Most of the time the synchronization is unnecessary; when it is necessary, it's probably at the wrong level. (I.e. you probably need synchronization in a higher-level sync block, if you need it at all.) In most code the difference in performance is probably negligible. However since standard libraries sometimes need to be called many times in a ...

31. using plus inside StringBuffer

Sure ... The + operator to concatenate two String objects. The + operator appears innocent, but the code generated produces some surprises. Using a StringBuffer for concatenation can in fact produce code that is significantly faster than using a String. To discover why this is the case, we must examine the generated bytecode from our two examples. The bytecode for the ...

32. String and StringBuffer

I realize there is some benefit to constructing long Strings by using a StringBuffer object and then calling toString(), rather than repeatedly concatenating String objects. I'm wondering though, if there is some rule about how many Strings are being concatenated that makes it easier to decide if I should use the StringBuffer method. If I'm just concatenating two strings, is it ...

33. StringBuffer truncated

Hi all, need your help! we have a servlet that generate html. it uses StringBuffer with around 7500 character and it gets truncated. well, the system already implemented - so don't what the story and I have been asked to debug this damn thing. any idea how to fix this?? is it a bug? thanks folks tony

34. String and StringBuffer::: null

String is a rare class in Java. It's allowed to straddle the line between behaving as an object (using new() ) and behaving as a primitive )using direct assignment. The designers knew String objects would be prominent in almost any program. To conserve the run-time cost of creating them, they chose to allow static String assignments. Static assignments are ...

35. StringBuffer

Hi, Could anyone please mail be which one would be better of the below two in performance. How many strings will be created in Method1 and how many in Method2? Thanks in advance. Method - 1 String b = "beta"; StringBuffer buf = new StringBuffer("The strings are "); buf.append("a"+ b + "c"); Method - 2 String b = "beta"; StringBuffer buf ...

36. Easy stringbuffer questions

I have legacy code where this is done often: StringBuffer sqlStmt= new StringBuffer(""); sqlStmt.append("lots of stuff"); sqlStmt.append...again and again sqlStmt = sqlStmt.delete(0, sqlStmt.length()); sqlStmt.append("lots of stuff"); sqlStmt.append...again and again sqlStmt = sqlStmt.delete(0, sqlStmt.length()); Why use delete? Why not simply: StringBuffer sqlStmt= new StringBuffer(""); sqlStmt.append("lots of stuff"); sqlStmt.append...again and again sqlStmt = new StringBuffer(""); sqlStmt.append("lots of stuff"); sqlStmt.append...again and again sqlStmt = ...

37. StringBuffer

You need to host the file on some kind of server and transmit it via JSP or Servlets. Have you set up a Java or J2EE servlet before? I'd download and try Tomcat out, then try some JSP writing tutorials. Once you've got that covered you can output the string buffer data pretty easily. Alternatively, if you have a non-java server ...

38. String or StringBuffer?

Hi, I have a question on writing a method signature, whether to pass an argument as a String or a StringBuffer? The question is in the lines of the performance and also in the lines of pass by value/reference. Here's the scenario: I'm writing a method to parse an xml and add the XML attributes to a file, And within this ...

39. StringBuffer in Java

Hello, I was using a final variable to store a StringBuffer object; I was thinking that Java should not allow me to append that StringBuffer, as the variable that holds this StringBuffer is final. But, I'm surprised to see that Java allows me to do that. See below the example. public final StringBuffer s = new StringBuffer("Text1"); s.append(" Text2"); System.out.println(" S ...

40. Difference between String and StringBuffer

Hello everybody, It's amazing to see the help regarding this topic "Difference between String and StringBuffer", Every topic says that String is immutable where as StringBuffer is mutable. But please can any one be more elaborative on this topic. I mean what exactly we can do with StringBufer and we can't with String. A practical explanation will be the best answer ...

41. why dont we prefer to use StringBuffer

They are different ways of representing String data, so the choice of when to use which depends on your circumstance. The core difference is, I would say, that String objects are immutable whilst StringBuffer objects are mutable. At a very simple level String is best when your data won't change. StringBuffer is best for runtime String concatenation (the compiler will take ...

42. StringBuffer VS StringBuilder

I am a little confused about the difference between StringBuilder and StringBuffer. Practically they both look like they do the same thing and I did Google on this and got this on JGuru : " The StringBuilder class was introduced with JDK 5.0. Essentially, a StringBuffer is a thread-safe version of StringBuilder. If you are only adding/removing characters from a single-thread, ...

43. StringBuffer

44. Strings and StringBuffer

45. usng StringBuffer

46. String and StringBuffer

StringBuffer has a performance advantage when you concatenate a lot of variable parts into one string. You can try some experiments on your target platform to see when "a lot" becomes worth the effort of using a StringBuffer. StringBuffer is also mutable, meaning you can change it, while String is not. Look through the doc and compare methods. What can one ...

47. StringBuffer problem

48. Value of using StringBuffer for this example

Yes - but only one entry, no matter how many times the code is called. The idea that we need to "keep the string literal pool as small as possible" really is not necessary in the vast majority of applications. (Unless maybe you're using String.intern() an awful lot for some reason.) It really isn't something Jared needs to worry about.

49. StringBuffer is useless ?

I suddenly get a new thought here. So assumed that making the string buffer synchronized was of no use which they realized in java 1.5 and going by same idiom we have vector. Here is the text from Java Concurrency in Practise If synchronization is the cure for race conditions, why not just declare every method synchronized? It turns out that ...

50. StringBuilder slower than StringBuffer!?

StringBuilder javadoc API: "Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations." Both StringBuilder and StringBuffer classes extend AbstractStringBuilder class. The methods of StringBuffer are synchronized. Naturally the above statement would make sense and expected. But the below test executed multiple times always gives faster execution times for ...

51. Cleanup StringBuffer

In answer to the "Heap" posting ... one of the nice things about Java is that you don't have to care (for the most part) about your memory management. The internals do all that for you. The code that I just posted is all that you have to do ... any memory reallocation is done by the JVM. ------------------ Chris Stehno ...

52. StringBuffer Question

Being new to OO in general, comparing objects is a concept I've been struggling with, so this is a great question. If someone wouldn't mind confirming my thoughts on this I'd appreciate it. sb1 and sb2 are entirely seperate instances of a StringBuffer object complete with their own references and that is why they aren't equal, because the references are different? ...

53. Why use StringBuffer?

I believe that some code like this will be altered by the compiler: String myString1 = "add " + "all " + "these " + "bits " + "together"; //gets interpreted as: StringBuffer tempBuffer = new tringBuffer(); tempBuffer.append("add "); tempBuffer.append("all "); tempBuffer.append("these "); tempBuffer.append("bits "); tempBuffer.append("together."); and this is fine, since it's more efficient but happens under the covers so you ...

54. StringBuffer Very Urgent please

55. StringBuffer question

56. When The default value of "StringBuffer" is fill?

Hi again Danish, Ok, let's take another crack at this. Are you asking why SUN in it's infinite wisdom decided to allocate a default buffer with a 16 char capacity? If that's your question, I have no earthly, freakin' idea, but I suspect it went something like this: System engineer dude 1: "Hey Joe how may char's should the initial buffer ...

57. StringBuffer

58. String & StringBuffer

59. Stringbuffer vs. String

Strings are immutable, which means that you can not change their contents. This is very useful for things that represent "primitive" kinds of values, like numbers and strings and such. It allows you do to something like: String myString = "Hello World"; yourMethod( myString ); Because the string is immutable, I know that no matter how hard you try (or how ...

60. StringBuffer Question

I need to delete all characters in a StringBuffer from the beginning through the first occurance of a newline character. Essentially, I am deleting the first line. I am aware of the delete method that takes the start and end, but I do not know how to determine the end index, or the first occurrance of the newline. There does not ...

61. StringBuffer

62. Max length/capacity for StringBuffer

63. About "StringBuffer"

64. StringBuffer problem

65. StringBuffer holding value

Hey everybody. It's been a while, but I'm trying to get back on track here... I've read up on the StringBuffer, but I don't understand what's going on here. It might have something to do with the servlet lifecycle - I don't know... Basically what is happening is that my string buffer is holding values that I don't want it to. ...

66. splitting a StringBuffer into tokens?

Hello. I'm just getting started with Java and having a bit of difficulty using Strings and StringBuffers. I decided to make a class that would take a string (this was before I knew there two types in Java!) representing a name, split it into its constituent parts, capitalise the first letter of each of these and put them into an array. ...

67. String and StringBuffer

Reasons to use String instead of StringBuffer: 1. Many methods that take a String argument don't accept a StringBuffer argument. 2. The String class has many useful methods and constructors not available from StringBuffer. 3. String is handled very efficiently in terms of memory and execution time by most jvm's. Of course, objects that change often should be of class StringBuffer ...

68. String Vs StringBuffer

The important thing to remember is that: line 1: String a = "1"; line 2: String b = "2"; line 3: a += b; causes the String object referenced by "a" to be dereferenced at line 3, and so eligible for garbage collection (which can be seen as a delayed overhead) and a new String object, to which "a" is pointed, ...

69. String Vs StringBuffer

71. When should I use StringBuffer instead of String?

A String is a string. A StringBuffer is a tool for building Strings. When you need to pass an argument to a method named, say, setName(), the argument, presumably a "name", ought to be a String. When you need to return something from a method named, say, getName(), and it's supposed to return the "name" of something, then that method should ...

72. String >StringBuffer

public class StTest { public static void main(String[] args) { String s1 = new String("Danny"); System.out.println("s1 = " + s1.toUpperCase());//prints DANNY System.out.println("s1 = " + s1);//prints Danny - hasn`t been changed s1=s1.toUpperCase(); //apply change System.out.println(s1); //has kept change StringBuffer sb = new StringBuffer(s1);// value passed DANNY System.out.println("sb = "+sb); //prints DANNY System.out.println("sb reverse = " + sb.reverse());//reverse sb System.out.println( sb ); ...

73. String and StringBuffer

74. StringBuffer Confusion?????

Also tell me if i want that comparision of sb1 and sb2 should return true value.......... let me make myself more clearer... I have created two instances of string Buffer and have stored same value in both say "Megha". as they are pointing to different memory locations, but i want to only compare the contains only and as both contain "megha" ...

75. Problem with StringBuffer

It's almost ( :roll: ) always better to be clear about your context and what your problem is -- what you are trying to do -- than to just ask a specific question about subproblem. It appears that you've had trouble in the past inserting text with embedded single quotes into your database, and as a result of that, you are ...

77. StringBuffer()

78. doubt in StringBuffer

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - ...

79. What is the advantage of using String over StringBuffer?

Thank Sharath Kumar for your reply. I think what you have told is the behaviour of string but i want to know the advantage of using String over String Buffer. The thing you have mentioned is that we can't change the String value but that can be achieved by another way of using 'final' keywork to any variable! Hi All, Please ...

80. String and StringBuffer

Using concatenation operation on String objects are not transparent to the user. As you said, Strings are immutable. That is once a String object is created, it cannot be changed. This is the case even in case of concatenation. The only thing is this is not known to the user. String str1 = "hello "; String str2 = "world "; str1 ...

81. StringBuffer help

82. String and StringBuffer

84. StringBuffer

Hi Ernest Thanks a lot for your description. I am unable to follow the below lines . Is there any chance that you can elobrate it. Now, the designer of this method had an idea which s/he felt was clever. The "append" method doesn't need to return anything; it could just return 'void'. But instead, s/he made it return "this" -- ...

85. Strings and StringBuffer

86. Strings and StringBuffer

class Main { public static void main(String[] args) { final int max = 3000; long time = System.currentTimeMillis(); String s = ""; for(int i=0; i

87. StringBuffer does not have replaceAll

88. Stringbuffer class?

90. stringbuffer :nullpointerexception

import java.util.*; import*; public class d7 { private static BufferedReader ifile; private static PrintWriter ofile; private static StringBuffer sb; private static String s0; public static String removepunct(String s) { for(int i = 0;i < s.length();i++) { if(Character.isLetter(s.charAt(i))) sb.append(s.charAt(i));// <- nullpointerexception here! } return sb.toString(); } public static void main(String[] args) throws IOException { BufferedReader ifile = new BufferedReader(new FileReader(args[0])); PrintWriter ...

91. reg: string and stringbuffer

93. String and StringBuffer

94. Looping StringBuffer

I didn't get you what you are trying to say could you please give small example for my problem it may solve my problem i don't understand why we need to use [code] tags could you please give small example if you have enough time please apologise me if you don't have enough time.

95. String and StringBuffer

Because String is immutable, the Java compiler can do certain optimizations with it. For example, if you create multiple strings with the same (compile-time constant) value, then the compiler will create only one String object, and make both variables refer to that one String object. This saves memory. This optimization wouldn't be possible if strings would be mutable.

96. align output with StringBuffer

When I use printf that is also working fine. But I don't know what is printf(I remember we use it in C) class StringFormatting { public static void main(String[] args) { String ramya="ramya"; String brilliant="brilliant"; String format = "|%1$-30s|%2$-10s|%3$-20s|\n"; //String s = String.format(format, "a", "b", "c"); System.out.printf("%c%20s is %-20s%c", '*', "Campbell", "brilliant", (char)0x2a); System.out.println(); //System.out.printf("%c%10s is %-10s%c", '*', "Ramya", //brilliant", (char)0x2a); ...

97. regarding StringBuffer

Always use line.separator if you can. The most likely explanation is that there was a tiny spelling error in what you were submitting first time. You know you can get an array of properties from a method in the System class, then you can go through them and get the correct spelling, preferably with copy-and-paste.

98. String VS StringBuffer

99. StringBuffer confusion ?

If your stringBuffer contained "Hello. My name is Inigo Montoya.", what would you increment this to? Further, I don't think this is a well defined problem, especially since different character sets may not have letters in the same order, or consecutive letters stored as consecutive integers. And what would you increment a 'z' to? Can you define the problem better? Tell ...

100. Stringbuffer and carriage return

Hi All, I am trying to read a file and then save all of its content in one stringbuffer so that I will read the file just once. public static String readFile(File aFile){ StringBuffer sb = new StringBuffer(); BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(aFile)); String strRead = ""; while((strRead = reader.readLine())!=null){ sb.append(strRead); sb.append("\r\n"); } reader.close(); } ...