CS2_19_Strings

CS2_19_Strings - CS2 Module 19 Category: OO Concepts Topic:...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: CS2 Module 19 Category: OO Concepts Topic: Strings Objectives Complete treatment of Strings including methods CS 2 Introduction to Object Oriented Programming Module 19 OO Concepts Strings String-Fu Every String is an instance of Javas built-in class String . Thus, Strings are objects. Java provides extra support for Strings for convenience since Strings are frequently used (hence they can seem like primitives but they are not) Three obvious String support features: 1. You need not explicitly instantiate Strings with the new keyword . 2. The + operater overloaded for Strings, to support concatenation. 3. There are several predefined methods provided in built-in class String. Strings vis-a-vis Objects Strings vis-a-vis Objects Java automatically instantiates a String object when it encounters a text string within double-quotes. For example . . . String Literals as String Objects String Literals as String Objects Assignment of String references to String Objects: Code : Memory: String str1; Box box1; str1 box1 str2 = Glib Folksies; str2 Glib Folksies ?default? str1 = Hello World; box1 = new Box(length,width,height); Hello World str1 box1 10 20 40 str1 = Hello World; box1 = length, width, height; Hello World str1 box1 ERROR: must use new new and call constructor str2 ?default? str2 = new String(); Strings as Objects Given: String s = new String(); How can we be sure of the default behavior? Remember that thing - the API? Download it! ( http://www.javasoft.com/docs ) 1. Go to class String 2. Click on constructors 3. Look for the default one and read its documentation 4. Mystery solved! Now back to the show.. Crucially Important Digression Again, Java automatically creates a new String object whenever it encounters text within double-quote marks. Thus, the code: String str1 = Hello World; accomplishes four things: 1. It creates str1 as a reference to a String. 2. It creates an instance of a String. 3. It initializes that instance to Hello World. 4. Lastly it associates the reference str1 with that Hello World object. This is inconsistent with how Java treats standard objects. With standard objects, you must explicitly: instantiate (via new ), and initialize (via a constructor). Then associate with a reference if desired. Strings vis-a-vis Objects Basically equivalent to String str1 = new String(Hello World); Three obvious String support features: 1. You need not explicitly instantiate Strings . 2. The + operator is overloaded for Strings, for concatenation, e.g., System.out.println(This string is an example of + one that is too long to fit on one line. Your TAs take off points + for lines that exceed 80 column characters.); 3. Several predefined methods provided in built-in class String....
View Full Document

Page1 / 46

CS2_19_Strings - CS2 Module 19 Category: OO Concepts Topic:...

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