Free Data Structures and Algorithms Course

Subscribe below and get all best seller courses for free !!!


Subscribe to all free courses

How to concatenate two strings together in Java source code

Introduction :-
In Java, Concatenating two strings together is of high importance. Java programmer who is a beginner must know how to concatenated two string ?. Learning Java by example is the most effective proven technique. Java fundamentals are necessary to get an idea for writing an efficient code. Our program code in this leaf will need Java's basic knowledge such as : How to write your first Java program ?. and how to create, compile and run a java program ?.Java provides many mechanism to concatenated two strings. I will be discussing two of the mechanism here.

1. String concatenate operator (+).
2. String class method concat().

So to learn Java effectively , you should know these two mechanism to concatenate two strings. Let us begin with both the methods by an example and its explanation.
Concatenation of two Strings Explanation :-
By Concatenation of two Strings we mean that adding or appending one string to another string. We can also state that it is adding of two strings together. Concatenation of strings comes handy when dealing with the strings in a complex code. Generally concatenation of strings can occur in many ways. Most of the mechanism used is been coded into the sample code below. Let us look into the code step-by-step and see what it does?.

Program to demonstrate working of String Concatenation

public class Concatenation { 
    public static void main(String[ ] args) { 
        String ruler1 = " a ";
        String ruler2 = ruler1 + "b" + ruler1;
        String ruler3 = ruler2 + "c" + ruler2;
        String ruler4 = ruler3 + "d" + ruler3;
        String ruler5 = " 1 ";
        String ruler6 = "2";
        String ruler7 = ruler5.concat(ruler6);

Output and Explanation of working of above sample code :-
Above program shows us the working of string concatenation operator + and String class concat method. When we run above program we get output as:
a b a
a b a c a b a
a b a c a b a d a b a c a b a
1 2
Line 4 to 6:- All the three lines shows the code how + operator is used to concatenate the strings together. Line 4 code String ruler2 = ruler1 + "b" + ruler1; takes ruler1 string value which is " a " appends value "b" to it. So our new value becomes " a b". Now our code takes value " a b" and appends " a " to it. So after line 4 gets completely executed ruler2 gets value as " a b a ". Same execution is done for line 5 and 6.

Line 15:- This line shows us the code for the String method called as concat(). This method takes one string as an argument and appends it to the value of the string calling that method. So in our case ruler5.concat( ruler6 ) takes ruler5 value and appends ruler6 value to it and assign that value to ruler7. In our case it takes " 1 " and appends "2" to it. Finally ruler7 gets the value as " 1 2".

Line 8 to 11 and Line 16:- All these 5-6 lines just prints the values of ruler1, ruler2, ruler3, ruler4 and ruler7 on the console.
© 2021 Learn Java by Examples Template by Hubberspot