Introduction to Computers and Programming Strings Professor: Evan Korth New York University.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Chapter 8: Arrays.
CS110 Programming Language I
Notes from HW3 / Lab3 Program documentation – At the top of every class: //************************************************************ // seu01.java Author:
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Introduction to Computers and Programming Lecture 11: Introduction to Methods Professor: Evan Korth New York University.
Road Map Introduction to object oriented programming. Classes
Introduction to Computers and Programming Introduction to Methods in Java.
Overloading methods review When is the return statement required? What do the following method headers tell us? public static int max (int a, int b)
Introduction to Computers and Programming Lecture 7:
Introduction to Computers and Programming Lecture 12: Math.random() Professor: Evan Korth New York University.
Introduction to Computers and Programming Lecture 17: Arrays (cont) Professor: Evan Korth New York University.
Introduction to Computers and Programming Lecture 16: Arrays (cont) Professor: Evan Korth New York University.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Introduction to Computers and Programming Lecture 3: Variables and Input Professor: Evan Korth New York University.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Working with the data type: char  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
Fundamental Programming Structures in Java: Strings.
The Class String. String Constants and Variables  There is no primitive type for strings in Java.  There is a class called String that can be used to.
Introduction to Computers and Programming Lecture 13: User defined methods Instructor: Evan Korth New York University.
Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.
Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.
Recursion & Collections API Recursion Revisited Programming Assignments using the Collections API.
Moving from C to Java. The Java Syntax We Know Java Types Some of the types we know and love are still there  Integer: byte, short, char, int, long.
11 Chapter 5 METHODS CONT’D. 22 MORE ON PASSING ARGUMENTS TO A METHOD Passing an Object Reference as an Argument to a Method Objects are passed by reference.
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
Primitive Types Java offers a number of primitive types eg.) int, short, long double, float char A variable which is declared as a primitive type stores.
SE-1010 Dr. Mark L. Hornick 1 Some Java Classes using & calling methodsS.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Introduction to Computers and Programming Lecture 14: User defined methods (cont) Professor: Evan Korth New York University.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
SE-1010 Dr. Mark L. Hornick 1 Variables & Datatypes.
Characters and Strings. Characters  New primitive char  char letter; letter = ‘a’; char letter2 = ‘C’;  Because computers can only represent numbers,
String and Scanner CS 21a: Introduction to Computing I First Semester,
1 Fencepost loops suggested reading: The fencepost problem Problem: Write a static method named printNumbers that prints each number from 1 to a.
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
String Class. Variable Holds a primitive value or a reference to an object. Holds a primitive value or a reference to an object. A reference lets us know.
Sahar Mosleh California State University San MarcosPage 1 The Class String There is no primitive type for strings in Java The class String is a predefined.
Coding Bat: Ends in ly Given a string of even length, return a string made of the middle two chars, so the string "string" yields "ri". The string.
Lab 4 - Variables. Information Hiding General Principle: – Restrict the access to variables and methods as much as possible Can label instance variables.
Road map char data type Reading –Liang 5: Chapter 2: 2.7.4; 2.9; –Liang 6: Chapter 2: 2.7.4; 2.9 –Liang 7: Chapter 2: 2.7.4; 2.9.
Creating and Using Class Methods. Definition Class Object.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Computer Programming 2 Lab (1) I.Fatimah Alzahrani.
CSC 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Chapter 8 Arrays and the ArrayList Class Arrays of Objects.
Examples of Classes & Objects
AKA the birth, life, and death of variables.
Building Java Programs Chapter 4.3
Chapter 3: Using Methods, Classes, and Objects
Road Map Introduction to object oriented programming. Classes
CISC/CMPE320 - Prof. McLeod
Introduction to Programming in Java
AP Java Unit 3 Strings & Arrays.
Type Conversion, Constants, and the String Object
An Introduction to Java – Part II
An Introduction to Java – Part I, language basics
Classes & Objects: Examples
Chapter 4 Writing Classes.
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Chapter 2 Programming Basics.
Unit-1 Introduction to Java
AP Java Unit 3 Strings & Arrays.
String Class.
What is a String? String s = "compsci"; s c o m p s i
More on iterations using
Presentation transcript:

Introduction to Computers and Programming Strings Professor: Evan Korth New York University

Road map Strings –String variables –Concatenation –length() –charAt() –Strings and methods

review What does scope refer to in Java? What is method overloading? What does it mean when we say "pass by value"?

Characters and Strings We have studied the char data type with can hold one character. The String is another data type (it is actually a Class) we can use in Java. –A String is a series of chars. –We can treat the series as one unit –There are methods in the Java API we can use on strings –We can pass Strings to methods and return Strings from methods.

String variables String variables are unlike other variables we have used in this class. String variables are called reference variables because they hold a reference to where the string is stored in memory. –The rest of the variables we discussed in this class are called primitive variables Java hides the details from us. Example: String s = "A string of characters";

String concatenation In Java, we can add two Strings together using the + operator. That process is called concatenation. The following statements: String s1 = "This looks "; String s2 = "familiar"; String s3 = s1 + s2; assigns the String "This looks familiar" to the variable s3.

String's charAt() method String also has a method called charAt() which can be used to return any single character in a String. –Note: The first letter in a string is considered to be in position zero. For example, if we have: –String s = "Hello"; –s.charAt (0) will return 'H' –s.charAt (4) will return 'o' –s.charAt (5) will return StringIndexOutOfBoundsException

String's length() method All Strings know their own length. To find out the length of a string, we use String's length() method. For example: String s = "Hello"; int length = s.length(); Will place the value 5 in variable length.

Passing Strings to methods We can also pass a string to a method by using String types in a parameter list. For example, the following method header would represent a method which accepts a String as a parameter and does not return a value: public static void printVertical (String s)

Returning Strings from methods It is possible to write a method that creates and returns a String. For example, the following method header would represent a method which accepts no parameters and returns a String: –public static String getMonth(int month)