AP Java Unit 3 Strings & Arrays.

Slides:



Advertisements
Similar presentations
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Advertisements

Java Programming Strings Chapter 7.
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
CIS 234: Strings. What Is a String? series of characters enclosed in double quotes characters can include letters (lower case and upper case), digits,
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
Arrays Horstmann, Chapter 8. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
Fundamental Programming Structures in Java: Strings.
Introduction to Computers and Programming Strings Professor: Evan Korth New York University.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
Lecture 14 The String Class Lecture 14 The String Class Instructor: Craig Duckett.
CIS 234: Strings (click, scroll down)Strings Dr. Ralph D. Westfall April, 2010.
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Primitive Data Types. Identifiers What word does it sound like?
Some Standard Classes Goals The Object class The String class Wrapper classes The Math class Random Numbers.
Variables and Data Types Data (information we're going to store) – Numbers – Text – Dates What types of data can JavaScript process? How do we store it?
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.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.
Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
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.
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
Strings A String is a sequence of letters
Java String Methods - Codehs
CSC 211 Java I for loops and arrays.
EGR 2261 Unit 11 Pointers and Dynamic Variables
Computer Organization and Design Pointers, Arrays and Strings in C
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Chapter 2 Basic Computation
Pointers.
Lecture 5 D&D Chapter 6 Arrays and ArrayLists Date.
String class.
Foundations of Programming: Arrays
Multiple variables can be created in one declaration
Variables and Primative Types
Strings and Object References
Java Review: Reference Types
Wrapper Classes ints, doubles, and chars are known as primitive types, or built-in types. There are no methods associated with these types of variables.
Arrays in C.
void Pointers Lesson xx
Arrays & Functions Lesson xx
String Objects & its Methods
Structures Lesson xx In this module, we’ll introduce you to structures.
Lesson 2: Building Blocks of Programming
Type Conversion, Constants, and the String Object
Multiple Choice -answer the easiest question 1st
CS 302 Week 8 Jim Williams, PhD.
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
Java Programming Arrays
Strings A string is a sequence of characters that is treated as a single value. Strings are objects. We have been using strings all along. Every time.
Introduction to Primitive Data types
String and Lists Dr. José M. Reyes Álamo.
CS 200 Arrays Jim Williams, PhD.
elementary programming
Variables and Computer Memory
Unit 3: Variables in Java
IAT 800 Foundations of Computational Art and Design
CS31 Discussion 1H Fall18: week 6
AP Java Unit 3 Strings & Arrays.
LCC 6310 Computation as an Expressive Medium
More on iterations using
Introduction to Primitive Data types
Pre-AP® Computer Science Quiz
Introduction to Pointers
Introduction to Pointers
Presentation transcript:

AP Java Unit 3 Strings & Arrays

Lesson 1: Strings and Classes

So I need a program to handle all the postcards I need to mail home. Yes – I need to search for everyone that lives on my street – that way I don’t have to mail them Sounds like it. Good thing Java has all those String methods! So you’ll need to search the Strings. You need names, addresses….sounds like you’ll be using a lot of strings.

Java has two basic kinds of data types: Primitive: Holds only one piece of data at a time Ex: int, char, float Class: holds more than one piece of data at a time can hold data of different types has built in methods (tools) Examples: Strings, arrays Users can create their own data types by writing classes. String is a Class type

How are variables stored in memory? 17 Primitive types the variable holds the actual value int num1 = 17; int

Strings are Reference Variables in memory: String word1 = “Hello”; The variable holds the memory location of the actual data – REFERS to it This is true for ALL class type variables. address

Do Demo – pencil boxes and string null cut the yarn reference  String a = "what"; b = a; two strings

String word; What data type is word? What does word hold? Reference to a String What does word hold? null null – special value that means “no object”

So what is the difference? String w1; This is a null reference. It does not point to any object in memory. String w2 = “”; This is an empty String. It has a memory address that points to an empty spot. address Empty address Empty

Garbage Collection String alpha = “The final frontier” alpha = null; What happens to "The final frontier" ? It is Garbage Collected. Java goes through and "cleans up" any unattached values in memory.

What happens? String alpha = "The final frontier" String beta = alpha; alpha = null; After 3rd statement: After 2nd statement: After 1st statement: alpha null The final frontier beta

String a = “Howdy”; String b = “Howdy”; So why can’t I use = = ? Are they the same? No! == tests the value stored directly in a and b, and these are two different memory locations Howdy!

You know you’re curious. How Strings work You know you’re curious. The letters in Strings are stored as individual characters Each is given an address The addresses start at zero You can find the length using .length() word.length()

You must know: s.equals(); s.compareTo(“no”); s.charAt(2); Since Strings are classes they have data and methods. That means they can make changes to their own data. s.equals(); s.compareTo(“no”); s.charAt(2); s.toUpperCase(); s.indexOf(‘a’); s.toLowerCase(); s.substring(1, 4); There are more: http://download.oracle.com/javase/6/docs/api/java/lang/String.html

Why can’t we use ==? if ( a.eqauls(b) )  YES! .equals() ? we just looked @ memory addresses  .equals() This method is built into String It tests if the contents are equal if ( a.eqauls(b) )  YES! .equals() ?

.charAt() tells us what letter is at a spot in the String the first spot is 0 String w = “canine”; System.out.println(w.charAt(2));  prints the letter ‘n’

That’s nice, but I know the letter, I need to know where it is! .indexOf(c); put in a character, tells where it is String w = “canine”; System.out.println(w.indexOf(‘i’)); prints 3 Try it and see: What happens if the letter is not in the String?  What if the letter is there more than once? That’s nice, but I know the letter, I need to know where it is! remember char uses ‘, not “

Changes the String to all upper case letters .toUpperCase() ? Changes the String to all upper case letters s = s.toUpperCase(); You try it: What happens to any digits that are in there? What do you think toLowerCase() does?

Try these in the compiler: String s = “Canines in glasses”; System.out.print( s.substring(11, 7)); System.out.print( s.compareTo(“apple”)); System.out.print( s.compareTo(“zebra”)); What did you get? There are more? I need a nap.

Still not sure what it means? Look here: Concatenate Concatenation: add Strings word3 = word1 + word2; The + symbol is overloaded This means it has multiple uses depending on the situation