Presentation is loading. Please wait.

Presentation is loading. Please wait.

AP Java Unit 3 Strings & Arrays.

Similar presentations


Presentation on theme: "AP Java Unit 3 Strings & Arrays."— Presentation transcript:

1 AP Java Unit 3 Strings & Arrays

2 Lesson 1: Strings and Classes

3 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.

4 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

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

6 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

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

8 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”

9 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

10 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.

11 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

12 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!

13 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()

14 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:

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

16 .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’

17 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 “

18 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?

19 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.

20 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


Download ppt "AP Java Unit 3 Strings & Arrays."

Similar presentations


Ads by Google