Effective Java Gruppe Gul Items vi vil uddybe: # 15Minimize Mutability # 23 Don’t use raw types in new code # 47Know and use Libraries # 63Include failure-capture.

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

Mutability SWE 332 Fall 2011 Paul Ammann. SWE 3322 Data Abstraction Operation Categories Creators Create objects of a data abstraction Producers Create.
Generic programming in Java
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
Inheritance Inheritance Reserved word protected Reserved word super
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Immutable Objects and Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Java 1.5 & Effective Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
ITP © Ron Poet Lecture 13 1 Helper Objects. ITP © Ron Poet Lecture 13 2 Console Helper Object  We have used Console objects for a while, let’s see what.
Chapter 3 Introduction to Collections – Stacks Modified
Java Generics.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
50.003: Elements of Software Construction Week 8 Composing Thread-safe Objects.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
Effective Java: Generics Last Updated: Spring 2009.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
1 Software Construction Lab 4 Classes and Objects in Java Basics of Classes in Java.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Puzzle 3 1  Write the class Enigma, which extends Object, so that the following program prints false: public class Conundrum { public static void main(String[]
Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n.
Best Practices. Contents Bad Practices Good Practices.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Generic Instructor : Sarah Alodan. Problem?! Java code used to look like this: public class Courses { public static void main(String[] args) { ArrayList.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
Ordered Linked Lists using Abstract Data Types (ADT) in Java Presented by: Andrew Aken.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
Puzzle 1  what does the following program print? public class Puzzle01 { public static void main(String[] args) { System.out.print("C" + "S" + "E"); System.out.println('1'
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 19 Generics.
Java Generics. It is nice if we could write a single sort method that could sort array of any type of elements: – Integer array, – String array, Solution:
From C++ to C# Part 5. Enums Similar to C++ Similar to C++ Read up section 1.10 of Spec. Read up section 1.10 of Spec.
Announcements Final Exam: TBD. Static Variables and Methods static means “in class” methods and variables static variable: one per class (not one per.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Arrays and Array Lists CS 21a. Problem 1: Reversing Input Problem: Read in three numbers and then print out the numbers in reverse order Straightforward.
1 Chapter 21 Generics. 2 Objectives F To use generic classes and interfaces (§21.2). F To declare generic classes and interfaces (§21.3). F To understand.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 10 Thinking.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
CSC 520 – Advanced Object Oriented Programming, Fall, 2010 Thursday, September 30 Week 5, Generics and Inheritance Techniques, Meyer Ch. 10 & 16.
OOP Tirgul 10. What We’ll Be Seeing Today  Generics – A Reminder  Type Safety  Bounded Type Parameters  Generic Methods  Generics and Inner Classes.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
Chapter 10 Thinking in Objects
CSC 243 – Java Programming, Spring 2014
Chapter 10 Thinking in Objects
Chapter 10 Thinking in Objects
HIGHLEVEL REVIEW Chapter 9 Objects and Classes
CS139 – Fall 2010 How far we have come
Object Based Programming
Chapter 9 Thinking in Objects
Generic programming in Java
Chapter 9 Thinking in Objects
CSC 220 – Processing Spring, 2017
Chapter 9 Objects and Classes
CSE 143 Lecture 2 ArrayIntList, binary search
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
slides created by Ethan Apter and Marty Stepp
ITE “A” GROUP 2 ENCAPSULATION.
Presentation transcript:

Effective Java Gruppe Gul Items vi vil uddybe: # 15Minimize Mutability # 23 Don’t use raw types in new code # 47Know and use Libraries # 63Include failure-capture information in detail mesages # A short note on items 7, 31, 39, and 55

Item 15 Minimize Mutability How to make immutable classes 1.Do not provide any methods that modify the object’s state 2.Ensure that the class cannot be extended 3.Make all fields final 4.Make all fields private 5.Ensure exclusive access to any mutable components

ProsCons Constant state throughout lifecycle. Requires separate objects for each distinct value. Thread safe. Easy maintenance. Item 15 Minimize Mutability If absolute immutability is not possible, limit mutability as much as possible

Item 15 Minimize Mutability public final class MyImmutable { public final Køretøj fiat = new Køretøj(); public Køretøj getKøretøj(){ return fiat; } private final int max; private final int min; public MyImmutable(int min, int max){ this.min = min; if(max > min) this.max = max; else{ String msg = “error:max <= min, max = " + max +" & min =" + min; throw new IllegalArgumentException(msg); } public int getMax() { return max; } public static MyImmutable getImmutable(int min, int max){ return new MyImmutable(min, max); } Code Example A The class is final Do not do this! No accessor and no public types Use static methods and private constructors

Effective Java Gruppe Gul Plan # 15Minimize Mutability # 23 Don’t use raw types in new code # 47Know and use Libraries # 63Include failure-capture information in detail mesages # A short note on items 7, 31, 39, and 55

Item 23 Don’t use raw types in new code - A raw type is a generic type without an actual type parameter e.g.: private Collection cars =... - When using raw types one loses all type safety e.g. the following is allowed: cars.add(new Airplane())

Item 23 Issues when using raw types - When getting elements from raw types one must cast them e.g.: Car someCar = (car) cars.get(0) - This example would throw a ClassCastException at runtime as the element at rank 0 is an airplane!

Item 23 Unbounded types and Wildcards - Do not know / care about the element type of a collection? - Raw types might seem like the answer, but are not type safe - Unbounded wildcards achieve the same functionality while remaining both flexible and type safe. e.g.: public void collMethod( Collection coll){…}

Item 23 Old code and Eclipse - As generics were not added to Java prior to the 5th edition one may encounter raw types in older code segments - Eclipse issues a warning when raw types are used in the source code

Effective Java Gruppe Gul Plan # 15Minimize Mutability # 23 Don’t use raw types in new code # 47Know and use Libraries # 63Include failure-capture information in detail mesages # A short note on items 7, 31, 39, and 55

Item 47 Know and use libraries  Use the knowledge of experts and other developers  Don’t waste time on ad hoc solutions  The library is being opdated and optimized continously  The code becomes more mainstream, easier to read, and maintain

Item 47 Know and use libraries Therefore If you have any doubts concerning the content of the libraries – Look it up in the API! If it does not exist – Code it! Attention! Beware of updates in the libraries

Effective Java Gruppe Gul Plan # 15Minimize Mutability # 23 Don’t use raw types in new code # 47Know and use Libraries # 63Include failure-capture information in detail mesages # A short note on items 7, 31, 39, and 55

Item 63 Include failure-capture information in detail messages ProsCons Easier to debugRequires additional coding Easy to maintain Looks more professional

Item 63 Include failure-capture information in detail messages public class MyImmutable2 { private final int min; private final int max; public MyImmutable2(int min, int max) { this.min = min; if(max <= min) { String msg = "reason-: max <= min, max = " + max +" & min =" + min; throw new IllegalArgumentException(msg); } else this.max = max; } public static void main(String[] args) { try{ MyImmutable test = new MyImmutable(2,2); } catch(Exception e){ System.out.println(e); } } //OUTPUT: java.lang.IllegalArgumentException: reason-: max <= min, max = 2 & min =2 } Code Example D

Effective Java Gruppe Gul Plan # 15Minimize Mutability # 23 Don’t use raw types in new code # 47Know and use Libraries # 63Include failure-capture information in detail mesages # A short note on items 7, 31, 39, and 55

Items 7, 31, 39, 55 A short note on items 7, 31, 39, and 55 Item #7 – Finalizers: - Java takes care of garbage collection itself - Equal to C++’s destructors Item #31 – Use Instance Fields instead of Ordianls: - When you use the fields you control the value - The Enum can be expanded without breaking the code

Items 7, 31, 39, 55 A short note on items 7, 31, 39, and 55 Item #39 – Make Defensive copies when needed: - Protects your code from malicious clients - Futhers encapsulation Item #55 – Optimize Judiciously: - Optimization often gets in the way of making working code - Improving your choice of algorithms often is the best way to optimize your program

Effective Java Gruppe Gul Items vi vil uddybe: # 15Minimize Mutability # 23 Don’t use raw types in new code # 47Know and use Libraries # 63Include failure-capture information in detail mesages # A short note on items 7, 31, 39, and 55