08slide Java基础教程好!.ppt
文本预览下载声明
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)
显示全部