Java String Concatenation Example using concat()
This Snippet is coded by a user inOnline java Compiler. You can see and run this code too.
زبان: Java
This Java program demonstrates how to concatenate two strings using the concat() method of the String class.
The code declares two String variables: firstName with value "John " and lastName with value "Doe". Then, the concat() method is called on firstName with lastName as an argument, producing a new String "John Doe". The result is printed to the console using System.out.println.
Key Points:
- The
concat()method appends the specified string to the end of the invoking string. - It returns a new string; the original strings are not modified.
- Alternatively, the
+operator can be used for string concatenation.