Using Objects 26-Jul-16 Classes and objects The type of an object is the class that describes that object A variable can only hold values of the appropriate type count = 5; name = "Dave"; background = Color.RED; A literal is when we write an actual value, not a variable If we say int count, the type of count is int If we say String name, the type of name is String If we say Color background, the type of background is Color 5 is a literal of type int "Dave" is a literal of type String There aren’t any Color literals There are exactly eight primitive types There are thousands of classes, and you can create more 2 Declarations You declare variables to hold primitive values like this: You declare variables to hold objects like this: int classSize; double area; Color uglyBrown; String myName; In both cases, the syntax is: Type name ; 3 Assignment statements An assignment statement has the form: Examples: variable = expression ; classSize = 40; area = pi * radius * radius; uglyBrown = new Color(175, 175, 30); myName = "David Matuszek"; Assignment to object variables is just like assignment to primitive variables 4 Combining declaration and assignment Declaration and assignment can be combined into a single statement: int classSize = 40; String myName = "David Matuszek"; Color uglyBrown = new Color(175, 175, 30); You can only declare a variable once, but you can assign to it many times in many places A variable declared inside a method is declared only for that one method Hence, a method can declare a variable with the same name as a variable in another method—but it’s a different variable 5 Methods Primitives have operations, classes have methods One exception: Strings have the + operation The syntax for operations is built into Java You cannot define new primitives, but you can define new classes You cannot define new operations, but you can define new methods Here we will talk about using methods supplied by Java, not defining new ones 6 Data in classes and objects A class is the type of an object A class describes: How to make a new object of that class What kind of data is in an object Example: new Color(175, 175, 30); In this example the keyword new says we are using a constructor, which is similar to a method Example: a Color object contains three numbers representing the amount of red, green, and blue The methods of an object (the actions it can perform) Example: a Color object can tell you how much red it contains: int amount = myColor.getRed(); 7 Sending messages to objects We don’t perform operations on objects, we “talk” to them This is called sending a message to the object A message looks like this: object.method(extra information) • • • The object is the thing we are talking to The method is a name of the action we want the object to take The extra information is anything required by the method in order to do its job Examples: g.setColor(Color.pink); amountOfRed = Color.pink.getRed( ); 8 Messages and methods Messages can be used to: Tell an object some information Ask an object for information (usually about itself) Request an object to do something Any and all combinations of the above A method is something inside the object that responds to your messages A message contains commands to do something Java contains thousands of classes, each typically containing dozens of methods When you program you use these classes and methods, and also define your own classes and methods 9 Different objects, different methods When we send a message to an object, that object must have a method that “knows” how to respond to the message The method must have the correct name and the same number and corresponding types of parameters Different objects may have methods with the same name and same number and types of parameters We could send the same message to two different objects, and (if those objects are of different classes) they might execute different methods If we do this, the methods should do similar things 10 Messages to a Graphics If you have a Graphics, and its name is g, here are some things you can do with g: Tell it to use a particular color: g.setColor(Color.orange); Ask it what color it is using: Color currentColor = g.getColor(); Tell it to draw a line: g.drawLine(14, 23, 87, 5); 11 Messages to a Color Once you make a Color, you cannot change it; you can only ask it for information // Make a new purplish color Color myColor = new Color(100, 0, 255); // Ask how much blue is in it int amountOfBlue = myColor.getBlue(); // Ask the color for a brighter version of itself Color brightColor = myColor.brighter(); The last method doesn’t change the color; it makes a new color 12 String A String is an object, but... ...because Strings are used so much, Java gives them some special syntax There are String literals: "This is a String" There is an operation, concatenation, on Strings: (Almost) no other objects have literals "Dave" + "Matuszek" gives "DaveMatuszek" In other respects, Strings are just objects 13 String methods A String, like a Color, is immutable Once you create it, there are no methods to change it But you can easily make new Strings: myName = "Dave"; myName = "Dr. " + myName; This is kind of a subtle point; it will be important later, but you don’t need to understand it right away If s is the name of the string "Hello", then s.length() tells you the number of characters in String s (returns 5) s.toUpperCase() returns the new String "HELLO" (s itself is not changed) But you can say s = s.toUpperCase(); 14 String concatenation + usually means “add,” but if either operand (thing involved in the operation) is a String, then + means concatenation If you concatenate anything with a String, you are actually concatenating a “string version” of that thing For example, you can concatenate a String and a number: System.out.println("The price is $" + price); + as the concatenation operator is an exception to the rule: Primitives have operations, Objects have methods 15 Data in classes A class describes objects. It describes: How to construct an object of that class, the kind of data in an object, and the messages that the object can understand A class can also contain its own data, which is the same for any object of that class Constants are often provided this way Examples: class Color contains the constant Color.RED class Math contains the constant Math.PI 16 Printing out results, part 1 In Java, “print” really means “display on the screen” Actually printing on paper is much harder! System is one of Java’s built-in classes System.out is a data object in the System class that knows how to “print” to your screen Because print is a method of the System.out object, we can “talk to” (send messages to) this mysterious object without knowing very much about it 17 Printing out results, part 2 System.out is a object with useful methods that will let you print anything: print(x) turns x into a String and displays it println(x) (pronounced “print line”) turns x into a String and displays it, then goes to the next line Examples: System.out.print("The sum of x and y is "); System.out.println(x + y); 18 New vocabulary class: the type, or description, of an object object: an instance, or member, of a class message: something that you “say” to a class, either telling it something or asking it for information immutable: cannot be changed after it is created operand: one of the inputs to an operation 19 The End “If you give someone a program, you will frustrate them for a day; if you teach them how to program, you will frustrate them for a lifetime.” --Anon. 20