Strings are Immutable | StringBuilder | StringBuffer

Strings are Immutable | StringBuilder | StringBuffer:

In this post, let us discuss about Strings, why Strings are immutable where as StringBuilder and StringBuffer are mutable.


What is Immutability?

When we assign a value to a String variable, that value can never be changed. That is known as immutability.

Strings can be created in 2 ways,

String S1 = “One”;

String S1 = new String(“One”);

What is the difference between these two ways of String creation?

String creation Way 1:

First let us see the type 1, i.e. String S1 = “One”;

For better understanding, let us create 3 strings

In general, Whenever a String is created all the values will be stored in StringPool. Here we have created 3 Strings so all these values – “One”, “Two” and “One” should be stored in StringPool.

Let us see how these above String values are Stored in StringPool.


As we can see the values – “One” and “Two” are stored in StringPool but another value of String S3 = “One” is not stored in StringPool. What actually happened?

When the values are Stored in StringPool when Strings are created in above mentioned way, When two or more strings has the same value only one of that such value will be stored in StringPool and the address of that value in StringPool will be referenced to String Variables.

So from above, since S1 and S3 has same value only one of such value is stored in StringPool and the address Addr1 will be referenced to S1 and S3

So the String variables will point to,

S1 = Addr1
S2 = Addr2
S3 = Addr1

How does “==” Work when Strings are created in above mentioned way?

The “==” works by comparing the address of the String variable rather than comparing the exact value of Strings. So in this case if we try to compare (S1==S3), Since both has same address it will
return True.

What if we concatenate the String? Will the value be replaced?

As we discussed, Strings are immutable. That is, once a value has been created that value will never change.
Consider this example,

If we concatenate String S1, what will happen?

S1 = “One” + “Three”;

Will the above statement replace the value One in StringPool? The answer is No.

Now we have concatenated the String, How the values will be at StringPool?

The new concatenated value will be inserted into StringPool and the String Variable S1 will point to new inserted value address – Addr3, Where as the value “One” remains unchanged.
This is the reason Strings are said to be Immutable.

Second way of Creating a String:

Another way of creating a String is using new Keyword, Let us create 3 strings for our understanding.

So we have created 3 Strings – S1,S2 and S3 and assigned the value – “One”, “Two” and “One” using new keyword.

How are these values stored in StringPool?

As you can see the difference, the value “One” is inserted again for the variable S3. Why?

Because the way we created String, We have used new keyword. When we create a String using new keyword, each String is considered as new value and it will be Stored in StringPool even though the same value might be available in StringPool.

So here, The String S1 will have Addr1 pointed to it, S2 will have Addr2 and S3 will have Addr3 – even though S1 and S3 has same value, since we used new keyword to insert the String – it inserted the “One” as new value and new address is given.

How does “==” Work when Strings are created in second String creation way?

Since each value is created newly and inserted in StringPool, The result will always be false for S1==S3 as both S1 and S3 have different addresses.

Why is StringBuilder and StringBuffer are mutable?

StringBuilder internally extends AbstractStringBuilder, it has a value which has an underlying working of Array. Since Arrays are mutable and StringBuilder is an Array wrapped class, it is mutable.

What if we perform Concatenation in StringBuilder?

It will not create a new value, instead it will replace the existing value with newly concatenated value

So what is the difference between StringBuilder and StringBuffer?

The only difference between StringBuilder and StringBuffer is – StringBuilder is not Synchronized (i.e. Not Thread Safe) where as StringBuffer is Synchronized (i.e.Thread Safe)

When considering the performance, StringBuilder is faster than StringBuffer because, StringBuilder is not synchronized.

Coupling | Tight Coupling vs Loose Coupling | Examples

Coupling | Tight Coupling vs Loose Coupling | Examples

What is Coupling?
When one object is used by another object it is known as Coupling. In general, it is based on dependency. One object dependency over another object.

There are two types of Coupling,

  • Tight Coupling
  • Loose Coupling

First let us see about tight coupling,

Tight Coupling:

When one object is dependent very much over another object it is known as Tight Coupling. Let us understand with an Example.

For understanding this example, let us create an interface first,

We have a Job Interface with a method display in it.

Now to understand this example, let us create 2 classes. Doctor and Engineer which implements the Job Interface.

Now in main class,

