1 / 38

CS221

Week 1 - Friday. CS221. Last time. What did we talk about last time? OOP Encapsulation Dynamic dispatch Polymorphism Inheritance Self-reference Java stuff Interfaces Exceptions Threads. Questions?. Assignment 1. Static. What is static?.

ralph
Download Presentation

CS221

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Week 1 - Friday CS221

  2. Last time • What did we talk about last time? • OOP • Encapsulation • Dynamic dispatch • Polymorphism • Inheritance • Self-reference • Java stuff • Interfaces • Exceptions • Threads

  3. Questions?

  4. Assignment 1

  5. Static

  6. What is static? • There are three ways that static can be used in Java • Static methods • Static members • Static inner classes • "Staticness" is a confusing concept, but it boils down to missing a connection to a specific object

  7. Static methods • A static method is connected to a class, not an object • Thus, static methods cannot directly access non-static members • You also can't use this inside them • Static methods can indirectly access members since they have the privileges to access private and protected data • You just have to pass them an object of the class they're in • Static methods are slightly more efficient since they do not have dynamic dispatch • Thus, they cannot be overridden, only hidden

  8. Static methods public class X { private int x; public static void print() { System.out.println("X"); //x = 5; //previous line would not compile //if uncommented } } public classY extends X { public static void print() { System.out.println("Y"); } }

  9. Static methods X x = new X(); Y y = new Y(); X z; x.print(); //prints X y.print(); //prints Y z = x; z.print(); //prints X z = y; z.print(); //prints X

  10. Static members • A static member is stored with the class, not with the object • There is only ever one copy of a static member • Static members are a kind of global variable • They should be used very rarely, for example, as a way to implement the singleton design pattern • Static members can be accessed by static methods and regular methods

  11. Static members public class Balloon { private String color; private intsize; private static inttotalBalloons = 0; public Balloon(String color, intsize) { this.color = color; this.size = size; totalBalloons++; } public static String getColor() { return color; } public static intgetBalloons() { return totalBalloons; } }

  12. Inner Classes

  13. Static inner classes • The simplest kind of inner class is a static inner class • It is a class defined inside of another class purely for organizational purposes • It cannot directly access the member variables or non-static methods of a particular outer class object

  14. Static inner class example public classLinkedList { private Node head; private static class Node { public intvalue; public intNode next; } } • In this example, the Node class is used like a struct from C or C++ to hold values

  15. Inner classes • A non-static inner class is connected to a specific outer class object • It can directly access the members and non-static methods of the outer class Outer Inner Inner Inner Inner Inner

  16. Inner class example public classLinkedList { private Node head; private intsize; private class Node { public intvalue; public intNode next; public Node() { if( size > 100 ) System.out.println( "Your list is too long!"); } } }

  17. Creating inner classes • If a static inner class is public, you can create it directly • However, a non-static inner class requires an instance of the outer class to be created (with weird syntax) • Inside the outer class, it is not necessary to give a reference to the outer class, since this is assumed Outer.StaticInner inner; inner = newOuter.StaticInner(); Outer outer = new Outer(); Outer.Inner inner = outer.new Inner();

  18. References

  19. Pointers in Java • Technically, Java doesn't have pointers • Instead, every object in Java is referred to with a reference • A reference is just an arrow that points at an object • A reference can point at nothing (null) • A primitive type can never be null

  20. How should you think about this? • Picture a ham… • Imagine that this ham is actually a Java object • You may want a reference of type Ham to point at this ham • Let’s call it ham1 ham1

  21. How many hams? ham2 • Now, what if we have another Ham reference called ham2 • What happens if we set ham2 to have the same value as ham1 using the following code? Ham ham2 = ham1; ham1

  22. There is only one ham! • When you assign an object reference to another reference, you only change the thing it points to • This is different from primitive types • When you do an assignment with primitive types, you actually get a copy x y intx = 37; inty = x; 37 37

  23. Reference vs. primitive variables • Since reference variables are only pointers to real objects, an object can have more than one name • These names are called aliases • If the object is changed, it doesn’t matter which reference was used to change it

  24. Ham solo ham2 • Thus, if we tell ham2 to take a bite away, it will affect the ham pointed at by ham1 • Remember, they are the same ham ham2.bite(); ham1

  25. Remember that primitives make copies • We have intvalues x and y, both with value 37 • If we change x, it only affects x • If we change y, it only affects y intx = 37; inty = x; x++; y--; x y 38 36 37 37

  26. The clone() method • Sometimes you want to make a full copy of an object • Every object has a clone() method that allows you to do this • clone() is intended to make a deep copy instead of a shallow copy • Ideally, all the objects inside of the object are cloned as well • There is no way to guarantee that clone() gives deep copies for arbitrary objects • clone() works well for Java API objects • You have to write your own if you want your objects to work right • Doing so can be tricky

  27. Generics

  28. Generics • Allow classes, interfaces, and methods to be written with a generic type parameter, then bound later • Java does the type checking (e.g. making sure that you only put Strings into a List<String>) • After typechecking, it erases the generic type parameter • This works because all classes extend Objectin Java • Appears to function like templates in C++, but works very differently under the covers • Most of the time you will use generics, not create them

  29. Generic classes • You can make a class using generics • The most common use for these is for container classes • e.g. you want a List class that can be a list of anything • JCF is filled with such generics

  30. Generic class example public class Pair<T> { private T x; private T y; public Pair(T a, T b ) { x = a; y = b; } public T getX() { return x; } public T getY() { return y; } public void swap() { T temp = x; x = y; y = temp; } public String toString() { return "( " + x + ", " + y + " )"; } }

  31. Generic class use public class Test { public static void main(String[] args) { Pair<String> pair1 = new Pair<String>("ham", "eggs"); Pair<Integer> pair2 = newPair<Integer>( 5, 7 ); pair1.swap(); System.out.println( pair1 ); System.out.println( pair2 ); } }

  32. JCF Java Collections Framework

  33. Container interfaces • Collection • Iterable • List • Queue • Set • SortedSet • Map • SortedMap

  34. Container classes • LinkedList • ArrayList • Stack • Vector • HashSet • TreeSet • HashMap • TreeMap

  35. Tools • Collections • sort() • max() • min() • replaceAll() • reverse() • Arrays • binarySearch() • sort()

  36. Upcoming

  37. Next time… • No class on Monday! • I will talk about computational complexity • Read Chapter 2

  38. Reminders • Read Chapter 2 • Work on Assignment 1 • Due next Friday by 11:59pm

More Related