Ch. 7 Characters, Strings, and the StringBuilder

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Classes we use to work with text data

1. Characters: a class whose instance holds single char 2. String: a class for working w/ fixed string data 3. StringBuilder & StringBuffer: classes for storing and manipulating changeable data composed of multiple char

equal()

1. Used w/ String object and the method uses the unique contents of that object to make a comparison, you can tell that it is not a static method. 2. Can be used in an if statement, you can tell that it returns a Boolean value. 3. String used between the parentheses in the method call, you can tell that the equals() method takes a String argument. similar to public boolean equals(String s)

Now aGreeting holds "Bonjour" instead of "Hello"

1. address held by aGreeting is altered 2. aGreeting holds a new address where the characters Bonjour are stored. 3. "Bonjour" is an entirely new object created with its own location. 4. The Hello String is still in memory, but aGreeting no longer holds its address. 5. Eventually, a part of the Java system called the garbage collector discards the Hello characters so the memory address can be used for something else. 6. Strings, therefore, are never actually changed; instead, new Strings are created and String references hold the new addresses. Strings and other objects that can't be changed are immutable.

The Character class contains standard methods for testing the values of characters

1. isUpperCase() return T/F 2. toUpperCase() makes it capitalized 3. isLowerCase() return T/F 4. toLowerCase() 5. isDigit() true if the argument is a digit (0-9) or false 6. isLetter() 7. isLetterOrDigit() true if the argument is a letter or digit 8. isWhitespace() Returns true if the argument is whitespace and false otherwise; this includes the space, tab, newline, carriage return, and form feed - Carriage return means to return to the beginning of the current line without advancing downward. T - Form feed means advance downward to the next "page".

The String class equals() method evaluates the contents of two String objects to determine if they are equivalent

Ex: if(aName.equals(anotherName))

Declare a variable a primitive data type

Ex: int x = 10; the memory address where x is located holds the value 10. If you later assign a new value to x, the new value replaces the old one at the assigned memory address. For example, if you code x = 45;, then 45 replaces 10 at the address of x

The creators of Java made Strings immutable for several reasons.

For example, in environments where multiple programs (or parts of programs, called threads of execution) run concurrently, one logical path cannot change a String being used by another path. The compiler also can be made to execute more efficiently with immutable String objects.

String comparison inefficiencies

If you created a string that stored "Dove" and you asked a user to enter a name and they entered "Dove" and it is stored in a string; if you compare these two Strings that hold "Dove" it would say they're not equal to each other. Why? Answer is below

String variable is

a named object of the same class. The class String is defined in java.lang.String, which is imported automatically into every program you write.

A string variable name is

a reference that is it holds a memory address.

indexOf()