This will give us the output,

So far so good, right?

Now let us consider the scenario, The requirement changes in future. We might have to add another professional for example Scientist. What we do now?

We need to create a class, implement job interface and override display method.

In, we need to give reference to Scientist, add the Scientist variable to Constructor and create a new object and pass Scientist Object and then call display() method in App. java.

Too much of work right?

This is known as tight coupling. One object too much dependent and coupled with another object.

So how can we overcome this, here we are going to use concept of Loose coupling.

By this time, you might have guessed – Reducing the dependencies between object is known as Loose Coupling.

Now for the above same requirement with Doctor and Engineer, let us see how can we write the main class with Loosely Coupling method,

Loose Coupling:

Reducing the dependencies of one object with another object is known as Loose Coupling.

The above main method is re-written again with concept of Loose Coupling.

This code looks simpler when compared to Tight Coupled Code.

Instead of passing Doctor and Engineer objects directly, we have passed Job as reference.

During the run time we are passing the required object to perform the process. In this case, even if the requirement changes in future we do not have to make much changes for the above code.

Even when a new profession is introduced, example : Scientist. Now it is easy for us to make code changes without much change.


Docker-Compose | Configure docker-compose | Container to Container Communication With Example

Docker-Compose | Configure docker-compose | Container to Container Communication With Example:

In our previous posts, we have seen how to Dockerize SpringBoot Application (here) and How to pull and push images in Docker Hub (here).

In this post, Let use see about Docker-compose.

What is a docker-compose file?
It is a tool for running multi-container applications. We can configure the services within the docker-compose file and with the help of single command, we will be able to bring up individual containers for each service configured in docker-compose file.

What are we going to do in this post?

  1. We are going to create 2 Spring Boot Applications
  2. We are going to create an image, tag them and push them to docker hub
  3. We are going to create and configure docker-compose file
  4. We are going to deploy the containers using docker-compose in docker swarm mode.

We are going to make a call from SpringBoot_Docker-1 application to SpringBoot_Docker application. When these 2 are deployed as an individual containers, we are going to see how these both containers are going to communicate with each other.

We are going to push and pull the image from docker hub, so if anyone is unaware about how to pull and push the image to/fro docker hub, I would suggest to go through – Docker Hub – Pull and Push Image before proceeding this post further.

Project Structure:

There are certain files which are common to both the projects, (Same for both the applications) (SpringBoot_Docker) (SpringBoot_Docker-1)

In the above call, we have used http://helloworld:8080/docker1, Where docker1 is the path of SpringBoot_Docker application. helloworld here is the service name, we can see about this when we configure docker-compose file.

Dockerfile (Same for both the applications)

Detailed explanation of Dockerfile is given Here.

Now let us create an image for the above 2 applications and push them to docker hub,

SpringBoot_Docker Application Image Build:

SpringBoot_Docker-1 Application Image Build:

We have successfully pushed our images to dockerhub.

Now let us configure docker-compose file,


The above is a simple docker-compose file for this example, there are many other properties that will be included in real time scenario.

Let us understand the docker-compose,

version: ‘3’:
This is the version of docker, We have to follow docker version and we cannot version it by ourselves.

Each application, for example – In our docker-compose we have included 2 spring boot applications, each application is considered as service in docker-compose file.

helloworld and helloworld2:
In docker-compose, we define the applications as services. Here the SpringBoot_Docker is named as helloworld service and SpringBoot_Docker-1 is named as helloworld2 service.

