Movatterモバイル変換


[0]ホーム

URL:


Open In App

The string concat() methodconcatenates (appends) a string to the end of another string. It returns the combined string. It is used for string concatenation in Java. It returns NullPointerException if any one of the strings is Null.

Java
classGFG{publicstaticvoidmain(Stringargs[]){// String InitializationStrings="Geeks";// Use concat() method for string concatenations=s.concat("forGeeks");System.out.println(s);}}

Output
GeeksforGeeks

Syntax of concat() Method

public Stringconcat(String s);

Parameters:

  • A string to be concatenated at the end of the other string.

Return Value:

  • Concatenated(combined) string.

Exception:

  • NullPointerException:When either of the string is Null.

Java String concat() Examples

There are many ways to use the concat() method in Java:

Combining Two Strings with concat() Method

The below example combines two strings using concat() method of string class.

Example:

Java
classGFG{publicstaticvoidmain(Stringargs[]){// String InitializationStrings1="Geeksfor";Strings2="Geeks";// Concatenate the strings s1 and s2 using the concat() method and store the result back in s1.s1=s1.concat(s2);System.out.println(s1);}}

Output
GeeksforGeeks

Sequential Concatenation of Multiple Strings

The below example shows sequential concatenation using String concat() method in Java.

Example:

java
publicclassGFG{publicstaticvoidmain(Stringargs[]){Strings1="Computer-";Strings2="Science-";// Combining above strings by passing one string as an argumentStrings3=s1.concat(s2);// Print and display temporary combined stringSystem.out.println(s3);Strings4="Portal";Strings5=s3.concat(s4);System.out.println(s5);}}

Output
Computer-Science-Computer-Science-Portal

Note: As perceived from the code we can do as many times as we want to concatenate strings bypassing older strings with new strings to be contaminated as a parameter and storing the resultant string in String datatype.

Handling NullPointerException in String concat()

The below example shows the NullPointerException in String concat() method.

Example:

Java
publicclassGFG{publicstaticvoidmain(Stringargs[]){Strings1="Computer-";Strings2=null;// Combining above strings by passing one string as an argumentStrings3=s1.concat(s2);// It will raise NullPointerExceptionSystem.out.println(s3);}}

Output

Exception in thread "main" java.lang.NullPointerException

Reversing a String Using concat() Method

We can reverse a string using the concat() method of string class. Below is the example to reverse a string in Java.

Example:

Java
publicclassReverseString{publicstaticvoidmain(String[]args){// Declare original string variableStringa="Geeks";// Declare another string variable and initialize it with an empty stringStringb="";// Iterate through each character in string "a" from the last index to the first.for(inti=a.length()-1;i>=0;i--){// Extract the current character at index "i" of the "a" stringcharch=a.charAt(i);// Convert the character to a String object using the "Character.toString" methodStringch1=Character.toString(ch);// Concatenate the converted character String to the end of the "b" stringb=b.concat(ch1);}System.out.println(""+a);System.out.println(""+b);}}

Output
GeeksskeeG

Java Program to Concatenate Two Strings
Improve
Improve

Explore

Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp