 The general subject of comparing objects, or object references, can be introduced concretely with strings.  Recall that String is a class, and so strings.

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

ACM/JETT Workshop - August 4-5, Classes, Objects, Equality and Cloning.
Strings Testing for equality with strings.
Logic & program control part 2: Simple selection structures.
AP Computer Science TOPICS TO DISCUSS.equals() == instanceof operator compareTo Interfaces Abstract Classes.
CS0007: Introduction to Computer Programming
Programming 2 CS112- Lab 2 Java
CS 106 Introduction to Computer Science I 01 / 30 / 2008 Instructor: Michael Eckmann.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control if-else and switch statements.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Fundamental Programming Structures in Java: Strings.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
WHAT IS INHERITANCE? Java Unit 11: Inheritance I.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
4.1 Instance Variables, Constructors, and Methods.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
© 2006 Pearson Education 1 Obj: to use compound Boolean statements HW: p.184 True/False #1 – 6 (skip 3)  Do Now: 1.Test your “Charge Account Statement”
1 Data Comparisons and Switch Data Comparisons Switch Reading for this class: L&L 5.3,
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
P Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and.
1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance Interfaces Reading for this Lecture: L&L 6.4 – 6.5.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Introduction to Java Java Translation Program Structure
Operations on Strings. 8/8/2005 Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved L: String Manipulation.
Chapter 7: Characters, Strings, and the StringBuilder.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
Chapter 3 Boolean Expressions Section 3.2 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
CS 106 Introduction to Computer Science I 02 / 01 / 2008 Instructor: Michael Eckmann.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
ITP © Ron Poet Lecture 6 1 More on if. ITP © Ron Poet Lecture 6 2 Remembering Tests  We often want to remember the result of a test, so that we can use.
1 Program Development  The creation of software involves four basic activities: establishing the requirements creating a design implementing the code.
Restrictions Objectives of the Lecture : To consider the algebraic Restrict operator; To consider the Restrict operator and its comparators in SQL.
17-Feb-16 String and StringBuilder Part I: String.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
Sum of Arithmetic Sequences. Definitions Sequence Series.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
ICS102 Lecture 8 : Boolean Expressions King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science.
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
Module 5: I/O and Strings #1 2000/01Scientific Computing in OOCourse code 3C59 Module 5: I/O and STRINGS In this module we will cover The C++ input and.
Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
 Type Called bool  Bool has only two possible values: True and False.
JavaScript: Conditionals contd.
Java Unit 11: Inheritance I
More Sophisticated Behavior
CS0007: Introduction to Computer Programming
Introduction to Computer Science / Procedural – 67130
Debugging and Random Numbers
Control Structures – Selection
Object Oriented Programming (OOP) LAB # 8
Logical Operators & Truth Tables.
Conditions and Ifs BIS1523 – Lecture 8.
Chapter 3: Program Statements
Exception Handling Chapter 9 Edited by JJ.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
CMSC 202 Java Primer 2.
Coding Concepts (Data- Types)
The Data Element.
The Data Element.
Welcome back! October 11, 2018.
CISC101 Reminders Assignment 3 due today.
CMPE212 – Reminders Assignment 2 due next Friday.
Boolean Expressions September 1, 2019 ICS102: The course.
Presentation transcript:

 The general subject of comparing objects, or object references, can be introduced concretely with strings.  Recall that String is a class, and so strings themselves are instances of this class.  The String class implements a method called equals().  This is the method that checks to see whether the contents of two different string objects are the same.

 Let two strings be declared and initialized as follows:  String myString = “Hello World”;  String yourString = “Hello World”;  The situation can be diagrammed in this way:

 Two separate objects exist, containing the same sequence of characters.  Here is an if statement containing a call to the equals() method.  In this situation the condition evaluates to true.  if(myString.equals(yourString))

 If these string values were assigned to the two references, however, the equals() method would return false:  myString = “Tra-la-la”;  yourString = “La-de-dah”;

 The complication comes when you consider the use of the operator ==.  This tests for equality of reference, not equality of contents.  In other words, it tests to see whether the objects themselves are the same, not whether their contents are the same.  I consider this to be a major pitfall for new programmers, so make sure to understand this.

 Let the two strings now be declared and initialized as follows:  String myString = “Hello World”;  String yourString = myString;  if(myString == yourString)  The situation can be diagrammed in this way:

 Only one object exists, with two references to it.  Here is an if statement containing a test of equality of reference.  In this situation the return value would be true.  if(myString == yourString)

 There are other methods in the String class that support various kinds of comparisons.  Among them are compareTo() and equalsIgnoreCase().  The first of these allows you to compare the contents of strings to see which might come first alphabetically.  The second method allows you to check for equality of contents of strings where small and capital letters don’t make a difference. This is done by returning a boolean result.

 This can be useful when taking in input from users.  Complete information on each of these methods can be found in the Java API documentation.

 The same observations that were made for comparing equality of strings can be made for comparing equality of objects in general.  Suppose you have the following two declarations and initializations:  Point2D.Double myPoint = new Point2D.Double(10, 20);  Point2D.Double yourPoint = new Point2D.Double(30, 40);

 The contents are different and the objects are different. Both the equals() method and the == would return false.  Suppose instead that the initializations were as follows:  Point2D.Double myPoint = new Point2D.Double(10, 20);  Point2D.Double yourPoint = new Point2D.Double(10, 20); 

 Now there are two different objects with the same contents.  The equals() method would return true while the == would return false.  Finally consider this case:  Point2D.Double myPoint = new Point2D.Double(10, 20);  Point2D.Double yourPoint = myPoint;

 Now both equals() and == would return true.  The references refer to the same object, and by definition the contents of the object referred to by the references are the same.

 This description of equals() and == applies to system supplied classes.  However, beware: equals() will not work this way with classes you have written.  For example, suppose I have the following objects, created using my own class:  Shampoo myShampoo = new Shampoo();  Shampoo yourShampoo = new Shampoo();

 No matter what the contents of these objects might be, the equals() method will work exactly like the ==.  In other words, for your classes it is possible to call the equals() method, but it only tests for equality of reference, not equality of contents.

 Your class inherits the method equals() from a class above it in the hierarchy.  However, the inherited implementation does not work in the desired way.  How to write an equals() method will be covered in a future unit.