Java String.concat() Method

 Posted On  | Yashwant Chavan 

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.

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)


© technicalkeeda.com 2017

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0328 seconds.