Lecture 09: More Objects and Memory

Announcements

  1. No class on Friday
  2. Quiz 02 posted later today

Accountability Groups

Why?

  • Connect with peers in class
    • support structure
  • Discuss material from class
    • what is confusing?
    • what is exciting?
  • Give feedback to me

Last Time

A Simple Number Class

class Number {
    public int value;

    Number (int val) {
	value = val;
    }
}

First Question

What does the following code do?

    Number aNum = new Number(10);
    Number bNum = new Number(5);
    
    bNum = aNum;
    aNum.value = 20;

What are aNum.value and bNum.value at the end of the execution?

Second Question

Consider the method

    void setNumberValue (Number a, int value) {
      a.value = value;
    }

What is the result of

    setNumberValue(aNum, 0);

What is the value of aNum.value at the end of the execution?

What is Going on Here?

Primitive Datatypes

Java has 8 primitive datatypes:

  • byte, short, int, long
  • float, double
  • boolean
  • char

All other datatypes are objects (classes)

  • instances created with keyword new

Value Assignment of Primitive Datatypes

  • Assignment of primitive datatypes copies values
    int a = 10; // assign value of 10 to a
    int b = 5;  // assign value of 5 to b
    
    b = a;      // copy value of a (10), assign to b
    
    a = 20;     // assign value of 20

Passing Primitive Datatypes

  • Method calls take copies of datatype values passed as arguments

    • Copy of value of a passed to setValue(a, 0)
          setValue(a, 0);
    
    • a in method is a local copy; only local copy’s value is changed
          void setValue (int a, int value) {
            a = value;
          }
    
    • original value of a in main is unchanged!

Objects are Different

  • new Number(...) creates a new instance of the Number class

  • aNum stores a reference to the Number

    Number aNum = new Number(10);
    Number bNum = new Number(5);
  • the variable aNum stores the reference, not the Number itself!

Assignment

In this statement

    Number aNum = new Number(10);
    Number bNum = new Number(5);

    bNum = aNum;

the reference stored in aNum is copied and stored in bNum

  • aNum and bNum now refer to the same Number instance

Passing Objects to Methods

In this statement

    setNumberValue(aNum, 0);

the reference stored in variable aNum is passed to setNumberValue

    void setNumberValue (Number a, int value) {
      a.value = value;
    }

The statement a.value = value sets the value

In Pictures

void setNumberValue (Number a, int value) {
    a.value = value;
}

public static void main (String[] args) {
    Number aNum = new Number(10);
    setNumberValue(aNum, 0);
}

Another Puzzle

What happens?

int a = 10;
int b = 10;

if (a == b) {
    System.out.println("equal");
} else {
    System.out.println("not equal");
}

Another Puzzle, with Objects

What happens?

Number aNum = new Number(10);
Number bNum = new Number(10);

if (aNum == bNum) {
    System.out.println("equal");
} else {
    System.out.println("not equal");
}

Why are aNum and bNum not Equal?

How Can we Check Equality of Numbers?

A New Method

public class Number {
    public int value;
	
    public Number (int aValue) {
        value = aValue;
    }
	
    public boolean equals (Number a) {
	    return (value == a.value);
	}
}

Step By Step

Number aNum = new Number(10);
Number bNum = new Number(10);
boolean areEqual = aNum.equals(bNum);

Why References?

  • Only a single copy of the object instance in computer memory
    • more efficient
  • More flexible behavior
    • methods can modify multiple objects
    • make a method “return” multiple values

Drawbacks?

  • Need to be careful not to modify original object instance (if this is not what is intended)

Next Week

  1. Lab 04: Exception Handling
    • handling errors gracefully
  2. Object Inheritance
    • defining relationships between objects