Introduction to Java

Dr Derek Bridge

School of Computer Science & Information Technology

University College Cork

Part of the class hierarchy

Object is at the root of the class hierarchy

The Object class

toString: the true story

Equality testing: the true story


Primitive typesReference types
int, double, boolean arrays, String, Scanner, Dog
Contains a value Contains null or a reference ('pointer') to an object
Displays the value Runs toString
  • toString inherited from Object displays the pointer
  • override toString to display something more meaningful
Copies the value Copies the reference (hence two pointers to the same object)
Equality testing
== tests for equal values
  • == tests for equal references (pointing to the same object
  • equals, inherited from Object, is like ==
  • override equals to compare the objects' instance variables
Passing a parameter to a method
Copies the value, so changes made in the method do not affect the original Copies the reference (hence two pointers to the same object), so changes to the object made in the method do affect the original

Polymorphic data structures, again

A complication

The solution: casting

Run-time type errors