1 string class the primitive data types provide only the char for dealing with alpha-numeric data
TRANSCRIPT
![Page 1: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/1.jpg)
1
String Class
The primitive data types provide only the char for dealing with alpha-numeric data.
![Page 2: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/2.jpg)
2
String Class
The problem arises when we are required to maintain and process data that, by its nature, exceeds a single character
![Page 3: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/3.jpg)
3
String Class
One Way to resolve this problem is to create our own string of characters
![Page 4: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/4.jpg)
4
String Class
While this is within our ability, maintaining such structures would be burdensome
We can quickly see that every developer would begin to write their own solutions to handle a series of related characters
![Page 5: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/5.jpg)
5
String Class
Well, we have:
A common problem in handling an array/string of characters
A set of generic functions that will be used to manipulate these characters…
![Page 6: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/6.jpg)
6
String Class
What can be done to provide a practical solution ?
Bundle all of these common functions that manipulate a string of related characters
Create a String class !!!
![Page 7: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/7.jpg)
7
String Class
Java provides two classes, the String and the StringBuffer to handle a series of characters or words
![Page 8: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/8.jpg)
8
Our Objective:
Examine Java’s String class and StringBuffer Class
We will work with Java Docs to understand the methods within each class
We will look at the actual Java code to analyze HOW the String methods are implemented
We will implement these classes in our projects
![Page 9: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/9.jpg)
9
What is Available OnLine:
Class Lecture NotesThis Powerpoint PresentationJava’s String class codeStringExamples.javaStringConstructors.javaStringBufferConstructors.javaStringBufferCapLen.javaStringBufferInsert.java
![Page 10: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/10.jpg)
10
The String and StringBuffer classes provide a means for maintaining alpha-numeric data such as words or other text.
![Page 11: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/11.jpg)
11
We will examine these classes and their methods. We will also look into the code for these classes to get a better idea of how to design and develop useful classes for ourselves.
![Page 12: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/12.jpg)
12
The String and StringBuffer Classes are defined in the following Java API:
java.lang.String
java.lang.StringBuffer
![Page 13: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/13.jpg)
13
Strings:
The Java String is a class and as such it has constructors and methods
Actually, the String class has 9 constructor methods !!!
![Page 14: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/14.jpg)
14
There are literal Strings like “Hello World” that Java knows how to handle and there are the + and += operators that can be used (in their overloaded state) to concatenate strings with numbers or with other strings
![Page 15: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/15.jpg)
15
We will learn how to:
Declare and use string literals
Instantiate Strings by way of the various class constructors
Convert strings to and from numbers
![Page 16: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/16.jpg)
16
Work with the immutable property of a String
Use Various String methods
![Page 17: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/17.jpg)
17
Literal Strings:
These kind of string objects are
not instantiated as the Java compiler simply treats them as a reference to a String object that is stored in the runtime stack.
They are really unreferenced variables
![Page 18: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/18.jpg)
18
Example:
// string literal using a method
int litLength = 0;
litLength = "My String Literal".length();
System.out.println("length of \"My String Literal\" is: " +
litLength); // Answer is 17
![Page 19: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/19.jpg)
19
You can also explicitly declare an instance of the String class (an object of):
// declare string literal
String myString = "Hello World";
![Page 20: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/20.jpg)
20
TPS
There are many ways to create an instance of a String
Write out as many different ways you can think of…
Lets look at Java Docs list of constructors…..\..\..\Desktop\JAVA DOCS.lnk
![Page 21: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/21.jpg)
21
TPSString MyString1 = new String();
String myString2 = "Hello World";
String myString3 = new String("Hello Wabbit") ;
String myString4 = new String(myString2);
![Page 22: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/22.jpg)
22
String’s Immutability:
We can initialize a String object / variable using a literal or by a using the return of a String method
For example, we can initialize an empty string one of 2 ways:
String myString = “ “ ; // instantiates a //string object with no value, empty string
![Page 23: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/23.jpg)
23
String myString = new String(); // instantiates a string object with no // value, empty string
An uninitialized String:
String myString; // myString is set to null
NOTE: null and empty string are very different !!!
![Page 24: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/24.jpg)
24
You may execute String methods on an EMPTY string but if you attempt to Execute them against a NULL String, you will get a runtime error (NULL OBJECT REFERENCE)
Another way to initialize a String:
String myString = new String(“Hello Wabbit”) ; // invokes string constructor
![Page 25: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/25.jpg)
25
A String, once constructed, can not be modified (there are no modifier methods, sets, in the String class there are only accessor, gets, methods)
NONE of the String methods MODIFY the private attributes of the String !!!
This means that the String class is immutable
![Page 26: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/26.jpg)
26
The only way to “change” a String is to change the REFERENCE of the string with a new series of characters
Example:
String myString3 = new String("The Jets Stink !!!");
System.out.println(myString3);
myString3 = ("The Giants Rule !!!");
System.out.println(myString3);
![Page 27: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/27.jpg)
27
The place in memory, reference point associated with the variable, String instance/ object, has been modified to point to the new String “The Giants Rule !!!”
Java’s garbage collector ( explicitly called with System.gc(0); ) will destroy the old literal “The Jets Stink !!!”
![Page 28: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/28.jpg)
28
You can (copy constructor) have ONE String variable (object) COPIED into another variable:
// have 1 string get a COPY of another string
// different references or memory locations
String myString4 = new String(myString3);
System.out.println(myString4);
myString4 = ("Changed MyString4");
// now strings 3 and 4 are different
System.out.println(myString4);
System.out.println(myString3);
![Page 29: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/29.jpg)
29
These variables have two different memory locations or references
You can have TWO String variables (objects) refer to the same string object:
![Page 30: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/30.jpg)
30
// have 2 strings REFER to the same String //object reference
// SHARED reference or memory location
String myString5 = myString4;
System.out.println(myString5);
myString5 = (“Yankees Stink");
// now strings 4 and 5 refer to different // references
System.out.println(myString5);
System.out.println(myString4);
![Page 31: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/31.jpg)
31
String Class Methods:
Review the Java API java.lang.String to review the various constructors and methods
We will review some of the more common String methods but is up to the Student to understand and get familiar with the other methods as you are responsible for knowing about and how to use ANY published method !!!
![Page 32: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/32.jpg)
32
TPS
Using the StringExamples.Java as a guide, create a program that uses the String class
Create String class objects using the different methods
Copy 1 String to another, print out the values
Then, look through the Java Doc and work with some of the other methods
![Page 33: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/33.jpg)
33
String Class Methods:
We will also examine the actual Java String class code to see how some of these methods are implemented
We will look at a few of the String class constructors
Lets look at the String Class Subset (handout) and review how some of the String methods are written…
![Page 34: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/34.jpg)
34
Common Methods:
length()
charAt()
substring()
concat()
![Page 35: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/35.jpg)
35
Common Methods:
compareTo()
compareToIgnoreCase()
equals()
equalsIgnoreCase()
![Page 36: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/36.jpg)
36
Common Methods:
indexOf()
lastIndexOf()
trim()
replace()
![Page 37: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/37.jpg)
37
Common Methods:
toUpperCase()
toLowerCase()
![Page 38: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/38.jpg)
38
length( ) returns the number of characters in the string
System.out.println("The length of myString5 is: " +
myString5.length()); // ANS 13
The string is “Yankees Stink”
![Page 39: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/39.jpg)
39
charAt( ) Returns a character of a String
// length minus 2 gets next to last char as // "charat" starts at element ZERO and // NOT 1
char c = myString5.charAt(myString5.length() - 2);
System.out.println(" the next to last character is: " + c); //n
The last example returns ‘n’ and NOT ‘i’ because charAt(0) starts at position ZERO And NOT ONE
![Page 40: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/40.jpg)
40
If we were to just say:
char c = myString5.charAt (myString5.length());
We would get an IndexOutOfBounds exception error (more on exception handling in a later Lecture)
![Page 41: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/41.jpg)
41
substring( ) overloaded methods returns portions of a string (as a string)
// substring
String myString6 = myString5.substring(3);
System.out.println(myString6);
// kees Stink
![Page 42: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/42.jpg)
42
myString6 = myString5.substring(8, 12); // [from, to) positions
// to position is EXCLUSIVE
System.out.println(myString6);
// Stin
"hamburger".substring(4, 8) returns "urge" "smiles".substring(1, 5) returns "mile"
Note in the second substring method, the from position is INCLUSIVE and the to position is EXCLUSIVE (as it represents the position FOLLOWING the substring)
![Page 43: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/43.jpg)
43
TPSWork with the string methods just reviewed
Length charAt substring
Ask the user for a wordDisplay the length of the wordDisplay the middlemost character of the
wordSplit the word into 2 separate strings and
display each new string
![Page 44: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/44.jpg)
44
Concatenation:
The concat( ) method snaps together 2 strings and works exactly the same way as the overloaded operator+ method
![Page 45: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/45.jpg)
45
// concat
String myString7 = "Millburn ";
String myString8 = "High School";
String myString9 = myString7.concat(myString8);
System.out.println(myString9); // Millburn High School
String myString10 = myString7 + myString8;
System.out.println(myString10); //Millburn High School
![Page 46: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/46.jpg)
46
Both produce the same results
You can also use the operator+= overloaded operator, however it is MOST inefficient
String S = “2*2”;
S += “=4”;S is now “2*2 = 4” however it is inefficient
because the RVALUE is a temp string that is used to reassign variable S’s reference point to now refer to “2*2=4”
![Page 47: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/47.jpg)
47
You can also concatenate Strings and numbers as long as Java understands you are working with strings
String S = “Year “;
S += 2002;
Results in S being = to “Year 2002”
![Page 48: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/48.jpg)
48
HOWEVER, if you write:
String S = “Year “;
S += ‘ ‘ + 2003;
The compiler thinks you are performing math on ‘ ‘ and 2003 and will in effect perform S += (‘ ‘ + 2003) which will find the ASCII or Unicode for a space, which is 32, and add that to 2003 giving S the value “Year 2035”
![Page 49: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/49.jpg)
49
If you had modified the code to concatenate a space AS A STRING
S += “ “ + 2003;Then you would get the intended result as “
“ is understood as a string
IMPORTANT NOTE: String concatenation with + (operator+) actually invokes the toString( ) method for the particular class and converts any numbers to Strings
![Page 50: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/50.jpg)
50
Finding characters and substrings:
indexOf( char c) returns the position of the first occurance of the character c in the string
Remember that indicies begin at ZERO
There are several overloaded indexOf methods
A NOT FOUND condition results in a –1
![Page 51: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/51.jpg)
51
Examples:
int pos = myString7.indexOf('b');
// Millburn
System.out.println(pos); // 4
int pos = myString7.indexOf('x');
System.out.println(pos); // -1
You can also start searching form any point within the string
![Page 52: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/52.jpg)
52
Example:
pos = myString8.indexOf('h', 4);
// High School
System.out.println(pos);
// 7
Here, you search for an ‘h’ but start your search after element 4
![Page 53: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/53.jpg)
53
You can perform a search starting at the end of the string
Example:
// High School
pos = myString8.lastIndexOf('h', 8);
System.out.println(pos); // 7
pos = myString8.lastIndexOf('h', 2);
System.out.println(pos); // -1
![Page 54: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/54.jpg)
54
Here, the second parameter is the STARTING POINT from the END of the string
There methods are also overloaded to allow a search for a specific SUBSTRING
The methods are similar except that instead of a character variable or literal you add in a string literal or a String variable
![Page 55: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/55.jpg)
55
int pos = myString7.indexOf('bur');
// Millburn
System.out.println(pos); // 4
pos = myString8.indexOf('hoo', 4);
// High School
System.out.println(pos); // 7
String myStr = “ch”
pos = myString8.lastIndexOf(mySr, 8);
System.out.println(pos); // 6
![Page 56: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/56.jpg)
56
TPSWork with the string methods just
reviewed concat += + indexOf
Read in a series of words from a file (4 or 5 words)
Add these words to a single stringAsk the user to search for a character and
display each position the character is found
![Page 57: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/57.jpg)
57
Comparisons:
You CAN NOT use relational operators ( == != >= , etc) to compare Strings (unlike C++ which overloads those operators o work with strings, Java however does not allow for operators to be overloaded)
![Page 58: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/58.jpg)
58
Comparisons:
AS previously discussed, these operators, when applied against objects, compare REFERENCES to those objects (their memory address) and NOT THEIR STATE (values of their attributes)
![Page 59: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/59.jpg)
59
Comparisons:
Relational Operators work on primitive data types because there they compare the VALUES in these variables
![Page 60: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/60.jpg)
60
Comparisons:
To compare the STATE of a String, use the following methods:
equals( )
equalsIgnoreCase()
compareTo()
![Page 61: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/61.jpg)
61
Comparisons:
equals() and equalsIgnoreCase() are boolean methods and return simply TRUE or FALSE
True if the strings have the same length and characters (case matters) otherwise it returns false
![Page 62: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/62.jpg)
62
Examples:
boolean TF = myString7.equals(myString8);
System.out.println(TF); // FALSE
TF = myString7.equals("MIllburn ");
System.out.println(TF); // FALSE
TF = mStr.equalsIgnoreCase("Millburn ");
System.out.println(TF); // TRUE
![Page 63: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/63.jpg)
63
What will happen here:
String s;
Boolean TF = s.equals(“OK”);
![Page 64: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/64.jpg)
64
What will happen here:
String s;
Boolean TF = s.equals(“OK”);
Answer is a runtime error as s has yet to be instantiated, so it is NULL Object Reference
![Page 65: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/65.jpg)
65
One method for first checking to see if an object is indeed instantiated (resident in memory) you can do the following:
Boolean TF = ( s != null && s.equals(“OK”));
![Page 66: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/66.jpg)
66
I guess I lied when I said that you can not use relational operators
You can but they only EVALUATE THE REFERENCE (memory)
Here we are first checking to see if the string object s is resident, if it is not then the first part of the if is FALSE, and because of short circuit evaluation, the second part of the if is NEVER evaluated
Cool, huh !!!
![Page 67: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/67.jpg)
67
CompareTo() returns an integer that describes the RESULT of the comparison
S1.compareTo(s2) RETURNS:
A negative integer if s1 PRECEEDES s2
ZERO if they are equalPositive integer if s1 POSTCEDES s2
(comes later)
![Page 68: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/68.jpg)
68
He comparison begins at the first character and proceeds LEFT to RIGHT until different characters are encountered in corresponding positions OR until one of the strings end
When a difference occurs, the method returns the difference of the Unicodes (ASCII values) of the different characters
![Page 69: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/69.jpg)
69
The string with the “SMALLER” character , the one with he lower ASCII code , is deemed smaller
With strings of different lengths , the method returns the difference in lengths, so he shorter string is deemed SMALLER
Remember that UPPER CASE letters come BEFORE lower case letters
![Page 70: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/70.jpg)
70
This process is called LEXICOGRAPHIC ORDERING
![Page 71: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/71.jpg)
71
Examples:
String s = "ABC";
int result = s.compareTo("abc");
System.out.println(result);
![Page 72: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/72.jpg)
72
Examples:
String s = "ABC";
int result = s.compareTo("abc");
System.out.println(result);
//NEGATIVE 32, ABC smaller than abc
result = s.compareTo("ABCD");
System.out.println(result);
![Page 73: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/73.jpg)
73
Examples:
result = s.compareTo("ABCD");
System.out.println(result);
//NEGATIVE 1, ABC smaller than ABCD
result = s.compareTo("Amuh");
System.out.println(result);
![Page 74: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/74.jpg)
74
Examples:
result = s.compareTo("Amuh");
System.out.println(result);
//NEGATIVE 43
s = "adset";
result = s.compareTo("ADSET");
System.out.println(result);
![Page 75: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/75.jpg)
75
Examples:
s = "adset";
result = s.compareTo("ADSET");
System.out.println(result);
//POSITIVE 32, adset GT ADSET
result = s.compareTo("aDsetf");
System.out.println(result);
![Page 76: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/76.jpg)
76
Examples:
result = s.compareTo("aDsetf");
System.out.println(result);
//NEGATIVE 32
![Page 77: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/77.jpg)
77
There is also a compareToIgnoreCase( ) that works in the same manner except it ignores case differences
![Page 78: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/78.jpg)
78
Remember that Strings are OBJECTS and their variable names are REFERENCES to those objects
Therefore using the == to compare Strings will merely compare their REFERENCES
![Page 79: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/79.jpg)
79
String i = new String("21");String y = new String("21");
// compares REFERENCESif (i == y)
System.out.println("The Strings are EQUAL !!!"); else System.out.println("The Strings are NOT EQUAL !!!");
// compares STATE if (i.equals(y))
System.out.println("The Strings are EQUAL !!!"); else System.out.println("The Strings are NOT EQUAL !!!");
![Page 80: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/80.jpg)
80
String i = new String("21");String y = new String("21");
// compares REFERENCESi = y;if (i == y)
System.out.println("The Strings are EQUAL !!!");
else System.out.println("The Strings are
NOT EQUAL !!!");
RETURNS TRUE
![Page 81: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/81.jpg)
81
TPS
Try this out by writing code that evaluates 2 strings for equality using the = = and then usin g the String methods
See that = = compares references and the String methods compare STATE
![Page 82: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/82.jpg)
82
Conversions:toUpperCase() returns a new string in
all UPPER CASE
toLowerCase() returns a new string in all lower case
replace(c1, c2) builds and returns a new string in which all occurances of rthe character c1 are REPLACED with c2
![Page 83: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/83.jpg)
83
Conversions:
trim() builds and returns a new string in which all the “whitespace” characters (spaces, tabs, newline) are trimmed from the beginning and the end of the string
![Page 84: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/84.jpg)
84
Examples:
String s1 = “ modify This onE “
String s2;
s2 = s1.trim();
s2 = s1.toUpperCase();
![Page 85: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/85.jpg)
85
Examples:
String s1 = “ modify This onE “
String s2;
s2 = s1.trim();
// s2 = “modify This onE”
s2 = s1.toUpperCase();
// s2 = “ MODIFY THIS ONE “
![Page 86: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/86.jpg)
86
Examples:
String s1 = “ modify This onE “
String s2;
s2 = s1.toLowerCase();
s2 = s1.replace(‘o’, ‘x’);
![Page 87: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/87.jpg)
87
Examples:
String s1 = “ modify This onE “
String s2;
s2 = s1.toLowerCase();
// s2 = “ modify this one “
s2 = s1.replace(‘o’, ‘x’);
// s2 = “ mxdify This xnE “
![Page 88: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/88.jpg)
88
Remember that String s1 REMAINS UNCHANGED !!!
What does this code do ?
S1.trim();
![Page 89: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/89.jpg)
89
Remember that String s1 REMAINS UNCHANGED !!!
What does this code do ?
S1.trim();
// ANSWER is nothing as there is no placeholder, string, to accept the returned string from the trim method
See handouts 1 and 2 for Code examples using String
![Page 90: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/90.jpg)
90
TPSWork with the string methods just
reviewed trim toUpperCase toLowerCase replace
Read in a series of words from a file (4 or 5 words)
Remove whitespaceConvert every other word to UC & LCReplace all vowels with X
![Page 91: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/91.jpg)
91
Converting Strings to Numbers and Numbers into Strings:
There are 2 ways to convert an int into a String of digits
(including a minus sign iff)
![Page 92: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/92.jpg)
92
1. use the static method (remember what a static method is/ means ?) toString() of the Integer class(as mentioned before, we will discuss the Integer as well as the ofther Number classes in the second semester)
int n = -123;
String s1 = Integer.toString(n);
// s1 now has the value “-123”
![Page 93: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/93.jpg)
93
Now, notice that we did not create an instance of the Integer class !
Well, I thought that in order to work with the methods of a class you have to create an instance of that class ?
![Page 94: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/94.jpg)
94
The answer, obviously, is that Integer’s toString() method is a STATIC method
Static methods can be executed without having an instance (object) of that class declared as there is only one version of the static method for ANY instance of the particular class
![Page 95: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/95.jpg)
95
Remember, however, that STATIC mathods MAY NOT CALL non Static Methods as non Static methods are tied to an instance of the class (object)
![Page 96: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/96.jpg)
96
The Integer class (called a wrapper class as it “wraps” around the int primitive data type) is part of the java.lang package that is AUTOMATICALLY imported into all programs
![Page 97: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/97.jpg)
97
2. Use the Static method of the String class valueOf()
int n = -123;
String s2 = String.valueOf(n);
// s2 now has the value “-123”
![Page 98: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/98.jpg)
98
Even though we said there are 2 ways, can you think of a third ?
![Page 99: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/99.jpg)
99
Even though we said there are 2 ways, can you think of a third ?
How about using concatenation !!!
int n = -123;
String s2 = “” + n;
// s2 now has the value “-123”
![Page 100: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/100.jpg)
100
You can perform the same type of operations on doubles (there is also a Double wrapper class)
double n = 123.5;
String s1 = Double.toString(n);
// s1 now has the value “123.5”
![Page 101: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/101.jpg)
101
However, with doubles the resulting String value may not display cleanly, depending on the value in the double
To properly present the double in a String use a specified FORMAT
There is a class called DecimalFormat
![Page 102: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/102.jpg)
102
Create an instance of this class that describes the desired format
Then use that format for the conversion into a string
Example:
![Page 103: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/103.jpg)
103
import java.text.DecimalFormat;
DecimalFormat moneyFormat = new DecimalFormat("0.00");
double totalSales = 123.5;
String s1 = moneyFormat.format(totalSales);
System.out.println("total Sales Are $" + s1); // 123.50
![Page 104: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/104.jpg)
104
Converting a String into an Integer can be accomplished by using the parseInt( ) method of the Integer class
Example:
String s2 = “-123”;
int n = Integer.parseInt(s2);
// n = -123
![Page 105: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/105.jpg)
105
The same works for doubles
String s2 = “123.56”;
double n = Double.parseDouble(s2);
// n = 123.56
These methods throw NumberFormatExceprion error if the string is not in the proper format
![Page 106: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/106.jpg)
106
TPSWork with the various ways to convert a
number into a string
Then try converting a string into a number
Work with ALL the different ways to accomplish this
![Page 107: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/107.jpg)
107
Character Methods:
These methods can help to identify a specific character (in a String) is a digit, a letter or something else
The Character wrapper has several static boolean methods
They all take a char as an argument and return TRUE or FALSE
![Page 108: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/108.jpg)
108
Example:
Char c = ‘2’;
Boolean anwr = Character.isDigit( c );
char c1 = Character.toUpperCase(‘a’);
char c2 = Character.toLowerCase(‘A’);
![Page 109: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/109.jpg)
109
Char c = ‘2’;
Boolean anwr = Character.isDigit( c );
// TRUE
char c1 = Character.toUpperCase(‘a’); // c1 = ‘A’
char c2 = Character.toLowerCase(‘A’);
// c1 = ‘a’
![Page 110: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/110.jpg)
110
Java API String Search:
TPS Find the method that compares PORTIONS of 2 string objects for equality
![Page 111: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/111.jpg)
111
Java API String Search:
TPS Find the method that compares PORTIONS of 2 string objects for equality
ANS regionMatches( )
![Page 112: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/112.jpg)
112
S3.regionMatches( 0, s4, 0 ,5);
0 is the starting index of the string s3 s4 is the comparison string 0 the starting index of the
comparison string s4 5 is the number of characters to
compare between the 2 strings Returns TRUE or FALSE
![Page 113: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/113.jpg)
113
S3.regionMatches(true, 0, s4, 0 ,5);
Overloaded method, first argument, if true IGNORES case
![Page 114: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/114.jpg)
114
TPS Find the method(s) that tests a string to see if it starts with or ends with a particular set of characters
![Page 115: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/115.jpg)
115
TPS Find the method(s) that tests a string to see if it starts with or ends with a particular set of characters
ANS startsWith( ) endsWith( )
![Page 116: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/116.jpg)
116
String s3 = “started”;
s3.startsWith(“st”);
If s3 starts with the letters “st” returns TRUE
![Page 117: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/117.jpg)
117
s3.startsWith(“st”, 2);
If s3 starts AT INDEX 2 (3rd element) with the letters “st” returns TRUE
S3.endsWith(“ed”);
If s3 ends with the letters “ed” returns TRUE
![Page 118: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/118.jpg)
118
StringBuffer Class:
Unlike the Sring class, the StringBuffer class in NOT immutable as there are modifier, set, methods that allow you to alter the private attributes (state) of an instance of the SringBuffer class
StringBuffer attributes’ STATE is dynamic
![Page 119: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/119.jpg)
119
StringBuffer stores the characters of the string along with a capacity
This class has THREE constructors
B1 = new StringBuffer(); empty string with a default capacity of 16 characters
![Page 120: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/120.jpg)
120
B1 = new StringBuffer( 10 ); empty string with an initial capacity of 10 characters
![Page 121: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/121.jpg)
121
Example:
StringBuffer mySB = new StringBuffer(“Kerry Collins Rules”);
char c = mySB.charAt(0);
mySB = setCharAt(0, Chatacter.toLower( c ) );
mySB is now = kerry Collins
![Page 122: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/122.jpg)
122
Unlike the String class , which would have necessitated a NEW reference, StringBuffer maintains the original reference and only 1 character is modified
More efficient to use than String class when you will be constantly modifying the state of a “string”
![Page 123: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/123.jpg)
123
Some Methods of this class are :
length( )capacity( )setLength( )ensureCapacity( )charAt( )
![Page 124: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/124.jpg)
124
Some Methods of this class are :
setCharAt( )getChars( )reverse( )append( )insert( )delete( )
![Page 125: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/125.jpg)
125
StringBuffer sb = new StringBuffer(“Hello there”);
sb.charAt(4);
sb.setCharAt(6, ‘T’);
![Page 126: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/126.jpg)
126
StringBuffer sb = new StringBuffer(“Hello there”);
sb.charAt(4);
// returns the character at the index specified ANS = ‘o’
sb.setCharAt(6, ‘T’);
// sets the char at the index sb now = “Hello There”
![Page 127: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/127.jpg)
127
int a = 7;
sb.append(“ “);
sb.append(a);
![Page 128: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/128.jpg)
128
int a = 7;
sb.append(“ “);
sb.append(a);
// sb now = “Hello There 7”
![Page 129: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/129.jpg)
129
See handouts 3, 4, and 5 for Code examples using StringBuffer
Review the Java API java.lang.StringBuffer to review the various constructors and methods
![Page 130: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/130.jpg)
130
Projects...
Backwords
Letters and Digits
Occurrences
Name Flip
Encode / Decode
Palindrome
Count Words
Dictionary
Find Names
GREP
![Page 131: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/131.jpg)
131
...Projects:
Text Analysis
Using Classes
Add A StringBuffer vs String project to examine efficiency
NOTE: Read ALL projects, find common elements, plan, create a CLASS of common methods !!!
![Page 132: 1 String Class The primitive data types provide only the char for dealing with alpha-numeric data](https://reader035.vdocuments.us/reader035/viewer/2022062517/56649efd5503460f94c11af3/html5/thumbnails/132.jpg)
132
TEST IS THE DAY AFTER THE PROJECT IS
DUE !!!