Please enable JavaScript to use CodeHS

Chapter 2

Using Objects

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?

Introducing Strings

A String is a sequence of characters. You use Strings to represent full words and sentences. For example, the famous "Hello World" is a String. Some more examples of Strings:

"I am a String. A sequence of characters strung together to form words and/or sentences."
"CodeHS is the best coding website ever! Everyone loves CodeHS!"
"She sells seashells by the seashore."
"Strings!"
"abcdefghijklmnopqrstuvwxyz"
Plain text

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

String strLiteral = "karel"; // String Literal
String strConstr = new String("karel"); // String constructor
Plain text

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.

Concatenating Strings

Concatenate is a fancy word for joining or linking two things together. To concatenate two or more Strings 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 Strings, "Mc" and "Donald's", to form one String, "McDonald's".

String one = "Mc";
String two = "Donald's";
String concatenate = one + two;
System.out.println(concatenate);
Plain text

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 Strings, you are actually overwriting the existing String with a new String.

Concatenating Strings with Primitive Data Types

You are not limited to concatenating strings with other strings. You can concatenate Strings with other data types. Here you are concatenating a string and an integer together:

int number = 24;
System.out.println("This String is concatenated with an integer at the end: " + number);
Plain text

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:

This String is concatenated with an integer at the end: 24
Plain text

You can also concatenate Strings 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.

Escape Sequences

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

// Print "Hello, Karel," said Tracy
System.out.println("\"Hello, Karel,\" said Tracy");
Plain text

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

Immutable Strings

String Concatenation

Rectangle Dimensions

Printing Equations

Check Your Understanding

  1. Incorrect Correct No Answer was selected Invalid Answer

    What would be printed by this code snippet?

    String subject = "Computer Science";
    String opinion = " is for everyone!";
    
    System.out.println(subject + opinion);
    Java

Exercise: Quote Machine

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

Enter a quote: 
Oh the places you'll go!
Enter the author of the quote: 
Dr. Seuss
"Oh the places you'll go!"
Dr. Seuss
Plain text

Test quotes taken from: https://www.brainyquote.com/quote_of_the_day