Using Objects Chapter 3 Spring 2006 CS 101 Aaron Bloomfield 1 Variables vs. Types The type is the recipe or template for how to create a variable Examples: int, double, char, boolean, etc. There are only 8 primitive types There are only a few things you can do with a type: Declare a variable int x; Use it as a cast x = (int) 3.5; There is only one of each type The variable is the actual instance of a type in memory It’s a spot in memory where you store a value You choose the name: width, x, thatThemThereValue, etc. You can have as may variables as you want – but only one type! Like the difference between a recipe and a bunch of cookies 2 Values versus objects Numbers Have values but they do not have behaviors Objects Have attributes and behaviors 3 Classes vs. Objects A class is a user-defined “thing” Examples: String, Scanner, Rectangle, etc. We’ll start defining our own classes later this semester Classes are more complex than the primitive types A class is analogous to a type It’s just more complex and user-defined There can be only one class of each name An object is an instance of a class There is only one String class, but you can have 100 String objects An object is analogous to a variable It just is a reference instead A class is a “template” used for creating objects 4 Using objects First, we create an object: Scanner stdin = new Scanner (System.in); Most object creation lines look like this Then we use the object stdin.nextInt(); stdin.nextDouble(); Note that we could have called the object foo, bar, or anything stdin is just what we chose to call it 5 Using Rectangle objects Let’s create some Rectangle objects Rectangle creation: Rectangle r = new Rectangle (10, 20); Objects have attributes (or properties): System.out.println (r.length); System.out.println (r.width); Objects have behaviors (or methods): r.grow (10, 20) r.isEmpty() r.setLocation (5,4) 6 Using String objects Let’s create some String objects String creation: String s = new String (“Hello world”); Objects have attributes (or properties): But we can’t access them… Objects have behaviors (or methods): s.substring(0,6) s.indexOf (“world”) s.toLowerCase() 7 The lowdown on objects Objects are “things” that have properties (attributes) and behaviors (methods) We first create one or more objects We then manipulate their properties and call their methods 8 So why bother with objects? Let’s say you want to do a lot of String manipulation Once you create a String object, all the manipulation methods are contained therein Sun already wrote the methods for us So we can use String objects instead of writing our own code to get the substring, indexOf, etc. 9 Visualizing objects Class (type) name - width = 10 - height = 20 - ... Attributes (properties) + grow (int, int) : void + isEmpty ( ) : void + setLocation ( int, int ) : void + resize ( int, int ) : void + ... Methods (behaviors) Rectangle 10 How well do we understand using objects? 11 Review Variables of primitive types int, double, char, boolean, etc. Can assign a value to it Can read a value from it Can’t do much else! Objects String, Rectangle, etc. Have many parts Rectangle has width, length, etc. Like a complex type Have methods String has length(), substring(), etc. 12 String methods length(): returns the String’s length (duh!) String s = “hello world”; String t = “goodbye”; System.out.println (s.length()); System.out.println (t.length()); Prints 11 and 7 Note that calling s.length() is different than calling t.length()! Both return the length But of different Strings 13 More String methods Consider String weddingDate = "August 21, 1976"; String month = weddingDate.substring(0, 6); System.out.println("Month is " + month + "."); What is the output? Month is August. 14 More String methods Consider String fruit = "banana"; String searchString = "an"; int n1 = fruit.indexOf(searchString, 0); int n2 = fruit.indexOf(searchString, n1 + 1); int n3 = fruit.indexOf(searchString, n2 + 1); System.out.println("First search: " + n1); System.out.println("Second search: " + n2); System.out.println("Third search: " + n3); What is the output? First search: 1 Second search: 3 Third search: -1 15 These images are not animated… 16 Program WordLength.java public class WordLength { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); System.out.print("Enter a word: "); String word = stdin.next(); int wordLength = word.length(); System.out.println("Word " + word + " has length " + wordLength + "."); } } 17 More String methods trim() Returns the String without leading and trailing whitespace Whitespace is a space, tab, or return 18 Reading Javadocs What is a Javadoc? Documentation of Java classes Where are Sun’s Javadocs? http://java.sun.com/j2se/1.5.0/docs/api/index.html 19