A Technical Look At Strings
It may surprise you that the string literals that we have looked at in Unit 1 are actually objects too! String literals are part of the aptly named String class included in Java and have a set of methods that come along with it. We'll see what we can do with the String class in the next topic!
There are two ways to make strings: using a preinitialized string or by using a constructor. We have learned how to use preinitialized strings in Unit 1. As a reminder, you do this as follows:
String preInit = "Hello, I am a string!";
This will set preInit to be a reference to the string "Hello, I am a string!" so that when we call preInit, we are referencing this above string.
Meanwhile, we can use a constructor to make a string. Here is the constructor signature followed by a call to the constructor:
String newString = new String("This is a new string");
Calling the constructor makes a copy of the string in parentheses and sets it to the variable of interest. This makes it so when using the pre-initialized string, the variable and the pre-initialized string are the same string. On the other hand, the string in the constructor and the variable are copies and are two different strings.
Sometimes, you want to put a quote in a string. However, you quickly find out that the opening quotation in the quote marks the "end" of the string and you can't type out your quote unless you want the program to crash. What can you do? Luckily, you know about escape characters and you can type out your quote?
What are escape characters anyways? Escape characters are characters preceded by "\" that prints a certain character or whitespace (empty space, like a space or a tab) to the console or adds it to a string. Here is a table of some important escape characters:
Escape CharacterWhat It Prints
|\"||" (a quotation mark)|
|\\||\ (a backslash)|
|\n||A line break, so the remainder of the string is printed on a new line|
Sometimes, we also want to combine two strings together, and this is done with the help of string concatenation. String concatenation is done through the
+ operator and combines two strings together. Here is an example of string concatenation:
"3" + "3" = "33"
Did you think that the result of the concatenation would be 6 due to the two 3s? Remember that the quotations mean that 3 is a string, so the "+" represents string concatenation and not mathematical addition, so the strings are combined.
Here is another example. Do you think that the result of the string concatenation will equal "6", "33" again, or cause the program to crash?
3 + "3"
Once again, the result of this concatenation will be "33". Even though there is both a string and an integer, since there is a string, Java automatically knows that this is string concatenation and converts the integer to a string. In fact, if we are using string concatenation and one of the sides of the addition is any other type, it will be converted to a string.
For integers and doubles, the string is the textual representation of that number, while for booleans, the string will either be "true" or "false". If there is a mathematical expression on one side of the string concatenation, the expression is first evaluated before it is converted to a string. If you use an object when trying to concatenate a string, the object's toString() method is called, which usually converts the object's information to a String.
Moreover, we can also concatenate variables which is useful when you are doing user input. Let's try to concatenate my name!
String firstName = "Peter";
String lastName = "Cao"
System.out.println(firstName + lastName);
Oops, we accidentally printed out "PeterCao" without a space! This is because string concatenation combines the two strings as if they were just squished together. If you want to add a space, you would need to concatenate an actual space in addition to that as follows:
System.out.println(firstName + " " + lastName);