DOC PREVIEW
USF CS 112 - Strings

This preview shows page 1-2-3-25-26-27 out of 27 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 27 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 27 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 27 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 27 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 27 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 27 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 27 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

{small lecturenumber - heblocknumber :} Types in Javaaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Types in Javaaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Stringsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Stringsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} String Literalsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} String Literalsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Stack vs. Heap Iaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Immutable Stringsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Immutable Stringsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} ``Mutable'' Objectsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} ``Mutable'' Objectsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} ``Mutable'' Objectsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} ``Mutable'' Objectsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} ``Mutable'' Objectsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} ``Immutable'' Objectaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} ``Immutable'' Objectaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} ``Immutable'' Objectaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} ``Immutable'' Objectsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Back to Stringsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} String Methodsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} String Methodsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} String Methodsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} String Methodsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} String Methodsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} String Methodsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} String Methodsaddtocounter {blocknumber}{1}Intro to Computer Science IICS112-2012S-04StringsDavid GallesDepartment of Computer ScienceUniversity of San Francisco04-0: Types in JavaPrimative TypesHold simple valuesCan be stored on the stack(but can be stored on the heap if they areinstance variables in classes)integers: byte (8 bits), short (16 bit) int (32bit) long (64 bit)real numbers: float (32 bit) double (64 bit)boolean: true or false valuechar: single character (16 bit, unicode)in C, a char is 8 bits, uses ASCII04-1: Types in JavaObjectsCollection of data and methodsAlways stored on the heapPointer to object can be on the stackCreated with a call to “new”04-2: StringsStrings in Java are objectsContain both methods and dataData is the sequence of characters (type char)that make up the stringStrings have a whole bunch of methods forstring processing04-3: StringsStrings in Java are objectsStrings are stored on the heap, like all otherobjectsData is stored as an array of characters (moreon arrays next week. Similar to python lists)04-4: String LiteralsString s;s = "Dog";"Dog" is called a String LiteralAnything in quotation marks is a string literalSystem.out.println("Hello There")04-5: String LiteralsAny time there is a string literal in your code, thereis an implicit call to “new”A new string object is created on the heapData is filled in to match the characters in thestring literalPointer to that object is returnedString s;s = "MyString"; // Implicit call to new here!04-6: Stack vs. Heap Ipublic void foo(){int x = 99;char y = ’c’;String z = "c";String r = "cat";float w = 3.14;}04-7: Immutable StringsStrings are immutableOnce you create a string, you can’t change it.String s = "Car"; // Create a block of memory containing ’car’// Return a pointer to this block of memoryunknown.foo(s); // This function can’t mess with contents of sSystem.out.println(S); // s is guaranteed to be "Car" here04-8: Immutable StringsString objects are immutableOnce a string object is created, it can’t bechangedString variables can be changedCreate a new String object, assign it to thevariableString s = "dog";s = "cat";04-9: “Mutable” Objectspublic class ICanChange{private int x;public ICanChange(int initialX){this.x = initialX;}public int getX(){return this.x;}publc void setX(int newX){this.x = newX;}}04-10: “Mutable” ObjectsICanChange c = new ICanChange(4);c.setX(11); // Changed the value in object// c points toSystem.out.println(c.getX());Created an object of type ICanChangeChanged the data within that object04-11: “Mutable” ObjectsICanChange c = new ICanChange(4);c = new ICanChange(11);System.out.println(c.getX());Created an object of type ICanChange, with value4Created a new object of type ICanChange, withvalue 11Throw away the old object04-12: “Mutable” ObjectsICanChange c = new ICanChange(4);StrangeClass s = new StrangeClass(); // Don’t know what this does ...s.foo(c);System.out.println(c.getX());04-13: “Mutable” Objectspublic class StrangeClass{void foo(ICanChange a){a.setX(99);}}04-14: “Immutable” Objectpublic class ICantChange{private int x;public ICanChange(int initialX){this.x = initialX;}public int getX(){return this.x;}}04-15: “Immutable” ObjectICantChange c = new ICantChange(13);System.out.println(c.getX());c = new ICantChange(37);System.out.println(c.getX());Create a new object, have c point to this new objectOld object didn’t change, but the value of c did ....04-16: “Immutable” ObjectICantChange c = new ICantChange(13);Strange s = new Strange();s.foo(c);System.out.println(c.getX());Do we know anything about what the println willouput?04-17: “Immutable” Objectspublic class Strange{void foo(ICantChange icc){// We can’t change the value of x stored in icc// directly (private, no setters)//// Best we can do is change what icc points to ...icc = new ICantChange(99);// icc.getX() would return 99 here, but what about// the calling function?}}04-18: Back to StringsStrings are objects, like any other objectStored on the heap, but immutableWhole host of useful methods for stringmanipulation04-19: String Methodspublic char charAt(int i): returns the character atindex i (starting at 0)String s = "cartwheel";char c = s.charAt(2);What value would c now have?04-20: String Methodspublic int length(): returns the


View Full Document

USF CS 112 - Strings

Documents in this Course
Structs

Structs

4 pages

Trees

Trees

25 pages

Queues

Queues

3 pages

Trees

Trees

24 pages

Arrays

Arrays

5 pages

ArrayList

ArrayList

24 pages

Stacks

Stacks

2 pages

Stacks

Stacks

8 pages

Trees

Trees

24 pages

Stacks

Stacks

8 pages

Queues

Queues

16 pages

Queues

Queues

17 pages

Queues

Queues

17 pages

Load more
Download Strings
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Strings and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Strings 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?