Intro to Computer Science II

Similar documents
CS S-24 Final Review I 1

Appendix 3. Description: Syntax: Parameters: Return Value: Example: Java - String charat() Method

Crash Course in Java. Why Java? Java notes for C++ programmers. Network Programming in Java is very different than in C/C++

"Hello" " This " + "is String " + "concatenation"

Strings. Strings and their methods. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

Strings. Strings and their methods. Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

Programming with Java

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

A variable is a name for a location in memory A variable must be declared

We now start exploring some key elements of the Java programming language and ways of performing I/O

Notes from the Boards Set BN19 Page

More on Strings. String methods and equality. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Compu<ng and Mathema<cs h=p://

COE318 Lecture Notes Week 4 (Sept 26, 2011)

Getting started with Java

CSE 21 Intro to Computing II. JAVA Objects: String & Scanner

Intro to Strings. Lecture 7 CGS 3416 Spring February 13, Lecture 7 CGS 3416 Spring 2017 Intro to Strings February 13, / 16

CS1150 Principles of Computer Science Math Functions, Characters and Strings (Part II)

Review. Single Pixel Filters. Spatial Filters. Image Processing Applications. Thresholding Posterize Histogram Equalization Negative Sepia Grayscale

Introduction to Programming Using Java (98-388)

write the functions inside as well as outside the class. So in C++, they are called member functions and not methods.

0. What is the capital of Canada? (4pts) a. Toronto b. Quebec c. Ottawa d. Vancouver e. Calgary f. Not sure

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while

Overloaded Methods. Sending Messages. Overloaded Constructors. Sending Parameters

More non-primitive types Lesson 06

CS 1301 Ch 8, Part A

Slide 1 CS 170 Java Programming 1 More on Strings Duration: 00:00:47 Advance mode: Auto

Intro to Strings. Lecture 7 COP 3252 Summer May 23, 2017

Strings. Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects.

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

Installing Java. Tradition. DP Computer Science Unit 4.3: Intro to programming 1/17/2018. Software & websites

Java Foundations: Unit 3. Parts of a Java Program

CSC Java Programming, Fall Java Data Types and Control Constructs

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Introduction to Java

Java in 21 minutes. Hello world. hello world. exceptions. basic data types. constructors. classes & objects I/O. program structure.

Computational Expression

Assumptions. History

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

COMP 110 MORE TYPES. Instructor: Sasa Junuzovic

AP Computer Science A

2. The object-oriented paradigm

c) And last but not least, there are javadoc comments. See Weiss.

More on Strings. Lecture 10 CGS 3416 Fall October 13, 2015

CS121/IS223. Object Reference Variables. Dr Olly Gotel

CS Programming I: Arrays

Practice exam for CMSC131-04, Fall 2017

Program Fundamentals

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

Programming Languages and Techniques (CIS120)

CS 11 java track: lecture 1

Chapter 29: String and Object References Bradley Kjell (Revised 06/15/2008)

CHAPTER 6 MOST COMMONLY USED LIBRARIES

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

J.43 The length field of an array object makes the length of the array available. J.44 ARRAYS

CS 231 Data Structures and Algorithms, Fall 2016

CS 101 Exam 1 Spring 200 Id Name

An overview of Java, Data types and variables

Example: Tax year 2000

Strings in Java String Methods. The only operator that can be applied to String objects is concatenation (+) for combining one or more strings.

Compilers CS S-10 Object Oriented Extensions

Activity 9: Object-Oriented

CS 520 Theory and Practice of Software Engineering Fall 2018

Java Basic Programming Constructs

C Sc 127A Practice Test 2 SL Name 150pts

Fall 2017 CISC124 9/16/2017

Unit 4: Classes and Objects Notes

CS 520 Theory and Practice of Software Engineering Fall 2017

Intro to Computer Science II

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Java Classes & Primitive Types

CS 520 Theory and Practice of Software Engineering Fall 2017

Operators in java Operator operands.

TEST (MODULE:- 1 and 2)

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Midterm Review Topics Java Basics and Structure Variables Branching Loops Methods Arrays. Midterm Review Kage Weiss Lab 001 TA, SPR 17

Chapter 1 Getting Started

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Java Classes & Primitive Types

Lab 14 & 15: String Handling

Programming Language Concepts: Lecture 2

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing.

Lecture Notes K.Yellaswamy Assistant Professor K L University

Final Exam CS 152, Computer Programming Fundamentals December 9, 2016

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

Lecture 5. Assignments. Arithmetic Operations. Arithmetic Operations -Summary 1/24/18. Lab 3: Variables and operations. Read Sections

Building Strings and Exploring String Class:

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

CMPT 125: Lecture 3 Data and Expressions

a data type is Types

CSE1710. Big Picture. Tuesday, Dec 02 is designated as a study day. No classes. Thursday, Dec 04 is last lecture.

