chapter 7 (so far) Flashcards
(48 cards)
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