9/22/2006 CMSC 131 Fall 2006Rance Cleaveland©2006 Univeristy of MarylandLecture 10:Objects and Classes in JavaLast time:1. Pseudo-code2. Objects and classesToday:1. Object equality2. Objects and classes in JavaCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland1What Are Objects? Classes? Stack? Heap? Reference-type variables? new?CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland2Equality and Objects == can be used to compare objects, but result is not always whatyou expect. What is output of following?String a = new String (“abc”);String b = new String (“abc”);if (a == b)println (“Equal”);elseprintln (“Not equal”); Not equal ???CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland3== 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 2006Rance Cleaveland©2006 University of Maryland4StackabExampleString a = new String (“abc”);String b = new String (“abc”);if (a == b)println (“Equal”);elseprintln (“Not equal”);Heap“abc” “abc”CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland5HeapStackabContrasting ExampleString a = new String (“abc”);String b = a;if (a == b)println (“Equal”);elseprintln (“Not equal”); Equal is printed“abc”CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland6“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 alsoCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland7Classes in Java Class declarations have the following form in Java:public class Date {} When you create a class in Eclipse, it generates this template for youVisibility modifier:more later in classclass keyword class nameclass body: instance variables, methodsCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland8Anatomy of an Instance Variable Declarationpublic int day = 1;Visibility modifier Normal variable ceclarationCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland9Method 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 notCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland10Anatomy of a Method Declaration (1)… for methods that do not return valuespublic void setYear (int newYear){year = newYear;}Visibilitymodifiervoidkeywordmethod name parameter listbodyCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland11“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 2006Rance Cleaveland©2006 University of Maryland12Anatomy of a Method Declaration (2)… for methods that return valuespublic boolean isBefore (Date d){… return true;}Visibilitymodifierreturntypemethod name parameter listbodyCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland13“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 typeCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland14Example// "isBefore" returns true if the date is before// the inputted datepublic 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 2006Rance Cleaveland©2006 University of Maryland15Comments … allow you to insert explanations in your code Two form: // blah blah blah /* blah blah blah */ Every: Class declaration Instance variable declaration Method declarationmust have a comment explaining its purpose!CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland16Example// Programmer: Rance Cleaveland// Date:21 Sept. 2006//// This class implements simple operations on dates.public class Date {public int day = 1; // Day part of datepublic int month = 1; // Month part of datepublic int year = 1900; // Year part of datepublic String separator = "/"; // Separator used in printingpublic String note; // 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 valuepublic void setYear (int newYear) {year = newYear;}}CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland17Object 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 objectDate 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
View Full Document