Java String concat() Method



The Java String concat() method is used to concatenate the specified string to the end of this string. Concatenation is the process of combining two or more strings to form a new string. Since strings are immutable in Java, we cannot change the string value once it has initialized.

A String is an object that holds the sequence of characters. The concat() method accepts a parameter as a string that holds the value of the other string. It throws an exception if one string has a null value.

Syntax

Following is the syntax of the Java String concat() method −

public String concat(String str)

Parameters

  • str − This is the String that is concatenated to the end of this String.

Return Value

This method returns a string value which is a result of the concatenation operation on the current string and the given value.

Example

If the given string value is not null, the concat() method concatenates them and returns a new string.

In the following program, we are creating an object of the string class with the values “Hello” and “World”. Using the concat() method, we are trying to concatenate them.

import java.lang.*;
public class Concat {
   public static void main(String[] args) {
      
      //create an object of the String
      String str1 = new String("Hello");
      String str2 = new String("World");
      System.out.println("The given string values are: " + str1 + " and " + str2);
      
      //using the concat() method
      System.out.println("After concatenation the new string is: " + str1.concat(str2));
   }
}

Output

On executing the above program, it will produce the following result −

The given string values are: Hello and World
After concatenation the new string is: HelloWorld

Example

If the given string argument value is null, this method throws a NullPointerException.

In the following example, we are instantiating the String class with the value “Tutorials”. Using the concat() method, we are trying to concatenate the current string value with null.

import java.lang.*;
public class Concat {
   public static void main(String[] args) {
      try {
         
         //instantiate the String class
         String str1 = new String("Hello");
         String str2 = new String();
         str2 = null;
         System.out.println("The given string values are: " + str1 + " and " + str2);
         
         //using the concat() method
         System.out.println("After concatenation the new string is: " + str1.concat(str2));
      } catch(NullPointerException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

Output

Following is the output of the above program −

The given string values are: Hello and null
java.lang.NullPointerException: Cannot invoke "String.isEmpty()" because "str" is null
	at java.base/java.lang.String.concat(String.java:2769)
	at com.tutorialspoint.StringBuilder.Concat.main(Concat.java:11)
Exception: java.lang.NullPointerException: Cannot invoke "String.isEmpty()" because "str" is null

Example

If the given string values are empty, the concat() method returns an empty string.

In this program, we are creating the string literals with the empty value. Using the concat() method, we are concatenating the empty string values. Since the values are empty, this method returns an empty string.

import java.lang.*;
public class Concat {
   public static void main(String[] args) {
      try {
         
         //create the String literals
         String str1 = "";
         String str2 = "";
         System.out.println("The given string values are: " + str1 + ", " + str2);
         
         //using the concat() method
         System.out.println("After concatenation the new string is: " + str1.concat(str2));
      } catch(NullPointerException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

Output

After executing the program, it will return the following output −

The given string values are: ,
After concatenation the new string is: 
java_lang_string.htm
Advertisements