- 1、本文档共66页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 Objectives To use the String class to process fixed strings To use the Character class to process a single character To use the StringBuffer class to process flexible strings To learn how to pass strings to the main method from the command line To discover file properties, delete and rename files using the File class To write data to a file using the PrintWriter class To read data from a file using the Scanner class Introduction(P262 8.1) String are used often in programming. A String is a sequence of characters. In many language ,strings are treated as array of characters, but in Java a string is an Object. Strings Are Immutable(8.2.2) A String object is immutable; its contents cannot be changed. Does the following code change the contents of the string? String s = Java; s = s + HTML; Construct a String (1) To creating a string from a string literal (2) Since strings are used frequently, Java provides a shorthand initializer for creating a string: (3) Create a string from an array of characters Interned Strings String s1 = new String(welcome to java); String s2 = new String(welcome to java); System.out.println(s1==s2); Interned Strings(8.2.2) Since strings are immutable and are frequently used, to improve efficiency and save memory, the JVM uses a unique instance for string literals with the same character sequence. Such an instance is called interned. You can also use a String object’s intern method to return an interned string. Examples(8.2.2) Display: s1 == s is false s2 == s is true s == s3 is true Finding String Length(8.2.4) Definition: public int length() Retrieving Individual Character(8.2.4) Definition : public char charAt(int?index) Exercise 1 输入任意字符串,将其反向输出 Scanner input = new Scanner(System.in); String s = input.next(); 反向输出s Exercise 2(P270 listing8.1) 判断一个字符串是不是回文(palindrome)
文档评论(0)