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


public class StringConcat {

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


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



hello world!!
Exception in thread "main" java.lang.NullPointerException
 at java.lang.String.concat(

© 2018

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0460 seconds.