Java String.concat() Method

Java String.concat() Method

By Yashwant Chavan, Views 1791, Date 16-Nov-2016

In this tutorial you will learn how to concatenate strings using String .concat() method in Java. Each time it will creates new String in String constant pool.

tags java

Refer below steps

  • String.concat(String str) method concatenates the specified string to the end of this string.
  • Parameters: str the String which you want to concatenated to the end of this String.
  • It throws NullPointerException if you pass null value to concat() method. Refer second scenario

Java String.concat() method example

package com.technicalkeeda.app;

public class StringConcat {

    public static void main(String args[]) {
        String str = "hello";
        str = str.concat(" world!!");

        System.out.println(str);

        String hello = "hello";
        hello = hello.concat(null);

        System.out.println(hello);
    }
}

Output

hello world!!
Exception in thread "main" java.lang.NullPointerException
 at java.lang.String.concat(String.java:2027)
 at com.technicalkeeda.app.StringConcat.main(StringConcat.java:12)

Suggested Posts

Yashwant

Yashwant

Hi there! I am founder of technicalkeeda.com and programming enthusiast. My skills includes Java,J2EE, Spring Framework, Nodejs, PHP and lot more. If you have any idea that you would want me to develop? Lets connect: yashwantchavan[at][gmail.com]