Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java concat() Method

Java concat() Method

Java String ClassJava String class


In Java, the concat() method is used to concatenate two strings.

This method belongs to the String class and can be used to append one string to the end of another.

Note that the concat() method does not modify the original string, but returns a new string.

Syntax


public String concat(String s)

Parameters

Return Value

Returns the new concatenated string.

Example

Example

public

class

Test

{

       

public

static

void

main

(

String

args

[

]

)

{

               

String

s

=

"Runoob Tutorial:"

;

                s

=

s.

concat

(

"www.runoob.com"

)

;

               

System

.

out

.

println

(

s

)

;

       

}

}

The execution result of the above program is:


Runoob Tutorial:www.runoob.com

Notes

Example

public

class

ConcatExample

{

   

public

static

void

main

(

String

[

]

args

)

{

       

String

str1

=

"Hello"

;

       

       

// Concatenate an empty string

       

String

result

=

str1.

concat

(

""

)

;

       

       

// Print the result

       

System

.

out

.

println

(

result

)

;

// Output "Hello"

   

}

}

In summary, the concat() method is a simple and effective way to concatenate strings, but you should avoid passing null to prevent exceptions.


Java String ClassJava String class