determines whether a specific character or String occurs within a String. First position in String is 0. Ex: String name = "Stacy"; name.indexOf('S'); returns 0 name.indexOf('a'); returns 2 name.indexOf('x'); returns -1 (cuz it doesn't exist in Stacy)

Wrapper

is a class or object that is "wrapped around" a simpler element. Integer and Double class is a wrapper Double class parseDouble() method Also parseFloat() parseLong()

Char is a primitive data so

it is not a reference and can be compared using relational operators such as > or ==.

The equals() method does not perform an alphabetical comparison with Strings;

it performs a lexicographical comparison—a comparison based on the integer Unicode values of the characters

The Character class is defined in

java.lang and is imported automatically into every program you write. The Character class inherits from java.lang.Object.

replace()

method allows you to replace all occurrences of some character within a String. case sensitive If String partNum = "BK 761 23"; String newPartNum = partNum.replace(' ', '-'); Returns "BK-761-23"

substring()

method takes two integer arguments—a start position and an end position; if you call the method without a second integer argument, the substring extends to the end of the original string. Note that the end position is the position of the first character that YOU DO NOT WANT in the substring.

toString()

not part of String class. defined for other classes to convert their objects to strings. String theString; int someInt = 4; theString = Integer.toString(someInt); String anotherString; double someDouble = 8.25; anotherString = Double.toString(someDouble);

A literal string is an unnamed object, or anonymous object

of the String class.

endsWith() and startsWith()

returns t/f if String does or not end/start with specific arg myName = "Stacy"; myName.startsWith("sta") is false.

parseInt() is a

static method because you use it with an object that has the same name as the class from which it was created."

The toString() method you use with println()

takes arguments of any primitive type, including int, char, double, and so on, it is a working example of polymorphism.

When you declare a String object,

the String itself—that is, the series of characters contained in the String—is distinct from the identifier you use to refer to it.

String class equalsIgnoreCase()

this method ignores case when determining if two Strings are equivalent

String is a class, and each

created String is an object. A String variable name is a reference to a location in memory, rather than to a particular value.

Comparisons are made using each character's Unicode value

Character comparisons are evaluated alphabetically

String firstString = "abc"; String secondString = "abc"; if(firstString == secondString) System.out.println("Strings are the same");

If you declare two String objects and initialize both to the same value, the value is stored only once in memory and the two object references hold the same memory address. Because the data that comprises the String is stored just once, memory is saved. Consider the following example in which the same value is assigned to two Strings (as opposed to getting one from user input). The reason for the output in the following example is misleading. When you write the following code, the output is Strings are the same. The output is Strings are the same because the memory addresses held by firstString and secondString are the same, not because their contents are the same.

The reason why the above problem occurs

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. Therefore, when you compare two String objects using the == operator, you are not comparing their values, but their computer memory locations.

You can create a String object by using the keyword new and the String constructor, just as you would create an object of any other type.

String aGreeting = new String("Hello"); aGreeting: stores a reference to a String object—it keeps track of where the String object is stored in memory. Bc Strings are declared so routinely in programs, Java provides a shortcut, String aGreeting = "Hello";

regionMatches()

String firstString = "abcde"; String secondString = "xxbcdef"; firstString.regionMatches(1, secondString, 2, 4) is true Position 1 in firstString is "bcde" and the four-character substring starting at position 2 in secondString is also "bcde". Another way String thirdString = "123 Maple Drive"; String fourthString = "a maple tree"; thirdString.regionMatches(true, 4, fourthString, 2, 5) The substring of thirdString that starts at position 4 and continues for five characters is "Maple", the substring of fourthString that starts at position 2 and continues for five characters is "maple", and the argument that ignores case has been set to true:

charAt()

String name = "Stacy"; name.charAt(0); return S An error occurs if you use an argument that is negative, or greater than or equal to the length of the calling String.

Empty vs null

String word1 = ""; - Empty refers to a memory address where no characters are stored. String word2 = null; String word3; - Does not hold a memory address String word2 and word3 can't be used with the String methods like equals() and compareTo()

Declare a String, such as String aGreeting = "Hello";

aGreeting does not hold the characters that make up Hello; instead it holds a memory address where the characters are stored. If aGreeting happens to be stored at memory address 10876 and the String Hello happens to be stored at memory address 26040. You cannot choose the memory address where a value is stored; addresses such as 10876 and 26040 are chosen by the operating system.

Length

accessor method that returns the length of a String int len = greeting.length();

Strings

and other objects that can't be changed are immutable.

Literal string/ string literal

another name for string something in " "

Examples on how to use character class

char aChar = 'C'; Character.isUpperCase(aChar); Each of the Character class methods used is a static method because the method name is used without an object reference—you use only the class name, a dot, and the method name. Though the Character class name is used in this context it is the name of the object made from the Character class.

String class compareTo()

• returns zero if the values of two Strings are exactly the same • returns a negative number if the calling object is "less than" the argument • returns a positive number if the calling object is "more than" the argument compares based on unicode values where a < b < c aName = "Roger" aName.compareTo("Robert") returns a 5 Roger > Robert bc R = R O = O g > b: 7 - 2 = 5 which is why 5 is returned a = 1 b = 2 c = 3 d = 4 e = 5 f = 6 g = 7 I gave the letters random value in this case in this example please look at ASCII table for true value of the letters.


Ensembles d'études connexes

Chapter 1 - Art in the Stone Age

View Set

JCM 103 Semicolons, Colons, Dashes & Hyphens

View Set

MICRO FINAL EXAM PRACTICE chapters included in Quiz 2 (9, 10, 11, 12, 13, 14), and the materials covered thereafter (21, 15, 16, 17, 20, 26)

View Set

BB Transfusion Reactions Media lab

View Set

Real Estate Investment: Practice Exam

View Set