Java String Concatenation

    String Concatenation

    You can create a new string using the concat method from multiple strings. You can use below two ways to concat the strings-

    • By + (string concatenation) operator
    • By concat() method

    Using + operator-

    Example-

    class Simple{  
    
     public static void main(String args[]){  
    
    String s2="Hello"+"Welcome";
    
      System.out.println(s2);
    
     }  
    
    } 

    Output-

    Example-

    class Simple{  
    
     public static void main(String args[]){  
    
    String s2="Hello"+2+"Welcome"; 
    
    
    
      System.out.println(s2);
    
     }  
    
    } 

    Output-

    Using concat() method-

    This method will add the specified string at the end of the current string.

    Example-

    class Simple{  
    
     public static void main(String args[]){  
    
    String s1="Hello";
    
    String s2="Welcome"; 
    
     String s3=s1.concat(s2);
    
      System.out.println(s3);
    
     }  
    
    }

    Output-