Chapter 8 Characters and strings 2000 McGraw-Hl‖ Introduction to Object-Oriented Programming with Java-Wu Chapter 8-1
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 8 - 1 Chapter 8 Characters and Strings
Chapter 8 Objectives After, you have read and studied this chapter, you shoula be able to Declare and manipulate data of the char data type e Write string processing programs using String ang String Buffer objects. e Differentiate the string and String Buffer classes and use the correct class in solving a given task e Distinguish the primitive and reference data types and show how the memory allocation between the two is different e Tell the difference between equality and equivalence testings for String objects Show by using the state-of-memory diagrams how objects are passed to methods and returned from methods C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 8-2
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 8 - 2 Chapter 8 Objectives After you have read and studied this chapter, you should be able to Declare and manipulate data of the char data type. Write string processing programs using String and StringBuffer objects. Differentiate the String and StringBuffer classes and use the correct class in solving a given task. Distinguish the primitive and reference data types and show how the memory allocation between the two is different. Tell the difference between equality and equivalence testings for String objects. Show, by using the state-of-memory diagrams, how objects are passed to methods and returned from methods
Characters rIn Java single characters are represented using the data type char. Character constants are written as symbols enclosed in single quotes, for example, a, X and 5 rTo represent characters in computer, U. s computer manufacturers devised several coding schemes r One coding scheme widely used today is ASCII American Standard code for information Interchange) rTo accommodate the character symbols of non- English languages, the Unicode Consortium established the Unicode Worldwide character Standard, commonly known as Unicode C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 8-3
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 8 - 3 Characters In Java single characters are represented using the data type char. Character constants are written as symbols enclosed in single quotes, for example, 'a', 'X', and '5'. To represent characters in computer, U. S. computer manufacturers devised several coding schemes. One coding scheme widely used today is ASCII (American Standard Code for Information Interchange). To accommodate the character symbols of nonEnglish languages, the Unicode Consortium established the Unicode Worldwide Character Standard, commonly known as Unicode
ASCII Table 0 nul soh stx eot ac k bel b ht 10 If vt ff si dle dcl dc2 dc3 2 ak can ett b esc %& 40 ? @ A B C E For example character 'O'is G H K L M 79(row value T 70+ col value 9 80 79) d 1 k 120 del C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 8-4
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 8 - 4 ASCII Table For example, character 'O' is 79 (row value 70 + col value 9 = 79). O 9 70
Character processing char chl, ch2 Declaration and initialization messageBox. show("ASCII code of character x is+ (int) Type conversion message. show("Character with AsCII code 88 is+ between int and char. (char)88 This comparison returns true because ascll value of a is 65 while that of 'c' is 99 C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 8-5
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 8 - 5 Character Processing Declaration and initialization char ch1, ch2 = ‘X’; Type conversion between int and char. messageBox.show("ASCII code of character X is " + (int) 'X' ); message.show("Character with ASCII code 88 is " + (char)88 ); This comparison returns true because ASCII value of 'A' is 65 while that of 'c' is 99. ‘A’ < ‘c’
Strings rA string is a sequence of characters that is treated as a single value r The string data type is used to represent strings in Java r We have been using String objects all along. For example, to display a text with message Box, we write messageBox. show(Hello, how are you?) C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 8-6
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 8 - 6 Strings A string is a sequence of characters that is treated as a single value. The String data type is used to represent strings in Java. We have been using String objects all along. For example, to display a text with messageBox, we write messageBox.show( “Hello, how are you?” );
String is an object r String is a class in the java. lang package. Because String is a class we need to create an instance of String in Java for string processing like any other objects, we need a declaration and object creation for the instances of the String class. For example, String namely name1= new string(latte")i But we normally use a shorthand instead treating These two statements String objects much like primitive data. For example, are equivalent String ameli name1= latte C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 8-7
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 8 - 7 String is an Object String is a class in the java.lang package. Because String is a class, we need to create an instance of String in Java for string processing. Like any other objects, we need a declaration and object creation for the instances of the String class. For example, String name1; name1 = new String( “Latte” ); But we normally use a shorthand, instead, treating String objects much like primitive data. For example, String name1; name1 = “Latte”; These two statements are equivalent
Accessing Individual Elements r Individual characters in a String accessed with the charAt method String name Sumatra i 0 2 345 6 a a name name charAt(3 This variable refers to the The method returns the whole string character at position #3 C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 8-8
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 8 - 8 Accessing Individual Elements Individual characters in a String accessed with the charAt method. 0 1 2 3 4 5 6 S u m a t r a String name = “Sumatra”; name This variable refers to the whole string. name.charAt( 3 ) The method returns the character at position # 3
Determining the Size r We determine the number of characters in a String the length method String name =Sumatra str1 one/ str2= str3 namelength()i strl length()i 3 Error because no str2 length()i 0 object is created for str3. so it is a null str3 length()i Error C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 8-9
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 8 - 9 Determining the Size We determine the number of characters in a String with the length method. String name = “Sumatra”, str1 = “one”, str2 = “”, str3; Error because no object is created for str3, so it is a null. name.length( ); str1.length( ); str2.length( ); str3.length( ); 7 3 0 Error!
Example: Counting Vowels char letter string name inputBox getstring ("What is your name? nt numberofCharacters name. lengthoi int yowelCount for (int 1=0; i< numberofCharacters; i++)( Here' s the code to count the number of letter name charAt(i) vowels in the input string if letter a letter letter = 'elI letter EI letter =s 'i II letter letter == 'o lett letter = uIl letter U vowelCount++ messageBox. show(name " your name has " t vowelCount vowels" C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 8-10
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 8 - 10 Example: Counting Vowels char letter; String name = inputBox.getString("What is your name?"); int numberOfCharacters = name.length(); int vowelCount = 0; for (int i = 0; i < numberOfCharacters; i++) { letter = name.charAt(i); if ( letter == 'a' || letter == 'A' || letter == 'e' || letter == 'E' || letter == 'i' || letter == 'I' || letter == 'o' || letter == 'O' || letter == 'u' || letter == 'U' ) { vowelCount++; } } messageBox.show(name + ", your name has " + vowelCount + " vowels"); Here’s the code to count the number of vowels in the input string