- Forward


Strings in Java
An Introduction


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Getting Started
Back SMYC Forward
  • The String Class:
    • Encapsulates an ordered set of characters
  • String Literals:
    • Enclosed in double quotes
Immutability
Back SMYC Forward
  • An Important Property of String Objects:
    • String objects are immutable (i.e., they can't be changed)
  • An Observation:
    • This surprises people when they first hear it because they think they have changed them (e.g., using concatenation or toLowerCase()) but the result, which is another String, is always assigned to a variable or passed as an actual parameter
Immutability (cont.) - "Changing" Case
Back SMYC Forward
  • The Methods:
    • String.toLowerCase()
    • String.toUpperCase()
  • Implications of Immutability:
    • A String object is created (the owning object is not changed)
Immutability (cont.) - "Changing" Case
Back SMYC Forward
  • An Improper Use:
    • String course; course = new String("CS149"); course.toLowerCase();
  • A Proper Use:
    • String course; course = new String("CS149"); course = course.toLowerCase();
Concatenation
Back SMYC Forward
  • The Operator:
    • +
  • Implications of Immutability:
    • A String object is created
Concatenation (cont.)
Back SMYC Forward
  • An Improper Use:
    • String course, number; course = new String("CS"); number = new String("149"); // This line won't compile because it isn't a statement course + number;
  • A Proper Use:
    • String course, number; course = new String("CS"); number = new String("149"); course += number; // course = course + number;
Concatenation (cont.)
Back SMYC Forward
  • Right-Side Operands:
    • The right-side operand of the + operator need not be a String if it can be converted into one
  • An Example of a Common Use:
    • int number; String course, dept; dept = new String("CS"); number = 149; course = dept + number;
Concatenation (cont.)
Back SMYC Forward
  • A Potential Pitfall:
    • Since the + is also used as the "positive" operator and the addition operator, it is easy to make mistakes
  • An Example:
    • String s; s = "Value: "; s += 'a' + 'b'; JMUConsole.println(s);
      The output will be Value: 195 because 'a' + 'b' is evaluated first and evalutes to the sum of the ASCII values
Concatenation (cont.)
Back SMYC Forward
  • A Better Approach:
    • Use String.format() which behaves just like printf() but returns a String rather than printing it
  • An Example:
    • String s; s = String.format("Value: %c%c", 'a', 'b'); JMUConsole.println(s);
      The output will be Value: ab
Why Are We Suddenly Using the new Operator?
Back SMYC Forward
  • Remember:
    • The new operator creates (i.e., allocates memory for an initializes) an object
    • We've assigned literals to String variables before without using new
  • What's Going On?
    • As a convenience, the compiler creates a String object for each String literal
    • String s = "CS"; assigns the reference to the literal to the variable s
    • String t = new String("CS"); creates a String object that contains the characters 'C' and 'S' and assigns the reference to that object to the variable t
Why Are We Suddenly Using the new Operator? (cont.)
Back SMYC Forward
  • What To Do?
    • Now that you know about the new operator you should use it (even though it's a little inconvenient) because it will keep you from making some subtle mistakes
  • What To Remember?
    • Other people will assign String literals because it is convenient
    • At some point you too will know enough to do so, but shouldn't now
Some String Methods
Back SMYC Forward
  • Length:
    • String.length()
  • Examining Individual Characters:
    • String.charAt(int)
  • Comparing String Objects:
    • String.compareTo(java.lang.String)
    • String.equals(java.lang.String)
    • Never use the == operator (it compares references not the characters)
Some String Methods (cont.)
Back SMYC Forward
  • Searching/Containment:
    • String.indexOf(java.lang.String)
    • String.indexOf(java.lang.String, int)
    • String.endsWith(java.lang.String)
    • String.startsWith(java.lang.String, int)
  • Extracting Substrings:
    • String.substring(int)
    • String.substring(int, int)
Examples
Back SMYC Forward

Counting Vowels

javaexamples/strings/VowelCounter.java
 
Examples (cont.)
Back SMYC Forward

A Hex Converter

javaexamples/strings/HexConverter.java
 
There's Always More to Learn
Back -