Transcript Strings

Strings
A string is a sequence of characters that is treated as a
single value. Strings are objects.
We have been using strings all along. Every time you
use a “” string, a string object is created automatically.
For example, to display text
System.out.println("Hello, how are you?");
Blotgrunge ... out to reality ... HelloWorld.java
String is a class in the java.lang package.
Explicit String Objects
A declaration and object creation are needed for
instances of the String class. For example,
String name1,name2,name3;
name1 = "Frooot";
name2 = new String(name2);
name3 = new String({’F’,’r’,’o’,’o’,’t’});
Blitititi … out to reality … StringVar.java
Blitititi … out to reality … StringVar3.java
Concatenating Strings
The + operator concatenates strings.
A new string object is created - the operands are not
affected. Old strings are garbage collected.
Gadzook … out to reality … StringCat.java
Note that System.out.print really does this, not
“and also print”.
Gadzook … back to reality … StringVar3.java
String variables are References - 1
Code
A
String
word1, word2;
word1 = "Java”;
word2 = word1;
State
of
Memory
word1
L
word2
L
After A is executed
Both word1 and word2
are allocated memory
(to store references),
but the objects
themselves are not yet
created, so they both
contain null.
String variables are References - 2
Code
String
B
word1, word2;
One String object is
created and assigned
to word1, so word1
contains the address of
this object.
word1 = "Java”;
word2 = word1;
word1
String
Java
State
of
Memory
word2
L
After B is executed
String variables are References - 3
Code
String
Content of word1,
which is an address, is
assigned to word2,
making word2 refer to
the same object.
word1, word2;
word1 = "Java”;
C
word2 = word1;
word1
State
of
Memory
String
Java
word2
After C is executed
Gadzook … out to reality … StringAlias.java
Gadzook … out to reality … StringAlias2.java
Equality (==) vs. equals – Case 1
String
Java
word1
String
word2
word1 == word2
word1.equals( word2 )
Java
false
true
word1 and word2
point to different
objects having
the same string.
Equality (==) vs. equals – Case 2
String
Java
word1
String
word2
word1 == word2
word1.equals( word2 )
Bali
false
false
word1 and word2
point to different
objects with
different strings.
Equality (==) vs. equals - Case 3
word1
String
Java
word2
word1 == word2
word1.equals( word2 )
true
word1 and word2
point to the same
object.
true
Quixote … out to reality … StringJavas.java
Determining the Size
We determine the number of characters in a String
with the length method.
String name = "Sumatra";
String str2 = "";
String str3;
name.length();
7
Error because no
object is created for
str3, so it is a null.
str2.length();
0
str3.length();
Error!
Accessing Individual Elements
Individual characters in a String accessed with the
charAt method.
String name = “Sumatra”;
name
0
1
2
3
4
5
6
S
u
m
a
t
r
a
name.charAt( 3 )
Olipidoo … out to reality … StringVowels.java
Olipidoo … out to reality … StringWords.java
Other Useful String Operators
Method
Meaning
compareTo
Compares the two strings.
str1.compareTo( str2 )
substring
Extracts the a substring from a string.
str1.substring( 1, 4 )
trim
Removes the leading and trailing spaces.
str1.trim( )
valueOf
Converts a given primitive data value to a string.
String.valueOf( 123.4565 )
startsWith
Returns true if a string starts with a specified prefix string.
str1.startsWith( str2 )
endsWith
Returns true if a string ends with a specified suffix string.
str1.endsWith( str2 )
Command line Strings
The formal arguments to the main method receive
strings from the command line arguments.
When running a program, supply command line
arguments after the program name, e.g.,
java MyJavaProgram cat 27 'Java is great!'
has three command line arguments.
Gadzook … out to reality … MyJavaProgram.java
Strings and Methods
Strings are reference variables, so (like arrays) formal
parameters point to the actual parameter data
Strings returned from methods point to the data
created in the method
Likidylik … out to reality … StringMethod.java
Strings are Immutable
A String object is immutable, which means that once
a String object is created, is cannot be changed.
It is not possible to add, delete, or modify characters
of a String object.
The methods of the String class, such as
toUpperCase and substring, do not modify the
original string; they return a new string.
Java adopts this immutability restriction to implement
an efficient memory allocation scheme for managing
String objects.
Oieeeeiooo … out to reality … StringImmut.java
StringBuffer
Creating a new string from the old one will work for
most cases, but sometimes manipulating the content
of a string directly is more convenient.
Manipulation means operations such as replacing
a character, appending a string with another
string, deleting a portion of a string, and so forth.
Java has a StringBuffer class for this.
StringBuffers are created from strings (no shorthand)
Ay … out to reality … StringBufferX.java
Ay … out to reality … StringBufferMake.java