slanted W3C logo

Day 17 — Strings I

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.

String Literals

A string literal consists of zero or more characters enclosed in double quotes; for example:

"hello"
"416-736-2100"
"computer science and engineering"

Weirdness Part I

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

Weirdness Part II

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.

Memory Diagram I

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"

Memory Diagram II

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"

Weirdness + Memory Diagram III

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.

Weirdness Part IV

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;
  1. name1 is a reference to a String literal
  2. name2 is a reference to a String computed using an expression using only literals and +
    • such an expression is called a constant expression
    • a constant expression is treated as though it were a literal
    • name1 == name2 is true
  3. name3 is a reference to a String computed using an expression using variables, literals, and +
    • a new String object is created for all such expressions
    • name1 == name3 is false

Weirdness Part V

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

Weirdness Part V Explained

      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:

  1. '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
  2. 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";

Weirdness Part VI

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

Weirdness VII

What does the following print?

      String s1 = "Julie Payette";
      String s2 = "Julie " + "Payette";
      System.out.println("same object: " + s1 == s2);

To Do For Next Lecture

Continue reading Chapter 6.