Java String.concat() Method24 Mar 2025 | 3 min read SignatureThe signature of the string concat() method is given below: ParameteranotherString : another string i.e., to be combined at the end of this string. Returnscombined string String ConcatenationThe Java String class concat() method combines specified string at the end of string. It returns a combined string. It is just like appending another string. In this section, we delve into the intricacies of Java String Concatenation, exploring its nuances, best practices, and performance considerations. In this example, the + operator is used to concatenate the strings firstName, a space, and lastName, resulting in the fullName string. Alternatively, the same concatenation can be achieved using the concat() method: Both approaches yield the same result, but they differ in syntax and underlying implementation. Internal implementationJava String.concat() Method Example 1ExampleCompile and RunOutput: java string java string is immutable so assign it explicitly Java String.concat() Method Example 2Let's see an example where we are concatenating multiple string objects. ExampleCompile and RunOutput: HelloTpointTech HelloTpointTechReader Java String.concat() Method Example 3Let's see an example where we are concatenating spaces and special chars to the string object. It is done using the chaining of the concat() method. ExampleCompile and RunOutput: Hello TpointTech Reader Hello TpointTech Reader Hello!!! Hello@TpointTech Java String.concat() Method Example 4Till now, we have seen that the concat() method appends the string at the end of the string that invokes the method. However, we can do a bit of workaround to append the string at the beginning of a string using the concat() method. ExampleCompile and RunOutput: India is my Country Best PracticesWhen working with string concatenation in Java, it is essential to follow best practices to ensure code readability, maintainability, and performance:
Java String Concatenation is a fundamental operation in Java programming, essential for manipulating textual data. By understanding the different concatenation techniques, performance considerations, and best practices, developers can write efficient, maintainable, and robust Java code. Whether it's simple string concatenation or dynamic string building. Next TopicJava String contains() |
» « Java String.indexOf() The Java String class indexOf() method returns the position of the first occurrence of the specified character or string in a specified string. Signature There are four overloaded indexOf() method in Java. The signature of indexOf() methods are given below: No. Method Description 1 int indexOf(int ch) It returns the index position...
5 min read
» « Java The Java String class startsWith() method checks if this string starts with the given prefix. It returns true if this string starts with the given prefix; else returns false. Signature The syntax or signature of startWith() method is given below. public boolean startsWith(String prefix) public boolean startsWith(String prefix,...
3 min read
Java The Java String class lastIndexOf() method returns the last index of the given character value or substring. If it is not found, it returns -1. The index counter starts from zero. Signature There are four types of lastIndexOf() method in Java. The signature of the methods...
3 min read
Java The java string toLowerCase() method returns the string in lowercase letter. In other words, it converts all characters of the string into lower case letter. The toLowerCase() method works same as toLowerCase(Locale.getDefault()) method. It internally uses the default locale. Internal implementation public String toLowerCase(Locale locale) { ...
8 min read
Java The java string toUpperCase() method returns the string in uppercase letter. In other words, it converts all characters of the string into upper case letter. The toUpperCase() method works same as toUpperCase(Locale.getDefault()) method. It internally uses the default locale. Internal implementation public String toUpperCase(Locale locale)...
7 min read
» « Java In Java programming, string manipulation is an integral part of almost any application. Whether it's parsing data, formatting output, or manipulating user input, the ability to work effectively with strings is essential. Among the many methods available for string manipulation, the replace() method stands...
6 min read
» « Java The Java String class format() method returns the formatted string by given locale, format and arguments. If we do not specify the locale in String.format() method, it uses the default locale by calling Locale.getDefault() method. The format() method of the Java Sting class is the same...
8 min read
Java String.trim() Method The Java String class trim() method eliminates leading and trailing spaces. The Unicode value of space character is '\u0020'. The trim() method in Java string checks this Unicode value before and after the string, if it exists then the method removes the spaces...
6 min read
Java The Java String class getBytes() method does the encoding of string into the sequence of bytes and keeps it in an array of bytes. Signature There are three variants of getBytes() method. The signature or syntax of string getBytes() method is given below: public byte[] getBytes() public byte[]...
3 min read
Java The compareTo() method in Java's String class compares two strings lexicographically. It compares strings on the basis of the Unicode value of each character in the strings. If the first string is lexicographically greater than the second string, it returns a positive number (difference of character...
5 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India