Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StringBuffer or StringBuilder in Servlet's doFilter method? [duplicate]

Tags:

java

I'm implementing my custom filter:

public class MyFilter implements javax.servlet.Filter

Which should I use in this doFilter method - StringBuffer or StringBuilder?

I would like to use it in this way:

StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(MY_CODE_HERE);
response.sendRedirect(stringBuffer.toString());

or...

StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(MY_CODE_HERE);
response.sendRedirect(stringBuilder.toString());

I know that StringBuffer is thread safe, but would a StringBuilder be enough?

like image 568
Piotr0123456 Avatar asked Jul 29 '13 13:07

Piotr0123456


People also ask

What is the difference between StringBuffer and StringBuilder?

As StringBuilder is not synchronized, it is not thread safe but faster than StringBuffer. Use String, for a string which will be constant through out the application. Use StringBuffer, for a string which can change in multi-threaded applications. Use StringBuilder, for a string which can change in single-threaded applications.

How to compare StringBuffer objects with StringBuilder objects in Java?

Here, we compared StringBuffer objects using compareTo () method of java.lang.Comparable interface, after converting StringBuffer object into String object 3. Sorting Collection of StringBuilder: StringBuilder class too do not implements java.lang.Comparable interface, as shown in StringBuilder class signature above

Is it possible to sort StringBuffer/StringBuilder in Java?

Whereas for StringBuffer/StringBuilder which do not implements java.lang.Comparable interface like String class, therefore it is must to provide to custom-logic to sort by implementing java.util.Comparator interface and overriding compare () method Hope, you found this article very helpful.

Is StringBuilder a thread safe class in Java?

StringBuilder was introduced in Java 5. StringBuffer is synchronized. This means that multiple threads cannot call the methods of StringBuffer simultaneously. StringBuilder is asynchronized. This means that multiple threads can call the methods of StringBuilder simultaneously. Due to synchronization, StringBuffer is called a thread safe class.


1 Answers

Local variables are thread-safe and variables declared inside the doFilter() method will be thread safe. Use StringBuilder for your purpose, as you shouldn't unnecessarily incur the overhead of synchronization used in StringBuffer.

Moreover , The Servlet request and response objects are created afresh for every new request and response and so by their nature they are thread safe. The doFilter() method will be executed in separate threads for each request.

Suggested Reading:

  1. Why are local variables thread safe in Java.
  2. StringBuilder and StringBuffer in Java
  3. servlet-filters tag wiki
like image 158
AllTooSir Avatar answered Nov 14 '22 22:11

AllTooSir