A string concatenation operator can be used to string two or more strings. The string concatenation operator uses the addition sign (+). If both are strings, then both strings are directly strung together. If one of them is not a string, then the non-string will first be converted into a string and then joined with the other string. In string concatenation operators, conversion to string form can be done automatically by Java.
Consider the example of using string concatenation operators in the following println method argument.
System.out.println ("Programming using the Java language,
by Hindro H. ");
The above string goes beyond the line and then puts the remaining string into the next new line by giving a comma (,). This kind of writing syntax is wrong so that when compiled an error message will be displayed. Writing a string should not go beyond the line and continue to the next line. To correct the above statement, you can divide a long string into two or more substrings and then arrange the substrings using the string concatenation operator as below.
System.out.println ("Programming using the Java language" +
"By Hindro H.");
Following is an example of a simple program that displays the area of a rectangle in the Command Prompt window. As you already know, the area of a rectangle can be calculated by multiplying the long side and the wide side of the rectangle. This example program also demonstrates the use of string (+) concatenation operators as explained above.
Consider the example of using string concatenation operators in the following println method argument.
System.out.println ("Programming using the Java language,
by Hindro H. ");
The above string goes beyond the line and then puts the remaining string into the next new line by giving a comma (,). This kind of writing syntax is wrong so that when compiled an error message will be displayed. Writing a string should not go beyond the line and continue to the next line. To correct the above statement, you can divide a long string into two or more substrings and then arrange the substrings using the string concatenation operator as below.
System.out.println ("Programming using the Java language" +
"By Hindro H.");
Following is an example of a simple program that displays the area of a rectangle in the Command Prompt window. As you already know, the area of a rectangle can be calculated by multiplying the long side and the wide side of the rectangle. This example program also demonstrates the use of string (+) concatenation operators as explained above.
// Nama file : LuasPersegiPanjang.java
// Menampilkan luas persegi panjang di command prompt
public
class
LuasPersegiPanjang {
public
static
void
main(String[] args) {
// Deklarasi variabel panjang, lebar dan luas
long
panjang, lebar, luas;
// Memberi nilai pada variabel panjang dan lebar
panjang =
6
;
lebar =
4
;
// Menghitung luas
luas = panjang * lebar;
// Menampilkan panjang, lebar dan luas
// di command prompt
System.out.println(
"Panjang = "
+ panjang);
System.out.println(
"Lebar = "
+ lebar);
System.out.println(
"Luas Persegi Panjang = "
+ luas);
}
}