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

A deep understanding of neural network

Basics Neural network is a mathematical function to solve some programming problems which is almost impossible to be solved with conventional programming. Neural network consist of neurons and weights. Neurons are made up with activation function and holds output of the function called activation value. Weights are just values which connects one neuron to another. Neurons are grouped and forms a layer. Each layer is connected with other layer by weights. There are three kind of layers; input layer, hidden layer and output layer. Any neural network contains at least one hidden layer, though there might be more than one hidden layers are possible. Input layers accepts input, calculate activation values for next hidden layer, the next layer does same process and pass result to next of it. This goes on until output layer is reached.  Each layer calculates Z values using weight and activation. Using Z values activation of next layer is calculated. Once we get activation of output laye...

`this` keyword in javascript

this keyword used to reference current object in javascript. There are two scenario where current object is determined differently. * Calling object represented by `this` in function. * Arrow function can reference object where the function defined. To demonstrate the above scenarios we shall take some examples. Suppose, var x = 10; (function() {    console.log(this.x); })() // 10 (() => {    console.log(this.x); })() // 10 There are two self executing functions both resulted in "10". At first look it seems behaving same, but there is basic deference what we going to see. const xObj = {     value: 10,     getValue: () => {         return this.value;     } }; const yObj = {     value: 10,     getValue: function () {         return this.value;     } }; xObj.getValue(); // undefined yObj.getValue(); // 10 Why undefined for `xO...