In today's lecture we start to look at the String
class.
Strings represent text (a sequence of characters)
and are widely used in Java programs. Because they are so widely
used, the Java language lets a client perform actions with
String
objects that cannot be performed with other types
of objects.
A string literal consists of zero or more characters enclosed in double quotes; for example:
"hello" "416-736-2100" "computer science and engineering"
A string literal is actually a String
object. You might find
this surprising because an object is created without using a constructor
or calling a method.
import java.io.PrintStream; public class StringLiteral { public static void main(String[] args) { PrintStream output = System.out; // length of a string output.print("number of characters : "); output.println("hello".length()); // get the character in position 4 // counts from 0 output.print("fourth character : "); output.println("hello".charAt(4)); // same state? output.print("same state : "); output.println("hello".equals("hello")); } }
number of characters : 5 fourth character : o same state : true
A string literal is a reference to a String
object; everytime you use a particular literal, for example
"hello"
, you are referring to the same object.
The code snippet:
// same object? output.print("hello" == "hello"); output.print(" "); // same object? String s = "hello"; output.print(s == "hello");
will print true true
.
Consider the following program:
public class StringMemoryDiagram1 { public static void main(String[] args) { char first = "York University".charAt(0); } }
Its memory diagram looks like:
main | ||
first ⇒ | 'Y' | |
¦ | ||
String class | ||
¦ | ||
800 | String object | |
??? ⇒ | "York University" |
Consider the following program:
public class StringMemoryDiagram2 { public static void main(String[] args) { char first = "York University".charAt(0); String york = "York University"; } }
Its memory diagram looks like:
main | ||
first ⇒ | 'Y' | |
york ⇒ | 800 | |
¦ | ||
String class | ||
¦ | ||
800 | String object | |
??? ⇒ | "York University" |
A String
object can also be created using
new
; new
always creates a new
object.
public class StringMemoryDiagram3 { public static void main(String[] args) { char first = "York University".charAt(0); String york = "York University"; String s = new String("York University"); String t = new String(york); } }
Its memory diagram looks like:
main | ||
first ⇒ | 'Y' | |
york ⇒ | 800 | |
s ⇒ | 900 | |
t ⇒ | 1000 | |
¦ | ||
String class | ||
¦ | ||
800 | String object | |
??? ⇒ | "York University" | |
¦ | ||
900 | String object | |
??? ⇒ | "York University" | |
¦ | ||
1000 | String object | |
??? ⇒ | "York University" |
The objects referred to by
s
and t
are wasteful; we already have a
variable named york
that has the desired state. You will
see in the next lecture that a client cannot change the state of a
String
object.
You can join two strings to create a new string using the
concatenation operator +
:
String name1 = "James Bond 007"; String name2 = "James" + " " + "Bond" + " " + "00" + 7; String firstName = "James"; String lastName = "Bond"; String number = "007"; String name3 = firstName + " " + lastName + " " + number;
name1
is a reference to a String
literal
name2
is a reference to a String
computed
using an expression using only literals and +
name1 == name2
is true
name3
is a reference to a String
computed
using an expression using variables, literals, and +
String
object is created for all such
expressionsname1 == name3
is false
Concatenation and addition both use +
as the
operator. This can lead to errors if you forget that Java
evaluates expressions involving operators of the same
precedence from left to right.
String laugh = 'h' + 'a' + "ha"; System.out.println(laugh);
The above snippet prints:
201ha
String laugh = 'h' + 'a' + "ha"; System.out.println(laugh);
The expression 'h' + 'a' + "ha"
involves operators
all having the same precedence, so it is evaluated from left
to right:
'h' + 'a'
is the sum of two char
literals; addition is not defined for char
so both
operands are promoted to int
and then summed
to yield 201
201 + "ha"
involves a String
object so string concatenation is performed to yield
"201ha"
You can fix the problem by concatenating an empty string
""
at the beginning of the expression:
String laugh = "" + 'h' + 'a' + "ha";
You can also concatenate a string with an object reference. The
object reference is converted to a String
by calling
its toString
method.
Fraction f = new Fraction(5, 8); String s = "The fraction is " + f; System.out.println(s);
The above code snippet prints:
The fraction is 5/8
What does the following print?
String s1 = "Julie Payette"; String s2 = "Julie " + "Payette"; System.out.println("same object: " + s1 == s2);
Continue reading Chapter 6.