Table of contents
1.
Introduction
2.
concat()
2.1.
Concatenating two strings
2.1.1.
Output
2.2.
Concatenating multiple strings
2.2.1.
Output
3.
Frequently Asked Questions
3.1.
How is the + operator used to concatenate strings in Java?
3.2.
What are the other possible ways of concatenating strings in Java?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Java String concat()

Author Yashesvinee V
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Combining or joining two strings is a frequent operation performed by programmers for various applications. This can be carried out using many ways. In Java, strings can be joined or concatenated. Strings are joined using a specified delimiter, such as another substring or any special character. Strings are concatenated or combined to result in a new string. Concatenation can be achieved in two ways. 

  • By using the + operator
     
  • By using the concat() method.
     

In this blog, we shall concentrate on the use of concat() function.

concat()

The concat() method of the Java Strings class combines or appends one string to the end of another.

Syntax:

str1.concat(str2)
You can also try this code with Online Java Compiler
Run Code

 

Parameters:

str1, str2: The two Strings to be combined

Returns: String

str2 is attached to the end of str1, and the result is stored in another new string. The function returns a new string as String objects are immutable, i.e. they cannot be altered under the same name once initialised.

Concatenating two strings

The following example shows the use of the concat() function to join two separate strings.  

class Main 
{  
   public stattic void main(String[] args)
    {   
      String str1 = "Hello";
      String str2 = " world";
        
      String result = str1.concat(str2);
      System.out.println(result);

      result = "Say ".concat(str1);
      System.out.println(result);
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

Hello world
Say Hello

Concatenating multiple strings

The concat() function can be used to concatenate multiple strings in just a single statement as shown below. 

class Main 
{
    public static void main(String[] args) 
    {
        String str1 = "hello";
        String str2 = " world";
        
        String str3 = "\nLet's explore Strings";
        String str4 = " in Java";
        
        String result = str1.concat(str2).concat(str3).concat(str4);
        System.out.println(result);

    }
}
You can also try this code with Online Java Compiler
Run Code

Output

hello world
Let's explore Strings in Java

Frequently Asked Questions

How is the + operator used to concatenate strings in Java?

+ is an overloaded operator that can add numeric values as well as strings. Concatenation is implemented using the StringBuilder class’s append() method. The + operator can be used to concatenate strings with primitive values.

What are the other possible ways of concatenating strings in Java?

The append() method of the StringBuilder class, the join() and format() method of strings and the joining() method of the Collectors class are some other methods used to concatenate Strings in Java. 

Conclusion

In this article, we have extensively discussed the concat() function in Java. We also learnt the different ways of using the concat() method with examples.

Feeling curious? Coding Ninjas has you covered. Check out our articles on the StringBuffer class, StringBuilder class, and StringTokenizers in Java. Follow our Guided path for Programming in Java here.

Explore our Library on Coding Ninjas Studio to gain knowledge on Data Structures and Algorithms, Machine Learning, Deep Learning, Cloud Computing and many more! Test your coding skills by solving our test series and participating in the contests hosted on Coding Ninjas Studio! 

Check out this article - C++ String Concatenation
 

Recommended problems -

Looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc.? Look at the problems, interview experiences, and interview bundle for placement preparations.

Upvote our blogs if you find them insightful and engaging! Happy Coding!

Live masterclass