Lecture 10 Objects and Classes in Java Last time 1 Pseudo code 2 Objects and classes Today 1 Object equality 2 Objects and classes in Java 9 22 2006 CMSC 131 Fall 2006 Rance Cleaveland 2006 Univeristy of Maryland What Are Objects Classes Stack Heap Reference type variables new CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 1 Equality and Objects can be used to compare objects but result is not always what you expect What is output of following String a new String abc String b new String abc if a b println Equal else println Not equal Not equal CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 2 for Reference Values compares for identical values If x y are reference variables then they contain addresses Two addresses are equal if they point to exactly the same thing In previous example a and b are assigned different addresses because new is called twice CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 3 Example String a new String abc String b new String abc if a b println Equal else println Not equal Stack Heap a b abc abc CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 4 Contrasting Example String a new String abc String b a if a b println Equal else println Not equal Stack Heap a b Equal is printed abc CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 5 equals checks if two reference variables refer to the same object Methods like str equals check if two different objects have the same content Other classes will have an equals method also CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 6 Classes in Java Class declarations have the following form in Java Visibility modifier more later in class class keyword class name public class Date class body instance variables methods When you create a class in Eclipse it generates this template for you CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 7 Anatomy of an Instance Variable Declaration Visibility modifier Normal variable ceclaration public int day 1 CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 8 Method Declarations in Java Methods are operations like etc They can take inputs They can return values They can modify instance variables The form varies slightly depending on whether values are returned or not CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 9 Anatomy of a Method Declaration 1 for methods that do not return values Visibility modifier void keyword method name parameter list public void setYear int newYear year newYear body CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 10 void Parameter List If a method does not return a value use the void keyword The parameter list describes the form of inputs type name for use in body Parameter lists may be empty Multiple parameters are separated by CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 11 Anatomy of a Method Declaration 2 for methods that return values Visibility modifier return type method name parameter list public boolean isBefore Date d return true body CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 12 Return Type Methods that return values must specify the type of the value to be returned The bodies of these methods use return to indicate when a value is to be returned The value being returned must have the same type as the return type CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 13 Example isBefore returns true if the date is before the inputted date public boolean isBefore Date d if year d year return true else if year d year if month d month return true else if month d month day d day return true else return false else return false CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 14 Comments allow you to insert explanations in your code Two form blah blah blah blah blah blah Every Class declaration Instance variable declaration Method declaration must have a comment explaining its purpose CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 15 Example Programmer Rance Cleaveland Date 21 Sept 2006 This class implements simple operations on dates public class Date public int day 1 public int month 1 public int year 1900 public String separator public String note Day part of date Month part of date Year part of date Separator used in printing Used for comment on date print prints date in US format public void print System out print month separator day separator year setYear allows the year to be set to a give value public void setYear int newYear year newYear CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 16 Object Creation Once a class is defined objects based on that class can be created using new new Date To assign an object to a variable the variable s type must be the class of the object Date d new Date Each object has its own copies of all the instance variables in the class except for certain kinds we ll study later Instance variables and methods in an object can be accessed using d day 14 d setYear 1999 CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 17
View Full Document