Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
HowToDoInJava
  • Java
  • Spring AI
  • Spring Boot
  • Hibernate
  • JUnit 5
  • Interview

Java String concat()

Java String.concat() concatenates the argument string to the end of the current string and returns the combined string.

Lokesh Gupta

April 16, 2024

Java String class
Java String
Java String

The Java String.concat() concatenates the specified string to the end of the current string.

Internally, Java creates a new character array with a combined length of the current and argument string. Then it copies all content from both strings into this new array. Finally, the combined character array is converted to a new string and returned.

1. String.concat() API

The concat() API concatenates the specified string to the end of this string.

public String concat(String str);

2. String.concat() Example

The following Java program concatenates two strings to produce a combined string.

String str = "Hello";

Assertions.assertEquals("Hello World", str.concat(" World"));

3. Null and Empty Strings

Note that the method will return the original string if we can pass an empty string as the argument string.

Assertions.assertEquals("Hello", str.concat(""));

The null is not an accepted input and causes NullPointerException.

Assertions.assertThrows(NullPointerException.class, ()->{
    str.concat(null);
});

Happy Learning !!

References: Java String Doc

Source Code on Github

Comments

Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

String Examples

  • String Constant Pool
  • Convert String to int
  • Convert int to String
  • Convert String to long
  • Convert long to String
  • Convert CSV String to List
  • Java StackTrace to String
  • Convert float to String
  • Align Left, Right, Center
  • Immutable Strings
  • StringJoiner
  • Split a string
  • Escape HTML
  • Unescape HTML
  • Convert to title case
  • Find duplicate words
  • Left pad a string
  • Right pad a string
  • Reverse recursively
  • Leading whitespaces
  • Remove whitespaces
  • Reverse words
  • Find duplicate characters
  • Get first 4 characters
  • Get last 4 characters
  • (123) 456-6789 Pattern
  • Interview Questions

String Methods

  • String concat()
  • String hashCode()
  • String contains()
  • String compareTo()
  • String compareToIgnoreCase()
  • String equals()
  • String equalsIgnoreCase()
  • String charAt()
  • String indexOf()
  • String lastIndexOf()
  • String intern()
  • String split()
  • String replace()
  • String replaceFirst()
  • String replaceAll()
  • String substring()
  • String startsWith()
  • String endsWith()
  • String toUpperCase()
  • String toLowerCase()
Photo of author

Lokesh Gupta

A fun-loving family man, passionate about computers and problem-solving, with over 15 years of experience in Java and related technologies. An avid Sci-Fi movie enthusiast and a fan of Christopher Nolan and Quentin Tarantino.
Follow on Twitter Portfolio

Previous

Java String substring()

Next

Java String replace()

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Tutorial Series

OOP

Regex

Maven

Logging

TypeScript

Python

Meta Links

About Us

Advertise

Contact Us

Privacy Policy

Our Blogs

REST API Tutorial

Follow On:

  • Github
  • LinkedIn
  • Twitter
  • Facebook
Copyright © 2026 | Sitemap