String Concatenation using Java

By: Syed Fazal  


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.";
System.out.println(s);

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.";
System.out.println(longStr);

        }
}

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.";
System.out.println(s);

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;
System.out.println(s);

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".




Archived Comments

1. zwdbawlcb
View Tutorial          By: hvrufohfa at 2013-12-10 12:15:56

2. you can find some more details in the below link,"http://javadomain.in/string-concatenation-in-
View Tutorial          By: Diva at 2013-09-19 14:53:02

3. how to pass user input string (like a user entered: user_query="USA"). this user_query wil
View Tutorial          By: saif at 2012-12-04 19:51:26

4. thaks sir for giving information about adding int + string
View Tutorial          By: vijay kumar at 2012-10-10 04:59:05

5. what about strings in J2ME ?
View Tutorial          By: Swapnil at 2012-07-20 09:19:15

6. guys you both are correct
but,java is implicit as wel as explicit
so,we can use '+' o

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

7. ..how if the input is THE DOG BARKS then the output will be "THE" "DOG" "BA
View Tutorial          By: dharel at 2011-01-28 02:53:19

8. ..how if the input is THE DOG BARKS then the output will be "THE" "DOG" "BA
View Tutorial          By: dharel at 2011-01-28 02:49:23

9. @Vipin : vipin u r wrong,,,in reality java does not support explicitely perator verloading,,,,that
View Tutorial          By: Arpit Tyagi at 2010-07-23 04:11:20

10. string is immutable in java.String buffer is mutable.
so even though we add the symbol '+' fo

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

11. 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


Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial