Using Objects Overview • In this presentation we will discuss: – Classes and objects – Methods for objects – Printing results Classes and objects • A class is the type of an object • Just as a variable classSize may have type int, Color.red has type Color • Just as 5 is a literal of type int, "Hello" is a literal of type String • There are exactly eight primitive types • There are thousands of classes, and you can create more Declarations • You declare variables to hold primitive values like this: int classSize; double area; • You declare variables to hold objects like this: Color uglyBrown; String myName; Assignment statements • An assignment statement has the form: variable = expression ; • Examples: classSize = 57; area = pi * radius * radius; uglyBrown = new Color(175, 175, 30); myName = "David Matuszek"; Combining declaration and assignment • Declaration and assignment can be combined into a single statement: int classSize = 57; 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 – This rule is “true enough” for now – Exceptions are complicated and left for later Methods • Primitives have operations, classes have methods • 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 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 • Example: new Color(175, 175, 30); – What kind of data is in an object • 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 Sending messages to objects • We don’t perform operations on objects, we “talk” to them – This is called sending a message to the object • We do it like this: – object.message(extra information) • Examples: g.setColor(Color.pink); amountOfRed = Color.pink.getRed( ); Messages • Messages can be used to – Tell an object some information – Tell an object to do something – Ask an object for information (usually about itself) – Any and all combinations of the above 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); 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(); 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" • (Almost) no other objects have literals – There is an operation, concatenation, on Strings: • "Dave" + "Matuszek" gives "DaveMatuszek" • In other respects, Strings are just objects String methods • A String, like a Color, is immutable: once you create it, you cannot ever change it • ...but you can make new Strings and Colors • If s is the name of the string "Hello", then – s.length() tells you the number of chararacters in the string (returns 5) – s.toUpperCase() returns the new String "HELLO" (s itself is not changed) 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, that thing is first turned into a String • For example, you can concatenate a String and a number: System.out.println("The price is $" + price); 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 – Constants are often provided this way – Examples: • class Color contains the constant Color.red • class Math contains the constant Math.PI 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 • We can talk to this mysterious object without knowing very much about it 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 “printline”) 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); 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 The End