String Concatenation in Java


☞We can join two or more strings by using the following two methods.

1. By + (string concatenation) operator
2. By concat() method

Example

class ConcatExample1{
    public static void main(String args[]){
    
        String s1="inspire"+" "+"web"+" "+"soft";
        System.out.println(s1);
    }
}

Output

inspire web soft

Example

class ConcatExample1{
    public static void main(String args[]){
        
        String s1="Avlokan ";
        String s2="Mohan";
        String s3=s1.concat(s2);
        System.out.println(s3);

    }
}

Output

Avlokan Mohan

Example

class ConcatExample3{
    public static void main(String args[]){
        
        String s1=50+30+"inspirewebsoft"+25+25;
        System.out.println(s1);
    
    }
}

Output

80inspirewebsoft2525
Note : After a string literal, all the + will be treated as string concatenation operator.