Chapter 7
Characters, Strings, and the StringBuilder

Student Learning Outcome
1.
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.

Method

Description

isUpperCase()

Tests if character is upper case

toUpperCase()

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

isLowerCase()

Tests if character is lowercase

isDigit()

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

isLetter()

Returns true if the argument is letter and false if not

isLetterOrDigit()

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

toLowerCase()

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

isWhitespace()

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.
b.
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.
c.
java.lang.String is automatically imported into every program you write.
d.
String aGreeting="Hello"; is allowed in JAVA, instead of
String aGreeting = new String("hello")
e.
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.

Method

Behavior

Equals()

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

equalsIgnoreCase()

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

compareTo()

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

toUpperCase()

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

toLowerCase()

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

length()

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

indexOf()

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.

charAt()

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

endsWith()

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

startsWith()

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

replace()

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

toString()

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

substring()

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.
a.
parseInt() takes a string argument and returns its integer value. - int anInt=Integer.parseInt("649") and stores the numeric value 649 in variable anInt.
b.
parseDouble() takes a string argument and returns its double value double doublevalue =Double.parseDouble("147.44")
c.
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.
d.
StringBuilder message = new StringBuilder("Hello there");  when using StringBuilder you must use new in the constructor.
e.
StringBuilder uninitializedString=null; creates an empty variable.
f. A StringBuilder contains a buffer - allocated memory - the length of the buffer is the capacity.
g.
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.
j.
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.

StringBuilder

Constructor

publicStringBuilder()

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

publicStringBuilder(CharSequence)

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

l. append() - lets you add characters to the end of Stringbuilder object.
m.
insert() lets you add characters at a specific location within a StringBuilder object. The first position of a StringBuilder object is 0.
n.
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.