《JAVA OOP开发》英文版 Chapter 8 Characters and strings

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
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 《JAVA OOP开发》英文版 Chapter 7 Repetition Statements.ppt
- 《JAVA OOP开发》英文版 Chapter 6 Selection statements.ppt
- 《JAVA OOP开发》英文版 Chapter 5 Processing Input with Applets.ppt
- 《JAVA OOP开发》英文版 Chapter 4 Defining Instantiable Classes.ppt
- 《JAVA OOP开发》英文版 Chapter 3 Numerical Data.ppt
- 《JAVA OOP开发》英文版 Chapter 2 Java Programming Basics.ppt
- 《JAVA OOP开发》英文版 Chapter 1 Introduction to Object-oriented Programming and Software Development.ppt
- 《JAVA OOP开发》英文版 Introduction to Computers and Programming Languages.ppt
- 《Windows DNA应用程式》 面向对象分析与设计讲义.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第四章 第四讲 分支结构.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第十二章 文件.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第十一章 复杂数据类型及排序.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(教案讲义)第六讲 数组.doc
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第六讲 数组.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第七章(7-2)指针与指针变量.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第五章 循环结构.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第二章 数据类型与运算符.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(教案讲义)第二讲 数据类型与算术运算.doc
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(教案讲义)第九讲 函数.doc
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第九章 函数.ppt
- 《JAVA OOP开发》英文版 Chapter 9 objectives.ppt
- 《JAVA OOP开发》英文版 Chapter 10 Sorting and Searching.ppt
- 《JAVA OOP开发》英文版 Chapter 11 File Input and Output.ppt
- 《JAVA OOP开发》英文版 Chapter 12 Reusable classes and packages.ppt
- 《JAVA OOP开发》英文版 Chapter 13 GUI Objects and Event-Driven Programming.ppt
- 《JAVA OOP开发》英文版 Chapter 14 Inheritance and Polymorphism.ppt
- 《JAVA OOP开发》英文版 Chapter 15 Case Study Class Roster Maintenance program.ppt
- 《JAVA OOP开发》英文版 Chapter 16 Chapter 16 Recursive algorithms.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第一章 绪论.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第七章 牛行接与应用.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第三章 MCS-51单片机指令系统.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第九章 A/D、D/A转换接口.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第二章 MCS-51学机组成理.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第五章 输入/输出与中断.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第八章 并行接口与应用.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第六章 定时器/计数器及应用.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第十章 单片机应用系统设计与开发.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第四章 MCS-51单片机存储器的扩展.ppt
- 《网络安全设计》 第一章 安全设计简介.ppt
- 《网络安全设计》 第二章 创建网络安全计划.ppt