2.6 String Objects: Concatenation, Literals & More
As you have learned, a character is one of the primitive data types in Java. You use the char
keyword to declare it. A character is a single letter. 'A'
, 'a'
, 'D'
, and 'Z'
are all examples of characters. It is great that you have an easy way to represent a single character, but what if you wanted to represent words or sentences? How can you do this?
A String is a sequence of characters. You use String
s to represent full words and sentences. For example, the famous "Hello World"
is a String
. Some more examples of String
s:
A String
is not a primitive type like int
, char
, boolean
, and double
. Primitive types always start with lowercase letters, but a String
starts with a capital letter. This makes it an object.
You can create String
s using a string literal or calling the String
constructor. A string literal is a string inside of quotes, as you saw above. The String
constructor takes a string literal as an input.
Both options will create a String
variable. The constructor method will force Java to create a new String
value in the heap, while the String
literal may allow Java to reuse an existing value in the heap.
Concatenate is a fancy word for joining or linking two things together. To concatenate two or more String
s means that you are joining them together to form one String
. You can do this in Java by using the addition operator, +
, or +=
.
In this example, you concatenate two separate String
s, "Mc"
and "Donald's"
, to form one String
, "McDonald's"
.
After running the program, the concatenated String
"McDonald's"
gets printed to the screen.
It is important to note that String
objects are immutable, meaning that strings cannot be changed by methods or concatenation. Instead, when you run methods or concatenate values with String
s, you are actually overwriting the existing String
with a new String
.
You are not limited to concatenating strings with other strings. You can concatenate String
s with other data types. Here you are concatenating a string and an integer together:
Look familiar? You have been doing this all the time in previous programs when printing out results to the screen! After running the program, you get:
You can also concatenate String
s with other primitive types like char
, boolean
, and double
. When you concatenate a String
with a primitive variable, there is an implicit conversion of the primitive variable to a String
to perform the conversion.
In Java, you create string literals using quotation marks, but what if you want to print a string with quotes, for example "Hello, Karel," said Tracy.
Java uses the concept of an escape sequence to essentially send a signal to the compiler to treat a character a little differently. In Java, the escape character is a backslash, \
. When Java sees a backslash inside of quotation marks, it knows that it is a signal to treat the next character differently within the literal string. For example, if you want to include quotation marks in your output, you would use a \"
.
Here are some important escape sequences in Java:
Escape Sequence | Function | Output |
---|---|---|
\" |
"\"Allows for quotations\"" |
"Allows for quotations" |
\\ |
"Includes a backslash\\" |
Includes a backslash\ |
\n |
""This creates \na line break" |
This creates a line break |
\t |
"This adds a \ttab space" |
This adds a tab space |
-
Incorrect
Correct
No Answer was selected
Invalid Answer
What would be printed by this code snippet?
Write a program that asks the user for a quote and the author of the quote.
Print the quote in quotation marks followed by the author on the next line. Use the escape character “\n” to create the newline rather than using separate print statements.
Sample Output
Test quotes taken from: https://www.brainyquote.com/quote_of_the_day