Place your name tag here

Inf1-OOP. Data Types. A Foundation for Programming. type value set operations. Overview. Using Data Types 1. Image Processing

STRINGS AND STRINGBUILDERS. Spring 2019

STUDENT LESSON A10 The String Class

Mathematical Functions, Characters, and Strings. CSE 114, Computer Science 1 Stony Brook University

Eng. Mohammed Abdualal

8/25/17. Demo: Create application. CS2110, Recita.on 1. Arguments to method main, Packages, Wrapper Classes, Characters, Strings.

Transcription:

Intro to Computer Science II CS112-2012S-04 Strings David Galles Department of Computer Science University of San Francisco

04-0: Types in Java Primative Types Hold simple values Can be stored on the stack (but can be stored on the heap if they are instance variables in classes) integers: byte (8 bits), short (16 bit) int (32 bit) long (64 bit) real numbers: float (32 bit) double (64 bit) boolean: true or false value char: single character (16 bit, unicode) in C, a char is 8 bits, uses ASCII

04-1: Types in Java Objects Collection of data and methods Always stored on the heap Pointer to object can be on the stack Created with a call to new

04-2: Strings Strings in Java are objects Contain both methods and data Data is the sequence of characters (type char) that make up the string Strings have a whole bunch of methods for string processing

04-3: Strings Strings in Java are objects Strings are stored on the heap, like all other objects Data is stored as an array of characters (more on arrays next week. Similar to python lists)

04-4: String Literals String s; s = "Dog"; "Dog" is called a String Literal Anything in quotation marks is a string literal System.out.println("Hello There")

04-5: String Literals Any time there is a string literal in your code, there is an implicit call to new A new string object is created on the heap Data is filled in to match the characters in the string literal Pointer to that object is returned String s; s = "MyString"; // Implicit call to new here!

04-6: Stack vs. Heap I public void foo() { int x = 99; char y = c ; String z = "c"; String r = "cat"; float w = 3.14; }

04-7: Immutable Strings Strings are immutable Once you create a string, you can t change it. String s = "Car"; unknown.foo(s); // Create a block of memory containing car // Return a pointer to this block of memory // This function can t mess with contents of s System.out.println(S); // s is guaranteed to be "Car" here

04-8: Immutable Strings String objects are immutable Once a string object is created, it can t be changed String variables can be changed Create a new String object, assign it to the variable String s = "dog"; s = "cat";

04-9: Mutable Objects public 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 Objects ICanChange c = new ICanChange(4); c.setx(11); // Changed the value in object // c points to System.out.println(c.getX()); Created an object of type ICanChange Changed the data within that object

04-11: Mutable Objects ICanChange c = new ICanChange(4); c = new ICanChange(11); System.out.println(c.getX()); Created an object of type ICanChange, with value 4 Created a new object of type ICanChange, with value 11 Throw away the old object

04-12: Mutable Objects ICanChange 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 Objects public class StrangeClass { void foo(icanchange a) { a.setx(99); } }

04-14: Immutable Object public class ICantChange { private int x; } public ICanChange(int initialx) { this.x = initialx; } public int getx() { return this.x; }

04-15: Immutable Object ICantChange 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 object Old object didn t change, but the value of c did...

04-16: Immutable Object ICantChange c = new ICantChange(13); Strange s = new Strange(); s.foo(c); System.out.println(c.getX()); Do we know anything about what the println will ouput?

04-17: Immutable Objects public 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 Strings Strings are objects, like any other object Stored on the heap, but immutable Whole host of useful methods for string manipulation

04-19: String Methods public char charat(int i): returns the character at index i (starting at 0) String s = "cartwheel"; char c = s.charat(2); What value would c now have?

04-20: String Methods public int length(): returns the length of the string String s = "cartwheel"; int len = s.length(); What value would len now have?

04-21: String Methods pubic String substring(int beginindex): returns a new string, starting with beginindex String s = "cartwheel"; String s2 = s.substring(4); s2 would have the value wheel What value would s now have?

04-22: String Methods public String substring(int beginindex, endindex): returns a new string, starting with beginindex, with last char at positn (endindex - 1) String s = "cartwheel; String s2 = s.substring(1, 4); s2 would have the value art What value would s now have?

04-23: String Methods public String concat(string str) : returns a new string, consisting of this string concatenated with str String s1 = "dog"; String s2 = "house"; Sring s3 = s1.concat(s2); s3 would have the value doghouse What value would s1, s2 have?

04-24: String Methods public String tolowercase() : returns a new string, consisting of this string force into lower case String s1 = "ThisIsAString"; String s2 = s1.tolowercase(); s2 would have the value thisisastring What value would s1 have?

04-25: String Methods public String tolowercase() : returns a new string, consisting of this string force into lower case String s1 = "ThisIsAString"; s1.tolowercase(); What value would s1 have? What just happened?