String Concatenation in Java
Posted by
Ravi Kumar at Sunday, March 15, 2009
Share this post:
|
0 Comments
Strings are sequences of characters, such as "hello".
String a = ""; // an empty string
String greeting = "Hello";
Java allows you to use the + sign to join (concatenate) two strings together.
String a = "hello";
String b = "hai";
String message = a + b;
The above code makes the value of the string variable message "hellohai".
When you concatenate a string with a value that is not a string, the latter is converted to a string.
int age = 25;
String a = "hi" + age;
sets variable a to the string "hi25".