lecture16-String+IO

Lecture16-String IO - Lecture 16 Objects String File Lecture 16 Objects String File CS 170 Section 000 27 October 2009 October 2009 CS170 Section

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
cture 16: Objects: String File Lecture 16: Objects: String, File CS 170, Section 000 7 October 2009 27 October 2009 10/27/2009 CS170, Section 000, Fall 2009 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Lecture Plan ± More on the String class ¾ Gentle introduction to objects ¾ Chapter 8 (cont’d): File Class (another object) les directories browsing ¾ Files, directories browsing ¾ Text input/output ¾ Homework 5: Breaking the Caesar cipher ¾ Due Wednesday, Nov 4 th (new date) CS170, Section 000, Fall 2009 2
Background image of page 2
The String Class: Ch 8.1-8.4 Constructing a String Object String message2 = new String("Welcome to Java“); String s1 = new String(); nce strings are used frequently Java Since strings are used frequently, Java provides a shorthand initializer for creating a ring: string: String message1 = "Welcome to Java“; tring s2 “” 3 String s2 = “”;
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Strings Are Immutable A String object is immutable; its contents nnot be changed cannot be changed Does the following code change the contents of the string? String s = "Java"; s = "HTML"; 4
Background image of page 4
Trace Code String s = "Java"; s = "HTML"; After executing String s = "Java"; After executing s = "HTML"; : String String object for "Java" s : String String object for "Java" This string object is now unreferenced s : String String object for "HTML" Contents cannot be changed 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Trace Code String s = "Java"; s = "HTML"; After executing String s = "Java"; After executing s = "HTML"; : String String object for "Java" s : String String object for "Java" This string object is now unreferenced s : String String object for "HTML" Contents cannot be changed 6
Background image of page 6
String Comparisons java.lang.String equals( S tring):boolean +equals(s1: String): boolean +equalsIgnoreCase(s1: String): boolean +compareTo(s1: String): int Returns true if this string is equal to string s1. Returns true if this string is equal to string s1 case- insensitive. eturns an integer reater than 0, equal to 0, or less than 0 +compareToIgnoreCase(s1: String): t Returns an integer greater than 0, equal to 0, or less than 0 to indicate whether this string is greater than, equal to, or less than s1. Same as compareTo except that the comparison is case- sensitive int +regionMatches(toffset: int, s1: String, offset: int, len: int): boolean +regionMatches(ignoreCase: boolean, insensitive. Returns true if the specified subregion of this string exactly matches the specified subregion in string s1. Same as the preceding method except that you can specify toffset: int, s1: String, offset: int, len: int): boolean +startsWith(prefix: String): boolean endsWith(suffix: String): boolean whether the match is case-sensitive. Returns true if this string starts with the specified prefix. t t i fth t i d ith th ifi d ffi 7 +endsWith(suffix: String): boolean Returns true if this string ends with the specified suffix.
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
String Comparisons equals String s1 = new String("Welcome“); tring s2 = new String(“welcome”); String s2 new String( welcome ); if (s1.equals(s2)){ // s1 and s2 have the same contents } if (s1 == s2) { / s1 and s2 have the same reference 8 } // s1 and s2 have the same reference
Background image of page 8
String Length, Characters, and Combining Strings java.lang.String l th() i t t th b f h t i th i ti +length(): int +charAt(index: int): char +concat(s1: String): String Returns the number of characters in this string.
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/03/2010 for the course CS CS 170 taught by Professor Eugeneagichtein during the Fall '09 term at Emory.

Page1 / 40

Lecture16-String IO - Lecture 16 Objects String File Lecture 16 Objects String File CS 170 Section 000 27 October 2009 October 2009 CS170 Section

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online