When writing the for SpringBoot_Docker-1, we have mentioned this service name in forming the URL. (http://helloworld:8080/docker1)

This plays an important role in container to container communication. Whenever we build an image and deploy the container for SpringBoot application individually, it will be in bridge network where containers will not be able to communicate with other container.

In docker-compose, by default network will be overlay, where overlay network is required for Container to Container communication.

We are exposing the external and internal ports for the services for the user.

We can use either Docker in our system or login in play with docker ( for deploying the application.

docker swarm init –> This will make the environment to run in Swarm mode.

After this use,

docker-compose up

This will bring up our application.

Now let us hit the API to check whether SpringBoot_Docker-1 able to communicate with SpringBoot_Docker applicaiton.

Code can be downloaded from Here.

Docker Hub – Push and Pull Images – Deploy as Container with Example

Docker Hub – Push and Pull Images – Deploy as Container with Example

In our previous article, We learnt how to Dockerize SpringBoot Application with Simple Hello World example.  In this post, let us see how can we push the image to docker hub and how can we pull it and deploy the application as docker container.

So for pushing and pulling the image from Docker Hub, first we need to create an account in Docker hub.

Docker Hub –, We can login here and create an account.

We are going to use the same example used in our previous article,





First let us create an image from the above docker file,

docker image build -t “sampleImage” .

In this example let us use image name as helloworld,

docker image build -t “helloworld” .

The above command will generate the image and can be seen with command,

docker images

We have successfully build the image, So in order to push the image to docker hub. First we need to tag it,

docker image tag “imageid” dockerhub username/repository

For this example, My dockerhub account is sriis1987,

docker image tag 09b9013b2982 sriis1987/helloworld

This will tag the image name, you can see it with docker images.

Then we need to push it,

docker image push dockerhubid/repository

For this example,

docker image push sriis1987/helloworld

Now in our docker hub account, we can see the image been successfully pushed.


When clicking this repository, we can see our pull command too (public view),

Now we can pull this image and we can deploy the container using imageid.

docker container run imageid.

Suppose if we update the same application, do we have to create new repository?

No, We dont have to.

Suppose if you update the existing file and you can push the image to same repository. It will just update the same image.


docker image tag “imageId” sriis1987/helloworld:v1

docker image push sriis1987/helloworld:v1

The above command will update the repository with the new image.


Suppose if it shows cannot authenticate or need to authenticate use command,

docker login

This is request for username and password of docker hub.


Dockerize SpringBoot Application – Hello World Example

Dockerize SpringBoot Application

In this post, Lets us create a simple SpringBoot application – Hello World and dockerize and deploy the application as container.

What is Docker?

Docker is a tool which helps us to create, deploy and run applications by means of Containers. Containers are like a package which has all the dependencies and libraries which is required to run the application successfully.

Docker is a platform for deploying Microservices.

There are many commands used in dockers, but for our example purpose we are going to use only 2 commands.

Commands to build an image and deploy as container.

Creating Image:

docker image build -t “imagename” .

Deploying Container:

docker container run “imageid” –> Generated after image creation.

Note: Docker has to be installed for the below application to work.

Project Structure:

We are going to create a sample controller and main springboot class.



So what are we doing with Dockerfile?

FROM – used to pull an image, here were taking the latest of Ubuntu. Usually the pull is from dockerhub (DockerHub). There are many base images already available in dockerhub. Similar to ubuntu, there is alphine, openjdk etc. which can be pulled from dockerhub using FROM.

MAINTAINER: It is for our own purpose, including the name

EXPOSE: Exposing the port where 8080:8080. First 8080 is internal port where as second is external exposure

WORKDIR: Working directory inside the container.

COPY: This is where we copy our jar file which is available in Target folder to within docker container. Here i am copying to /usr/local/bin/

ENTRYPOINT: This is where we run our application, we usually run as java -jar sample.jar the same is used in entry point.

After this, we run mvn clean install.

So, How to create image from Dockerfile?

Before creating an image, if you are using OracleVM make sure the instance is running.

We are going to use command prompt.

Command to build image: docker image build -t “helloworld” .

From the above the image has been created with our docker file.

Step 1: It has pulled openjdk:8 (FROM openjdk:8)

Step 2: Exposing the port 8080:8080 (EXPOSE 8080:8080)

Step 3: Work directory is set to the path (WORKDIR /usr/local/bin)

Step 4: Copied the jar from target folder to bin folder (COPY ./target/docker-0.0.1-SNAPSHOT.jar helloworld.jar)

Step 5: Entrypoint is executed.

Now we have created the image, now we can to deploy it as container.

we are going to use the command

docker run -p 8080:8080 “image name”

Here we are exposing the port and running that image as a container.

Here we have exposed the port 8080:8080 and started the container.

Now let us check the browser,

If noticed, we have used some kind of IP ( and not Localhost. This is because we have deployed this as container in the docker. the docker machine will have an IP address.

So how to check the ip address,

docker-machine ip 

This will give us the docker ip address,

Code can be downloaded at: Dockerize SpringBoot Application – Hello World Example