String Concatenation using Java

By: Syed Fazal Emailed: 1766 times Printed: 2513 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

In general, Java does not allow operators to be applied to String objects. The one exception to this rule is the + operator, which concatenates two strings, producing a String object as the result. This allows you to chain together a series of + operations. For example, the following fragment concatenates three strings:

String age = "9";
String s = "He is " + age + " years old.";

This displays the string "He is 9 years old."

One practical use of string concatenation is found when you are creating very long strings. Instead of letting long strings wrap around within your source code, you can break them into smaller pieces, using the + to concatenate them. 

Here is an example:
// Using concatenation to prevent long lines.
class ConCat {
public static void main(String args[]) {

String longStr = "This could have been " +
"a very long line that would have " +
"wrapped around. But string concatenation " +
"prevents this.";


String Concatenation with other Data Types

You can concatenate strings with other types of data. For example, consider this slightly different version of the earlier example:

int age = 9;
String s = "He is " + age + " years old.";

In this case, age is an int rather than another String, but the output produced is the same as before. This is because the int value in age is automatically converted into its string representation within a String object. This string is then concatenated as before. The compiler will convert an operand to its string equivalent whenever the other operand
of the + is an instance of String. Be careful when you mix other types of operations with string concatenation expressions, however. You might get surprising results. Consider the following:

String s = "four: " + 2 + 2;

This fragment displays
four: 22

rather than the

four: 4

that you probably expected. Here's why. Operator precedence causes the concatenation of "four" with the string equivalent of 2 to take place first. This result is then concatenated with the string equivalent of 2 a second time. To complete the integer addition first, you must use parentheses, like this:

String s = "four: " + (2 + 2);
Now s contains the string "four: 4".

Java Home | All Java Tutorials | Latest Java Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.

Bookmark and Share


1. View Comment

but how is the string concatenation done with the '+' as java does not support operator overloading.

View Tutorial          By: Vipin at 2009-05-09 03:08:51
2. View Comment

string is immutable in java.String buffer is mutable.
so even though we add the symbol '+' for concatenation.
the string which supplied for concatenation is taken as a object of string buffer.
after the string buffer completes the necessary concatenation then its return the string by toString()

View Tutorial          By: krishnakumar at 2009-10-19 00:35:44
3. View Comment

@Vipin : vipin u r wrong,,,in reality java does not support explicitely perator verloading,,,,that is the rule,
java itself implicitely has done it,,,so we r not in the need of doing it explicitely,,,,java help us here,,,

View Tutorial          By: Arpit Tyagi at 2010-07-23 04:11:20
4. View Comment if the input is THE DOG BARKS then the output will be "THE" "DOG" "BARKS" and it canbe rumbled..who can give me an example of that??
plz lang poh..
i needie now

View Tutorial          By: dharel at 2011-01-28 02:53:19
5. View Comment

guys you both are correct
but,java is implicit as wel as explicit
so,we can use '+' operator their to concat more than two strings;
if we want to concat two simply we can use concat operation"Ex:str1.concat(str2) if str1 and str2 are two strings"
finally, using '+' operator only we can concat three or more strings.......... is iam right kada.
if any other way is their plz mention

View Tutorial          By: vijay at 2011-09-08 12:12:48
6. View Comment

what about strings in J2ME ?

View Tutorial          By: Swapnil at 2012-07-20 09:19:15
7. View Comment

thaks sir for giving information about adding int + string

View Tutorial          By: vijay kumar at 2012-10-10 04:59:05
8. View Comment

how to pass user input string (like a user entered: user_query="USA"). this user_query will b sent to google page for search.
my Question is how i can concatenate user_query to a web page can any body help me????

View Tutorial          By: saif at 2012-12-04 19:51:26

Your name (required):

Your email(required, will not be shown to the public):

Your sites URL (optional):

Your comments:

More Tutorials by Syed Fazal
switch in Javascript
break and continue in Javascript
do-while, while, for and for-in loops in Javascript
The if statement in Javascript
Using && in Javascript
Using ! and !! in Javascript
Bitwise NOT, AND, OR, XOR operators in Javascript
String Concatenation using Java

More Tutorials in Java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Java File
Java String
Count number of vowels, consonants and digits in a String in Java
Reverse a number in Java
Student marks calculation program in Java
Handling Fractions in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java
Multiple Inheritance sample in Java
Java program using Method Overriding
Java program to check if user input is an even number

More Latest News
Most Viewed Articles (in Java )
Properties example in Java
Count number of vowels, consonants and digits in a String in Java
Getting current time in Java for inserting to MySQL
FileReader and FileWriter example program in Java
Stack example in Java - push(), pop(), empty(), search()
Vector example in Java
Method Overloading (function overloading) in Java
right shift operator, >>, in Java
String Concatenation using Java
Sample program to demonstrate the use of ActionListener
The Benefits of OOP
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Java File
Java String
Most Emailed Articles (in Java)
Converting a number into its equalant value in words in Java
Disadvantages of using Native methods in Java
What is UCS? What is ISO 10646?
java.lang.reflect package
Operator Precedence in Java
PushbackReader sample program in Java
concat(), replace(), and trim() Strings in Java
Use of - new - in Java
The Benefits of OOP
Increment and Decrement Operator
The continuing Revolution of java
Why java is important to the Internet
Multiple Inheritance sample in Java
HTTPS Java - URL Connection program. Using HttpsURLConnection
Using the AWS SDK for Java