Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is stringbuffer final?

Tags:

java

The string class is immutable and one of reasons behind that is the class is declared as final though there are other reasons also. But why is StringBuffer or StringBuilder final and still they are mutable? So what other factors are deciding for String to be immutable?

like image 708
Abhilash28 Avatar asked Apr 10 '15 17:04

Abhilash28


People also ask

What is the use of StringBuffer in Java?

StringBuffer class in Java. StringBuffer is a peer class of String that provides much of the functionality of strings. String represents fixed-length, immutable character sequences while StringBuffer represents growable and writable character sequences.

Why StringBuffer is mutable in Java?

Why StringBuffer is mutable in Java? We all know that the String class in Java is mutable i.e. once we create a String variable we cannot modify its data or do any manipulations. But, there may be scenarios where we need to modify the data of String variables.

Why is a string final in Java?

Originally Answered: Why is string final in Java? There is a difference between being final and immutable when it comes to objects. Final means that the reference cannot be reassigned to a new string whereas immutable means that the referenced object cannot be changed. Strings in Java aren’t final by default but they are immutable

Why string class is final class?

If a string is not immutable, changing the string with one reference will lead to the wrong value for the other references. One more thing about string class, it is final class so that no one can override or inherit of String class e.g. Immutability, Caching, hashcode calculation etc by extending and overriding behaviors.


1 Answers

StringBuffer and StringBuilder are mainly used for string concatenating operations within a single method, the code using them often being generating by the compiler. So being extended is not the typical use case.

On the other hand, being final allows better optimizations within a JVM, at least in the past; today’s HotSpot JVM does not require it, however, there never was a reason to change the final declaration from these classes.

Note that extending StringBuilder and overriding methods for polymorphic behavior would be a bit pointless as there is no public method within the entire JRE accepting or returning StringBuilder instance (besides within StringBuilder itself). There are Appendable and CharSequence filling this gap and offering much more flexibility.

The concepts of mutability and immutability are entirely different from the concept of final classes. They simply depend on what kind of methods or operations a class provides. String has no methods that allow modifying its contents while StringBuffer and StringBuilder have such methods. Declaring an immutable class final just helps prohibiting subclasses which could introduce methods supporting mutation, but that’s not a hard requirement.

like image 58
Holger Avatar answered Oct 06 '22 08:10

Holger