- Forward


Using Strings
An Introduction with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Getting Started
Back SMYC Forward
  • The String java.lang.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:
    • toLowerCase()
    • 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("CS159"); course.toLowerCase();
  • A Proper Use:
    • String course; course = new String("CS159"); 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("159"); // 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("159"); 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 = 159; 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';
      s 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 the String.format(java.lang.String, java.lang.Object...) java.lang.String#format(java.lang.String, java.lang.Object...) method
  • An Example:
    • String s; s = String.format("Value: %c%c", 'a', 'b');
      s will contain "Value: ab"
The Format String
Back SMYC Forward
  • Components:
    • String literals
    • Format specifiers
  • Format Specifiers:
    • Syntax: %[flags][width][.precision]conversion
  • Conversions:
    • 'b' for a boolean
    • 'c' for a char
    • 'd' for an integer
    • 'e' for a real number in scientific notation
    • 'f' for a real number
    • 's' for a String
  • Flags:
    • '-' for left-justified
    • '+' to always include the sign
    • ' ' to use a space for the sign of a positive number
    • '0' to pad with zeros
    • ',' to use grouping separators
    • '(' to put negative numbers in parentheses
Do We Need the new Operator?
Back SMYC Forward
  • Remember:
    • The new operator creates (i.e., allocates memory for an initializes) an object
    • You can, and we have, assigned literals to String variables 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
Do We Need 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:
    • length()
  • Examining Individual Characters:
    • charAt(int)
  • Comparing String Objects:
    • compareTo(java.lang.String)
    • equals(java.lang.String)
    • (Remember: The == operator compares references not the characters.)
Some String Methods (cont.)
Back SMYC Forward
  • Searching/Containment:
    • indexOf(java.lang.String)
    • indexOf(java.lang.String, int)
    • endsWith(java.lang.String)
    • startsWith(java.lang.String, int)
  • Extracting Substrings:
    • substring(int)
    • substring(int, int)
Java vs. Python - Important Differences
Back SMYC Forward
  • Literals:
    • In Python, string literals can be enclosed in either single-quotes or double-quotes
    • In Python, multi-line string literals are enlosed in three double-quotes
  • Accessing Elements:
    • In Python, elements of a string can be accessed using the [] operator
  • Length:
    • In Python, the length of a string can be determined by passing it to the len() function
  • Containment:
    • In Python, containment is determined using the in operator
There's Always More to Learn
Back -