Chapter 7
Characters, Strings, and the StringBuilder

Student Learning Outcome
Identify string data problems
2. Manipulate characters
3. Declare and compare String objects
4. Use other String methods
5. Convert Strings to numbers
6. Use the StringBuilder and StringBuffer classes

Important Points:
1. Identify string data problems. A string variable name is not a data type but a reference that holds a memory address. When comparing two strings using == you are not comparing the values but the computer memory location. Most often programmers want to compare values. We can use the following Java classes to do so.
a. Character - a class whose instances can hold a single character value. Also defines methods that can manipulate or inspect single-character data.
b. String - a class for working with fixed string data, unchanging data composed of multiple characters.
c. StringBuilder and StringBuffer - class for storing and manipulating changeable data composed of multiple characters.

2. Manipulating Characters.




Tests if character is upper case


Returns the uppercase equivalent of the argument. No change is made if character is upper case.


Tests if character is lowercase


Returns true if argument is a digit(0-9) and false if not.


Returns true if the argument is letter and false if not


Returns true if the argument is letter or digit and false if not.


Returns the lowercase equivalent of the argument, no change if already lower case.


Returns true if argument is whitespace and false if not. space,tab,newline,carriage return, and form feed are considered whitespace.

a. The character class is automatically imported into ever program you write java.lang.Object class.Character.isUpperCase(achar) calls the character class and uses the isUpperCase method.
char is primitive data class. Character is class.

3. Declaring and comparing string objects

a. Anonymous object (literal string) is an unnamed object of the string class.
b. A String variable is simply a named object of the same class.
java.lang.String is automatically imported into every program you write.
String aGreeting="Hello"; is allowed in JAVA, instead of
String aGreeting = new String("hello")
aGreeting holds the address where Hello is stored. A String variable name refers to a location in memory not its value.
f. If you declare two strings initialized to the same value, the value is stored only once in memory and the two objects referenced hold the same memory address.
g. You cannot choose the memory address where a value is stored - they are chosen by the operating system.
h. String objects refer to the memory address and if new data is assigned, it is given a new address. Strings are not actually changed. The new assignment is given a different memory address and is called immutable.
i. When you compare strings with == you are comparing memory addresses not values. 
4. Using other Methods for comparing String class.




Evaluates the contents of two String objects to determine if they are equivalent - if(aName.equals(anothername))- method header - public boolean equals(string s)


Ignores case when determining if two strings are equivalent. Useful when user is responding to prompts and may hit the shift key


Used to compare to strings - returns 0 if identical, returns -number if calling object is less, returns a + number if calling object is more than - based on Unicode


Converts string to upper case - aWord=aWord.toUpperCase()


Converts string to lower case- aWord=aWord.toLowerCase()


Returns the length of a word - String greeting="Hello"; int len=greeting.length(); accessor methods return info about an object.


Determines whether a specific character occurs with a String. Returns the position in the string or -1 if char is not found in string - String name="Stacy" name.indexOf('S') return 0 for first char in string.


name.charAt(0) returns 'S' the character at position 0 in the string


name="Stacy"; name.endsWith("z") would return false


name-"Stacy"; name.startsWith("Sta") would return true


Allows you to replace all occurrences of some character within a string. String Name="Stacy"; String newname=name.replace('c','x'); assigns "Staxy" to newname


Useful when working with Strings. Converts any object to a String


Takes two integer arguments - start position and end. The length of substring extracted is the difference between the second integer and the first integer.

5. Converting Strings to Numbers.
 When you try to convert a string to number that contains a letter or you try to convert the wrong type of number you will get NumberFormatException error.
parseInt() takes a string argument and returns its integer value. - int anInt=Integer.parseInt("649") and stores the numeric value 649 in variable anInt.
parseDouble() takes a string argument and returns its double value double doublevalue =Double.parseDouble("147.44")
parsefloat() and parseLong(0) are also available.

6. Using
StringBuilder and StringBuffer Classes.  When you know a string may be modified use StringBuilder or StringBuffer.
a. StringBuilder is more efficient
b. StringBuffer is thread safe. Means you should use it in application that runs multiple threads.
c. All references to StringBuilder will apply to StringBuffer.
StringBuilder message = new StringBuilder("Hello there");  when using StringBuilder you must use new in the constructor.
StringBuilder uninitializedString=null; creates an empty variable.
f. A StringBuilder contains a buffer - allocated memory - the length of the buffer is the capacity.
SetLength() allow the changing of the string length - length property identifies the number of characters in the String contained in StringBuilder.
h. If StringBuilder specifies a length of the string that is longer than the string, it inserts '\u0000' to fill in the buffer.
i. If StringBuilder specifies a length of the string that is shorter than the string, it is truncated.
capacity() will return the length of the string plus 16 held in the buffer.
k. The ability of Stringbuilder to be modified makes them more efficient when you string contents will change.




Constructs a StringBuilder with no character and a default size of 16

publicStringBuilder(int capacity)

Constructs a StringBuilder with no characters and a capacity specified by the parameter.

publicStringBuilder(String s)

Contains the same characters as those stored in the String object s(length of the String argument) plus 16


CharSequence - is another Java class that holds a sequence of char values.

l. append() - lets you add characters to the end of Stringbuilder object.
insert() lets you add characters at a specific location within a StringBuilder object. The first position of a StringBuilder object is 0.
charAt() - accepts an argument that is the offset of the character position from the beginning of a string and returns the character at that position.