π2.6: The String Class
Table of Contents
π This page is a condensed version of CSAwesome Topic 2.6
π Take notes in a Codespace during class, coding along with the instructor.
- Go to GitHub and click on your picture in the TOP RIGHT corner
- Select
Your repositories
- Open
CS2-Unit-2-Notes
- Now on your repository, click and select the
Codespaces
tab - Click
Create Codespace on main
(unless you already have one listed there), wait for the environment to load, then youβre ready to code!
The String Class
Strings in Java are objects of the String
class that hold sequences of characters (a
, B
, c
, $
, etc). You can declare a variable to be of the type String
.
Remember that a class (or classification) in Java defines the data that all objects of the class have (the instance variables) and the behaviors, the things that objects know how to do (the methods).
Class names in Java, like String
, begin with a CAPITAL letter. All primitive types: int
, double
, and boolean
, begin with a lowercase letter. This is one easy way to tell the difference between primitive types and class types.
In Java there are two ways to create an object of the String
class. You can use the new
keyword followed by a space and then the class constructor and then in parentheses you can include values used to initialize the fields of the object.
String greeting = new String("Hello World");
This is the standard way to create a
new
object of any class in Java.
In Java you can also use just a string literal, which is a set of characters enclosed in double quotes (" "
), to create a String
object:
String greeting = "Hello";
In both cases an object of the String
class will be created in memory, and the value of the variable greeting
will be set to an object reference (π a way to find that object).
String Concatenation
String
s can be added to each other to create a new string using the +
or +=
operator . This is also called appending or concatenating. You can also add any other kind of value to a String
with +
or +=
and the other value will be converted to a String
automatically. Objects are converted by calling their toString
method which weβll talk about in section 5.4.
Remember, however, that String
s are immutable, just like int
s and double
s. So when we add two String
s (or a String
and another value converted to a String
) we get a new String
without making any change to the values being added together just like when we add 1 + 2
the original 1
and 2
arenβt changed. When we use +=
we are making a new String
by adding something to the current value of a variable and then assigning that new value back into the variable, again just like with numbers.
Note that spaces are not added between strings automatically. If you want a space between two strings then add one using + β β +. If you forget to add spaces, you will get smushed output like βHiJoseβ instead of βHi Joseβ. And remember that variables are never put inside the quotes (ββ) since this would print the variable name out letter by letter instead of its value.
You can even add other items to a string using the +
operator. Primitive values like int
and boolean
will be converted to a String
like what you would type into a Java program and objects will be converted to String
using the toString
method discussed in the previous section.
π¬ Discuss: What do you think the following code will print out?
String message = "12" + 4 + 3;
System.out.println(message);
If you are appending a number to a string it will be converted to a string first before being appended.
Since the same operators are processed from left to right this will print 1243
. First 4 will be turned into a string and appended to 12 and then 3 will be turned into a string and appended to 124. If you want to do addition instead, try using parentheses!
What if you wanted to print out a double quote β character? Since the double quote β is a special character with meaning in Java, we put in a backslash in front of the quote to signal that we want just the character. This is called a backslash escape sequence. And if you wanted to print out a backslash, you would have to backslash it too in order to escape its special meaning. Another useful backslashed character is backslash \n which will put in a newline.
βοΈ Summary
-
Strings in Java are objects of the
String
class that hold sequences of characters. -
String objects can be created by using string literals (String s = βhiβ;) or by calling the String class constructor (String t = new String(βbyeβ);).
- new is used to create a new object of a class.
-
null is used to indicate that an object reference doesnβt refer to any object yet.
-
String objects can be concatenated using the
+
or+=
operator, resulting in a new String object. -
Primitive values can be concatenated with a String object. This causes implicit conversion of the values to String objects.
- Escape sequences start with a backslash
\
and have special meaning in Java. Escape sequences used in this course include\"
,\\
, and\n
to print out a quote, backslash, and a new line.
π When class ends, donβt forget to SAVE YOUR WORK!
- Navigate to the
Source Control
menu on the LEFT sidebar - Click the button on the LEFT menu
- Type a brief commit message at the top of the file that opens, for example:
updated Main.java
- Click the small
βοΈ
checkmark in the TOP RIGHT corner - Click the button on the LEFT menu
- Finally you can close your Codespace!
Acknowledgement
Content on this page is adapted from Runestone Academy - Barb Ericson, Beryl Hoffman, Peter Seibel.