The String Class is provided by Java
for the discussion and use of strings. In Java, strings are objects, not
primitive types such as int, double, char,
Strings are often treated, however, as it they are primitives.
The term string literal refers to any alphanumeric (character, digit, or both) enclosed in
double quotation marks (such as "Snoopy" or "123 West Third
Warning! If a string literal contains only
numeric digits ("1234"), it is NOT a number
(an integer). Be careful! It
is just a string of numeric digits that cannot be used to
perform mathematical calculations such as addition or
multiplication. Remember that you can perform
calculations on numeric data only, not on string literals. "1234" is not the same
"1234" is a
1234 is a number
Declaring a String variable is
different from declaring an int, double, char
or boolean. The variable name
of a String holds
the address of the memory location where the actual body of the
string is stored.
Each time a new assignment is stored in the
String variable name, the computer
finds a new memory location to hold the String
text and adjusts the contents of name to
hold the address of this new location. The old memory location
is set free.
Variable used in the manner (such as name)
are called references. The address values stored in
these variables are said to "point" to the actual data.
of declaring and initializing strings:
String greeting; // declare the string
greeting = "Hello"; //initialize the string
String title = "Captain"; //declare
and initialize at one time
System.out.println (title); //print
|String poem = Console.readLine("Enter the
|// This code will read all the characters
from the typed "line" up to the
// carriage return and store it in the variable called poem.
String is a "class" and as such has methods that are properties of the class.
To use a method, we need the dot operator.
- length( ) This is a
numerical returning method that gives the length
of the string. It returns an integer value.
int citylength = city.length( );
//stores the length of a city name into an int
System.out.println(citylength); //prints the
- charAt (n)
This is a value returning method. The argument states which character to return. The
subscripting of the locations of the characters starts with zero.
System.out.println (holiday.charAt (4) ); //prints out
String puppy ="Wally";
System.out.println (puppy.charAt (0) ); //prints out a 'W'
- trim() This is a
value returning method that returns a copy of the
argument after removing its leading and trailing blanks (not embedded blanks -
blanks within the statement). The string will not be changed in
String burp = " hic
burp.trim() ); // prints out hic up
In memory, the string is not changed, but it will be printed out in all lower
String cityname = "Houston";
System.out.println (cityname.toLowerCase() );
- substring (Start, End)
This is a value returning method. A string is returned beginning at Start
subscript up to but not including the End
String sport = "football";
System.out.println (sport.substring (1,3) ); // prints out "oo"
substring ( Start)
This is an alternate version. It returns the substring beginning at Start
up to and including the end of the string.
String sport = "football";
System.out.println (sport.substring (1) ); // prints out "ootball"
Returns the beginning subscript of the string where
"Garfield" is found
Example: (find the comma in a city, state, zip listing)
String city = "Fulton, New York 13069";
System.out.println ( city.indexOf (",")); // prints 6
equals ( ) Used
to compare two strings with the method to see
if they are exactly the same, this includes any blanks or spaces within the
Example: (check to see if the
user entered a dog name of Snoopy)
System.out.println ( "The user entered Snoopy.);
Compares strings to determine alphabetic location. Returns a zero if the two strings are equal,
a negative if the first string is alphabetically before the compared string,
and a positive if the first string is alphabetically after the compared
String subject = "mathematics";
answer = subject. compareTo ("biology"); // returns is a positive
answer = subject. compareTo("philosophy"); // answer is negative
answer = subject.compareTo (" mathematics"); //answer is zero