05.0 Java Notes‎ > ‎Java: String‎ > ‎

Java String: concat()

Concat is short for concatenation, which means to join two strings together

public void concatMethod1()
    {
        String str1;
        String str2;
        String str;

        // set up the values of the two strings to join together
        str1 = "Joe";        
        str2 = "Student";

        // join the two strings together
        str1 = str1.concat(" ");   // why is this line needed?
        str = str1.concat(str2);

        // This will also work, but may not be as clear
        // str = str1.concat(" ").concat(str2);
        
        // output the new string

        System.out.println(str);
    }

public void concatMethod2()
    {
        String str1;
        String str2;
        String str;

        // set up the values of the two strings to join together
        str1 = "Joe";        
        str2 = "Student";

        // join the two strings together
        str = str1 + " " + str2;

     
        // output the new string

        System.out.println(str);
    }

Comments