chapter 7 (so far) Flashcards
What is a string
-A String is a class
-Each created String is a class object
The String variable name is not a simple data type
-Holds the memory address of a string and is immutable
Reference
A variable that holds a memory address
What does the == operator do regarding strings
it compares the memory locations of the strings rather than their values
-Compares constants of the memory locations more frequently than memory locations themselves
What class to use for character
- Char/ Character
- holds a single character value
- defines methods that can manipulate or inspect single-character data
What class to use for working with fixed string data and unchanging data composed of multiple characters
string
What are StringBuilder and StringBuffer
Classes for storing and manipulating changeable data composed of multiple character
Describe the character class’ methods
Methods that begin with “is” such as isUpperCase() return a boolean value that can be used in comparison statements
Methods that begin with “to” such as toUpperCase() return a character that have been converted to the stated format
literal string
A sequence of characters enclosed within double quotation marks
An unnamed object, or anonymous object, of the String class
String variable
A named object of the String class
Class String
Defined in java.lang.String
Automatically imported into every program
What is the relation between the string and variable assigned to it
the string itself is distinct from the variable used to refer to it
string variable name
a reference to a variable
refers to a location in memory rather than a particular variable
What happens when a new value is given to a string
the address held by the string is altered
immutable
unchanging objects such as strings
this means making simple comparisons between strings produces sometimes misleading results
-Compares memory addresses, not values
equals() method
Evaluates the contents of two String objects to determine if they are equivalent
Returns true if objects have identical contents
public boolean equals(String s)
equalsIgnoreCase() method
Ignores case when determining if two Strings are equivalent
Useful when users type responses to prompts in programs
compareTo() method
Compares two Strings and returns:
-Zero: If two Strings refer to the same value
-Negative number: If the calling object is “less than” the argument
-Positive number: If the calling object is “more than” the argument
if (aWord.compareTo(anotherWord) < 0)
What is an empty string
Reference a memory address with no characters
Can be used in String methods
null string
- Use the null Java keyword
- Strings are set to null by default
- Cannot be used in String methods
toUpperCase() and toLowerCase()
Convert any String to its uppercase or lowercase equivalent
length()
Returns the length of a String
indexOf() method
Determines whether a specific character occurs within a String
Returns the position of the character
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
Indicates the position of the character that the method returns
endsWith() method and startsWith()
- Each takes a String argument
- Return true or false if a String object does or does not end or start with the specified argument, respectively
replace()
Replaces all occurrences of some character within a String
toString()
Not part of the String class Converts any object to a String Converts primitive data types to Strings String theString; int someInt = 4; theString = Integer.toString(someInt);
Concatenation
Join a simple variable to a String
String aString = “My age is “ + myAge;
Use the + operator
substring()
Extracts part of a String Takes two integer arguments -Start position -End position The length of the extracted substring is the difference between the second integer and the first integer
regionMatches()
Two variants that can be used to test if two String regions are equal
when is A substring of the specified String object is compared to a substring of the other
If the substrings contain the same character sequence, then the expression is true
Otherwise, the expression is false
A second version uses an additional boolean argument
Determines whether case is ignored when comparing characters
Integer class
Part of java.lang Automatically imported into programs Converts a String to an integer parseInt() method Takes a String argument Returns its integer value
Wrapper
A class or an object “wrapped around” a simpler element
Integer class valueOf() method
Part of java.lang
Automatically imported into programs
Converts a String to an integer
parseInt() method
Takes a String argument
Returns its integer value
Wrapper
A class or an object “wrapped around” a simpler element
Integer class intValue() method
Extracts the simple integer from its wrapper class
Double class
A wrapper class Imported into programs automatically parseDouble() method Takes a String argument and returns its double value
StringBuilder and StringBuffer classes
An alternative to the String class Used when a String will be modified Can use anywhere you would use a String Part of the java.lang package Automatically imported into every program
Compare StringBuilder and StringBuffer
StringBuilder is more efficient
StringBuffer is thread safe and used in multithreaded programs
Buffer
A memory block
Might or might not contain a String
The String might not occupy the entire buffer
The length of a String can be different from -the length of the buffer
Capacity
-The actual length of the buffer
setLength() method
Changes the length of a String in a StringBuilder object
length property
An attribute of the StringBuilder class Identifies the number of characters in the String contained in the StringBuilder
capacity() method
Finds the capacity of a StringBuilder object
What do StringBuilder objects do
Provides improved computer performance over String objects
Can insert or append new contents into StringBuilder
append() method
Adds characters to the end of a StringBuilder object
insert() method
Adds characters at a specific location within a StringBuilder object
setCharAt() method
Changes a character at a specified position within a StringBuilder object
charAt() method
Accepts an argument that is the offset of the character position from the beginning of a String
Returns the character at that position