Java String concat() Method

The String.concat() method in Java is used to concatenate the specified string to the end of the current string.

Table of Contents

  1. Introduction
  2. concat Method Syntax
  3. Examples
    • Basic Usage
    • Concatenating Empty Strings
    • Handling Null Values
    • Real-World Use Case
  4. Conclusion

Introduction

The String.concat() method is a member of the String class in Java. It allows you to concatenate one string to the end of another, creating a new string that is the result of this operation. The original strings are not modified since strings in Java are immutable.

concat() Method Syntax

The syntax for the concat method is as follows:

public String concat(String str)
  • str: The string that is to be concatenated to the end of the current string.

Examples

Basic Usage

The concat method can be used to concatenate one string to the end of another.

Example

public class ConcatExample {
    public static void main(String[] args) {
        String str1 = "Hello";
        String str2 = " World";
        String result = str1.concat(str2);
        System.out.println("Concatenated string: " + result);
    }
}

Output:

Concatenated string: Hello World

Concatenating Empty Strings

Concatenating an empty string to another string has no effect.

Example

public class ConcatEmptyStringExample {
    public static void main(String[] args) {
        String str1 = "Hello";
        String str2 = "";
        String result = str1.concat(str2);
        System.out.println("Result after concatenating empty string: " + result);
    }
}

Output:

Result after concatenating empty string: Hello

Handling Null Values

The concat method throws NullPointerException if the specified string is null.

Example

public class ConcatNullExample {
    public static void main(String[] args) {
        String str1 = "Hello";
        String str2 = null;

        try {
            String result = str1.concat(str2);
            System.out.println("Result after concatenating null string: " + result);
        } catch (NullPointerException e) {
            System.out.println("Error: Cannot concatenate null string.");
        }
    }
}

Output:

Error: Cannot concatenate null string.

Real-World Use Case

Example: Building a URL

One common use case for concat is building URLs dynamically from different parts.

public class BuildUrlExample {
    public static void main(String[] args) {
        String baseUrl = "https://www.example.com/";
        String endpoint = "api/v1/users";
        String parameters = "?id=123&name=John";

        String fullUrl = baseUrl.concat(endpoint).concat(parameters);
        System.out.println("Full URL: " + fullUrl);
    }
}

Output:

Full URL: https://www.example.com/api/v1/users?id=123&name=John

In this example, the concat method is used to build a complete URL from its components.

Conclusion

The String.concat() method in Java is a straightforward and efficient way to concatenate strings. It is particularly useful for dynamically building strings from different parts, such as constructing URLs or creating messages. However, always be cautious with null values to avoid NullPointerException. By understanding and utilizing the concat method, you can effectively manage string concatenation in your Java applications.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top