checking equality of reference variables. arrays and objects are both “reference” types n they...

17
Checking Equality of Reference Variables

Upload: marion-lester

Post on 02-Jan-2016

236 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n

Checking Equality of Reference Variables

Page 2: Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n

Arrays and objects are both “reference” types

They are allocated a chunk of memory in the address space

The memory needs to be initialized Assigning one object/array to another

object/array results in an alias

How do we check the equality of objects? We use the == operator for built-in types Cannot use == for objects

Use instance method equals(). Can define this method for any class. Its your responsibility to include this method if you are designing a new class.

2

Reference Variables and Equality

Page 3: Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n

3

Testing for Reference Variable Equality

String s1 = new String("hello”);String s2 = new String("hello”);System.out.println( s1.equals(s2) ); trueSystem.out.println( s1 == s2 ); falseSystem.out.println( s1 == s1 ); trueString s3 = s1;System.out.println( s1 == s3 ); true

Page 4: Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n

4

Some things can’t be changed: Color objects, for example

Immutability. Can't change a Color object's value once created.

Consequence. Don't need to worry about aliases.

Color a = new Color(160, 82, 45);Color b = a;a = new Color(0, 0, 0);

makes a point to a different Color, but does not change b

no relevant methods in Color API

Page 5: Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n

5

Java objects: Some are Immutable, Some are Not

Immutability. Can't change a Color object's value once created.

We can create a new color from an old one.

We can change the Color object the reference points to.The String class is immutable too.

Mutability. Can change a Picture object's value.

D0

D4

D8

DC

Color red = new Color(255, 0, 0);pic.set(0, 3, red);

no relevant methods in Color API

Page 6: Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n

6

Summary

Object. Holds a data type value; variable name refers to object.

In Java, programs manipulate references to objects.

Exception: primitive types, e.g., boolean, int, double.

Reference types: String, Picture, Color, arrays, everything else.

Page 7: Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n

3.2 Creating Data Types

Page 8: Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n

Recap of the “Height” Class

Height Data:

– Feet (as an integer value) – Inches (as a double value)

Methods:– Set height– Display height

API: Height(int feet, double inches) //constructor

void displayHeight() //instance method

8

Page 9: Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n

Recap of Height.java (Dr. Java)

public class Height { // instance variables int feet;double inches;

// constructorHeight(int setFeet, double setInches) {

feet = setFeet; inches = setInches; } // instance method void displayHeight() {

System.out.println("Height: "+feet+" feet and "+inches+ " inches");}

}

9

Page 10: Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n

Principles of Object Oriented Programming

Encapsulation: Combine data and the functions that operate on that data into a single unit (object)

Why is encapsulation useful?

Data Hiding: Clients should not be able to manipulate data in objects directly

Why is data hiding useful?

Declare instance variables to be “private”– Use getter methods to read data within objects

Any object should be able to invoke the instance methods– Declare instance methods to be “public”

Page 11: Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n

11

Abstract Data Types

Abstract data type. Data type whose internal representation is hidden. Separate implementation from design specification.

Class provides data representation and code for operations.

Client uses data type as black box. API specifies contract between client and class.

Page 12: Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n

12

Summary of Classes

(“Charge” Class Discussed in the Textbook)

Page 13: Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n

Complex Numbers

Page 14: Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n

14

Complex Number Data Type

Goal. Create a data type to manipulate complex numbers.Set of values. Two real numbers: real and imaginary parts.

API.

a = 3 + 4i, b = -2

+ 3i

a + b = 1 + 7i

a b = -18 + i

|a| = 5

Code in Dr. Java

Page 15: Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n

15

Complex Number Data Type: A Simple Client

Client program. Uses data type operations to calculate something.

Remark. Can't write a = b*c since no operator overloading in Java.

public static void main(String[] args) { Complex a = new Complex( 3.0, 4.0); Complex b = new Complex(-2.0, 3.0); Complex c = a.times(b); StdOut.println("a = " + a); StdOut.println("b = " + b); StdOut.println("c = " + c);} % java TestClient

a = 3.0 + 4.0ib = -2.0 + 3.0ic = -18.0 + 1.0i

result of c.toString()

Page 16: Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n

16

Complex Number Data Type: Implementation

public class Complex {

private double re; private double im;

public Complex(double real, double imag) { re = real; im = imag; } public String toString() { return re + " + " + im + "i"; }

public double abs() { return Math.sqrt(re*re + im*im); }

public Complex plus(Complex b) {

double real = re + b.re; double imag = im + b.im; return new Complex(real, imag); }

public Complex times(Complex b) { double real = re * b.re – im * b.im; double imag = re * b.im + im * b.re; return new Complex(real, imag); }}

constructor

instance variables

methods

creates a Complex object,and returns a reference to it

refers to b's instance variable

Page 17: Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n

17

Applications of Data Types

Data type. Set of values and collection of operations on those values.

Simulating the physical world. Java objects model real-world objects. Not always easy to make model reflect reality. Ex: charged particle, molecule, CS101 student, ….

Extending the Java language. Java doesn't have a data type for every possible application. Data types enable us to add our own abstractions. Ex: complex, vector, polynomial, matrix, ....