Pasting Strings Together

When you use System.out.println() and work with strings in other ways, you can paste two strings together by using +, the same operator that is used to add numbers.


Note

You’ll probably see the term concatenation in other books as you build your programming skills, so it’s worth knowing. However, pasting is the term used here when one string and another string are joined together. Pasting sounds like fun. Concatenating sounds like something that should never be done in the presence of an open flame.


The + operator has a different meaning in relation to strings. Instead of performing some math, it pastes two strings together. This action can cause strings to be displayed together or make one big string out of two smaller ones.

Concatenation is the word used to describe this action because it means to link two things together.

The following statement uses the + operator to display a long string:

System.out.println(""'The Piano' is as peculiar and haunting as any" +
    " film I've seen." — Roger Ebert, Chicago Sun-Times");

Instead of putting this entire string on a single line, which would make it harder to understand when you look at the program later, the + operator is used to break the text over two lines of the program’s Java source code. When this statement is displayed, it appears as the following:

"'The Piano' is as peculiar and haunting as any film I've seen."
    — Roger Ebert, Chicago Sun-Times

Several special characters are used in the string: , , , and . To better familiarize yourself with these characters, compare the output with the System.out.println() statement that produced it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.192.141