Busy. Please wait.
or

show password
Forgot Password?

Don't have an account?  Sign up 
or

Username is available taken
show password

why


Make sure to remember your password. If you forget it there is no way for StudyStack to send you a reset link. You would need to create a new account.
We do not share your email address with others. It is only used to allow you to reset your password. For details read our Privacy Policy and Terms of Service.


Already a StudyStack user? Log In

Reset Password
Enter the associated with your account, and we'll email you a link to reset your password.
Don't know
Know
remaining cards
Save
0:01
To flip the current card, click it or press the Spacebar key.  To move the current card to one of the three colored boxes, click on the box.  You may also press the UP ARROW key to move the card to the "Know" box, the DOWN ARROW key to move the card to the "Don't know" box, or the RIGHT ARROW key to move the card to the Remaining box.  You may also click on the card displayed in any of the three boxes to bring that card back to the center.

Pass complete!

"Know" box contains:
Time elapsed:
Retries:
restart all cards
share
Embed Code - If you would like this activity on your web page, copy the script below and paste it into your web page.

  Normal Size     Small Size show me how

Chapter 7

Characters, Strings, and the StringBuilder

TermDefinition
Class Character A class whose instances can hold a single character value and whose methods manipulate and inspect single-character data
Class String A class for working with fixed-string data - that is, unchanging data composed of multiple characters
Class StringBuilder and StringBuffer Classes for storing and manipulating changeable data composed of multiple characters
String In Java, String is a class, and each created String is an object. As an object, a String variable name is not a simple data type - it is a reference; that is, a variable that holds a memory address
Comparing String Using == Operator When you compare two String objects using the == operator, you are comparing not their values but their computer memory locations
1. Character Class Methods 1. Method[isUpperCase()]..Description[Tests if character is uppercase]... 2. Method[toUpperCase()]..Description[Returns the uppercase equivalent of the argument; no change made if not lowercase letter]...
2. Character Class Methods 3. Method[isLowerCase()]..Description[Tests if character is lowercase]... 4. Method[toLowerCase()]..Description[Returns the lowercase equivalent of the argument; no change made if not an uppercase letter]...
3. Character Class Methods 5. Method[isDigit()]..Description[Returns true if the argument is a digit(0-9) and false otherwise]... 6. Method[isLetter()]..Description[Returns true if the argument is a letter and false otherwise]...
4. Character Class Methods 7. Method[isLetterOrDigit()]..Description[Returns true if the argument is a letter or digit and false otherwise]...
5. Character Class Methods 8. Method[isWhitespace]..Description[Returns true if the argument is whitespace and false otherwise; this includes the space, tab, newline, carriage return, and form feed]
Literal Strings and String Variables A literal string is an unnamed object, or anonymous object, of the String class, and a String variable is simply a named object of the same class
Immutable When an object cannot be changed. Strings are immutable
equals() Method The String class equals() method evaluates the contents of two String objects to determine if they are equivalent. Ex: aName.equals(anotherName);
Lexicographical Comparison A comparison based on the integer Unicode values of the characters. Technically, the equals() method does not perform an alphabetical comparison with Strings; it performs a lexicographical comparison
equalsIgnoreCase() Method The String class equalsIgnoreCase() method is similar to the equals() method except this method ignores case when determining if two Strings are equivalent
1. compareTo() Method When the String class compareTo() method is used to compare two Strings, it provides additional information to the user in the form of an integer value. The method returns zero only if the two Strings refer to the same value.
2. compareTo() method If there is any difference between the Strings, a negative number if returned if the calling object is "less than" the argument, and a positive number if the calling objects is "more than" the argument.
3. compareTo() Method Strings are considered "less than" or "more than" each other base on their Unicode values; thus, "a" is less than "b", and b" is less than "c"
Methods toUpperCase() and toLowerCase() The methods toUpperCase() and toLowerCase() convert any String to its uppercase or lowercase equivalent
length() Method Is an accessor method that returns the length of a String
indexOf() Method Determines whether a specific character occurs within a String. The method returns the position of the character if it does; the first position of a String is zero. The return value is -1 if the character does not exist in the String
charAt() Method Requires an integer argument that indicates the position of the character that the method returns, starting with 0. For example, if myName is a String that refers to "Stacy", the value of myName.charAt(0) is "S".
endsWtih() and startsWith() Method Each take a String argument and and return true or false if a String object does or does not end or start with the specified argument. For example, if String myName = "Stacy";, then myName.startsWith("Sta") is true. These methods are case sensitive
replace() Method Allows you to replace all occurrences of some character within a String. For example, if String yourName = "Annette";, then String goofyName = yourName.replace('n', 'X'); assigns "AXXette" to goofyName. This method is case sensitive
toString() Method Although not part of the String class, the toString() method is useful when working with String objects. It converts any object to a String. Ex:... String theString;.. int someInt = 4;.. theString = integer.toString(someInt);
Concatenation Can be used to covert any primitive type to a String. You can join a simple variable to a String, creating a longer String using the + operator. Ex:... String aString = "My age is " + myAge;
1. substring() Method You can extract part of a String with this method and use it alone or concatenate it with another String. It takes two integer arguments - a start position and an end position - that are both based on the fact that a String's first position is 0.
2. substring() Method The length of the extracted substring is the difference between the second and first integer; if you call the method without a second integer argument, the substring extends to the end of the original string.
1. Version 1 regionMatches() Method Can be used to test whether two String regions are same. This version of the method takes four arguments - position at which to start in the calling String, other String being compared, position to start in the other String, and length of the comparison.
2. Version 1 regionMatches() Method For example, suppose that you have declared two String objects as follows:.. String firstString = "abcde";.. String secondString = "xxbcdef";... Then, the expression firstString.regionMatches(1, secondString, 2, 4) is true.
Version 2 regionMatches() Method This version of the method takes an additional boolean argument as the first argument. This argument represents whether case should be ignored in deciding whether region matches. Ex: thirdString.regionMatches(true, 4, fourthString, 2, 5)
Integer Class Is part of java.lang and is automatically imported into program you write. To convert a String to an integer, you use the Integer class. This class is an example of a wrapper.
Wrapper Is a class or object that is "wrapped around" a simpler element; the Integer wrapper class contains a simple integer and useful method to manipulate it
parseInt() Method Is part of the Integer class; it takes a String argument and returns its integer value. For example, the following statement stores the numeric value 649 in the variable anInt:.. int anInt = Integer.parseInt("649");
1. Converting a String to an Integer Using A Different Approach You can use the Integer class valueof() method to convert a String to an Integer class object, and then use the Integer class intValue() method to extract the simple integer from its wrapper class.
2. Converting a String to an Integer Using A Different Approach Ex:... integerHours = Integer.valueOf(stringHours);... hours = integerHours.intValue();
Double Class Is a wrapper class and is imported into your programs automatically. It is easy to convert a String object to a double using the Double class
parseDouble() Method The Double class parseDouble() method takes a String argument and returns its double value. For example, the following statement stores the numeric value 147.82 in the variable doubleValue. Ex:.. double doubleValue = Double.parseDouble("147.82");
1. StringBuilder and StringBuffer Class You use one of these classes, which are alternatives to the String class, when you know a String will be modified; usually, you can use a StringBuilder or StringBuffer object anywhere you would use a String.
2. StringBuilder and StringBuffer Class Like the String class, these two classes are part of the java.lang package and are automaticallyl imported into every program. The classes are identical except that StringBuilder is more efficient and StringBuffer is thread safe
Thread of Execution Units of processing that are scheduled by an operating system that can used to create multiple paths of control during program execution. You should use StringBuffer in applications that run multiple threads of execution
StringBuilder Object Contains a memory block called a buffer, which might or might not contain a string. In other words, the length of a string can be different from the length of the buffer. The actual length of the buffer is the capacity of the StringBuilder object
1. setLength() Method You can change the length of a string in a StringBuilder object with this method. The length of a StringBuilder object equals the number of characters in the String contained in the StringBuilder.
2. setLength() Method When you increase a StringBuilder object's length to be longer than the String it holds, the extra characters contain '\u0000'. If you use this method to specify a length shorter than its String, the string is truncated
capacity() Method Used to find the capacity of a StringBuilder object. Ex:... StringBuildewr nameString = new StringBuilder("Barbara");... int nameStringCapacity = nameString.capacity();.
StringBuilder Object Capacity When you create a StringBuilder object, its capacity is the length of the String contained in StringBuilder, plus 16. The "extra" 16 positions allow for modification of the StringBuilder object after creating without allocating any new memory locations
equals() Method and StringBuilder Objects When you use the equals() method with StringBuilder objects, it compares references and not object contents. You can compare the contents of two StringBuilder object by converting them to String. Ex:... obj1.toString().equals(obj2.toString());
StringBuilder Class's Four Constructors 1. public StringBuilder()... 2. public StringBuilder(int capacity)... 3. public StringBuilder(String s)... 4. The fourth StringBuilder constructor uses an argument of type CharSequence.
StringBuilder Class's First Constructor public StringBuilder() constructs a StringBuilder with no characters and a default size of 16 characters
StringBuilder Class's Second Constructor public StringBuilder(int capacity) constructs a StringBuilder with no characters
StringBuilder Class's Third Constructor public StringBuilder(String s) contains the same characters as those stored in the String objects s. The capacity of the StringBuilder is the length of the String argument you provide, plus 16 additional characters
StringBuilder Class's Fourth Constructor The fourth StringBuilder constructor uses an argument of type CharSequence. CharSequence is another Java class; it is an interface that holds a sequence of char values.
append() Method Lets you add charactes to the end of a StringBuilder object. Ex:.. StringBuilder phrase = new StringBuilder("Happy");... phrase.append(" birthday");. This alters the phrase to hold "Happy birthday"
insert() Method Lets you add characters at a specific location within a StringBuilder object. For example, if phrase refers to "Happy birthday", then phrase.insert(6, "30th "); alters the stringBuiler to contains "Happy 30th birthday". The first character is at 0
1. setCharAt() Method Allows you to change a character at a specified position within a StringBuilder object. This method requires two arguments: an integer position and a character.
2. setCharAt() Method If phrase refers to "Happy 30th birthday", then phrase.setCharAt(6, '4'); changes the value into a 40th birthday greeting
charAt() Method Used to extract a character from a StringBuilder object. Accepts an argument that is the offset of the character position from the beginning of a String and returns the character at that position.
charAt() Method Example The following statement assigns the character 'P' to the variable letter:... StringBuilder text = new StringBuilder("Java Programming")... char letter = text.charAt(5);