{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

L04cs2110fa09-6up

L04cs2110fa09-6up - Primitive vs Reference Types Primitive...

Info icon This 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
Image of page 1

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

View Full Document Right 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) { System.out.println(args.length); // old-style for (int i = 0; i < args.length; i++) { System.out.println(args[i]); } // new style for (String s : args) { System.out.println(s); } } } Generics and Autoboxing Old (pre-Java 5) Map numbers = new HashMap(); numbers.put("one", new Integer(1)); Integer s = (Integer)numbers.get("one"); New (generics)
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern