Skip to main content

Why String is immutable in java?

The question asked lot of time to lot of people. Each has some reasons for immutability of java strings.

Java has primitive types which are inherently immutable.

int i = 0;
void changeValue(int val) {
    val = 1;
}
changeValue(i);

Value of "i" is still 0.

The same way strings are just sequence of character. It should be primitive as well. It brings peace of mind with it.

As primitive has + and - operations strings also have concatenation and substring operations. String is not primitive so java has workaround for it and made it feels like primitive.

All this lead us to immutable string object.

The same rules applied to other java objects like. All wrapper classes (Integer, Float etc.), BigDecimal, BigInteger, Color etc. 

Comments

Popular posts from this blog

Basics of quantum computing: change of basis

Computing requires classical bits, which can hold a value of 0 or 1. Quantum computing gets benefit from superposition of quantum bit (qubit). Qubit is a representation of state of quantum object (electron, proton, photon, etc). The qubit holds a value between 0 and 1 while it is in superposition state. Superposition means the qubit possesses multiple values at the same time so we can harness its power for parallel computing. Once we try to measure the value it can be collapsed into either 0 or 1 based on probability. (Observer collapses wave function simply by observing.) Using bloch sphere, we can represent qubit physically. (The bloch sphere is a physical model to represent a spin state of qubit) A state vector can point to any direction from the center of the sphere. If a vector points to Z+ and Z-, it represents 0 and 1 state respectively. All other vectors represent superposition state in a Z basis. Now, if a vector points to X+, X-, Y+, Y-, all are at a 90-degree angle from ...

Monad: in programming

Monad is a structure with type constructor M and two functions unit ( η) and multiplication ( μ). and   To lift value from a to Ma we need unit function and to flatten from M(Ma) to Ma we need multiplication . Why do we need monad in functional programming? Why do we need functional programming anyways? Functional programming is a paradigm which uses pure functions to build an application. The power of pure functions are, we can compose them and we can build complex things out of the small and simple functions. If we have these functions   and   We can get   We can use below general purpose compose function to compose any two pure functions.   If we need to compose three functions then we can do like Now, we can't build application using only pure functions. It is next to impossible. We need side effects anyways to make the application practical in use. To achieve the goal, we need to keep pure functions in center/core part of application and need to move...

synchronized, wait() and notify() in java

synchronized keyword in java used to make sure that the code block would be executed by one and only one thread at a time. Using the keyword one can write a code block which would be side effects free from other concurrently running thread. There are two way we can use the keyword: 1) synchronized block and 2) synchronized method. synchronized (lockingObject) {     // code which must be executed by single thread at a time. } public synchronized void doWork () {     // code which must be executed by single thread at a time. } The synchronized block locks on lockingObject whereas synchronized method locks on object of that method ( this ). Good example of this is to achieve singleton pattern. public class Singleton {     private static Singleton s;     public static Singleton getInstance () {         if ( s == null ) {             synchronized (Singleton.class) {    ...