L04cs2110fa09-6up - 03/09/2009 Primitive vs Reference Types...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
03/09/2009 1 MORE ON SUBCLASSES, INHERITANCE, INTERFACES, ETC Lecture 3 CS2110 – Fall 9 Primitive vs Reference Types ± Primitive types ± int, short, long, float, byte, ± char, boolean, double ± Efficient ± 1 or 2 words ± Not an Object—unboxed 57 abc ± Reference types ± Objects and arrays ± String, int[], HashSet ± Usually require more memory ± Can have special value null ± Can compare null with ==, != ± Generates NullPointerException if you try to dereference null abc nonzero 57 val null next Comparing/copying primitive types ± Works just as you would expect int a, b; if(a < b) { … } a = b+3; Comparing/Copying Reference Types ± Comparing objects (or copying them) isn’t easy! ± You need to copy them element by element ± Compare objects using the “equals” method, which implements “deep equality” What you wrote How to write it correctly "xy" == "xy" xy".equals("xy") "xy" == "x" + "y" "xy".equals("x" + "y") “xy" == new String("xy“) "xy".equals(new String("xy")) Inheritance ± A subclass inherits the methods of its superclass ± Example: methods of the Object superclass: ± equals(), as in A.equals(B) ± toString(), as in A.toString() ± … others we’ll learn about later in the course ± … every object thus supports toString()! Overriding ± A method in a subclass overrides a method in superclass if: ± both methods have the same name, ± both methods have the same signature (number and type of parameters and return type), and ± both are static methods or both are instance methods ± Methods are dispatched according to the runtime type of the actual, underlying object
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
03/09/2009 2 Inheritance and Overriding let us create families of related classes ± For example ± Sets ± Array is a primitive reference type ± ArrayList is a subclass of Set ± HashMap is a subclass of Map ± All of these classes support similar functionality Array vs ArrayList vs HashMap Three extremely useful constructs (see Java API) ± Array ± Storage is allocated when array created; cannot change ± HashMap (in java.util) ± Save data indexed by keys ± Can lookup data by its key ± Extremely fast lookups ± ArrayList ( in java.util) ± An “extensible” array ± Can append or insert elements, access i’th element, reset to 0 length ± Lookup is slower than an array ± Can get an iteration of the keys or values ± Storage allocated as needed but works best if you can anticipate need and tell it at creation time. HashMap Example ± Create a HashMap of numbers, using the names of the numbers as keys: Map<String, Integer> numbers = new HashMap<String, Integer>(); numbers.put("one", new Integer(1)); numbers.put("two", new Integer(2)); numbers.put("three", new Integer(3)); ± To retrieve a number: Integer n = numbers.get("two"); ± Returns null if the HashMap doesn’t contain key ± Can use numbers.containsKey(key) to check this Accessing Array Elements Sequentially public class CommandLineArgs { public static void main(String[] args) {
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/08/2010 for the course CS 2110 taught by Professor Francis during the Spring '07 term at Cornell University (Engineering School).

Page1 / 9

L04cs2110fa09-6up - 03/09/2009 Primitive vs Reference Types...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online