190 likes | 370 Views
Effective Java Gruppe Gul. Items vi vil uddybe: # 15 Minimize Mutability # 23 Don’t use raw types in new code # 47 Know and use Libraries # 63 Include failure-capture information in detail mesages # A short note on items 7, 31, 39, and 55. Item 15 Minimize Mutability.
E N D
Effective JavaGruppe Gul Items vi vil uddybe: # 15 Minimize Mutability # 23 Don’tuse raw types in new code # 47 Know and use Libraries # 63 Include failure-capture information in detailmesages # A short note on items 7, 31, 39, and 55
Item 15Minimize Mutability How to make immutable classes Do not provide any methods that modify the object’s state Ensure that the class cannot be extended Make all fields final Make all fields private Ensure exclusive access to any mutable components
The class is final Code Example A Item 15Minimize Mutability Do not do this! No accessor and no public types public final class MyImmutable { public final Køretøj fiat = new Køretøj(); public KøretøjgetKø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 MyImmutablegetImmutable(int min, int max){ return new MyImmutable(min , max); } Use static methods and private constructors
Effective JavaGruppe Gul Plan # 15 Minimize Mutability # 23 Don’tuse raw types in new code # 47 Know and use Libraries # 63 Include failure-capture information in detailmesages # A short note on items 7, 31, 39, and 55
Item 23Don’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 23Issues when using raw types - Whengetting elements from raw types one must cast them e.g.: CarsomeCar = (car) cars.get(0) - Thisexamplewouldthrow a ClassCastException at runtime as the element at rank 0 is an airplane!
Item 23Unbounded types and Wildcards - Do not know / careabout the element type of a collection? - Raw types mightseemlike the answer, but are not type safe - Unbounded wildcards achieve the same functionalitywhile remainingbothflexible and type safe. e.g.: public voidcollMethod( Collection<?> coll){…}
Item 23Old code and Eclipse - As genericswere not added to Java prior to the 5th edition onemayencounterraw types in oldercode segments - Eclipseissues a warningwhenraw types areused in the sourcecode
Effective JavaGruppe Gul Plan # 15 Minimize Mutability # 23 Don’tuse raw types in new code # 47 Know and use Libraries # 63 Include failure-capture information in detailmesages # A short note on items 7, 31, 39, and 55
Item 47Know 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 optimizedcontinously The code becomes more mainstream, easier to read, and maintain
Item 47Know and use libraries Therefore If you have anydoubts 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 JavaGruppe Gul Plan # 15 Minimize Mutability # 23 Don’tuse raw types in new code # 47 Know and use Libraries # 63 Include failure-capture information in detailmesages # A short note on items 7, 31, 39, and 55
Item 63Include failure-capture information in detail messages
Code Example D Item 63Include 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 }
Effective JavaGruppe Gul Plan # 15 Minimize Mutability # 23 Don’tuse raw types in new code # 47 Know and use Libraries # 63 Include failure-capture information in detailmesages # A short note on items 7, 31, 39, and 55
Items 7, 31, 39, 55A short note on items 7, 31, 39, and 55 Item #7 – Finalizers: - Java takescare of garbagecollectionitself - Equal to C++’s destructors Item #31 – UseInstance Fields instead of Ordianls: - Whenyouuse the fieldsyoucontrol the value - The Enumcanbeexpandedwithoutbreaking the code
Items 7, 31, 39, 55A short note on items 7, 31, 39, and 55 Item #39 – Make Defensive copieswhenneeded: - Protectsyourcode from maliciousclients - Futhersencapsulation Item #55 – OptimizeJudiciously: - Optimizationoftengets in the way of makingworkingcode - Improvingyourchoice of algorithmsoften is the bestway to optimizeyour program
Effective JavaGruppe Gul Items vi vil uddybe: # 15 Minimize Mutability # 23 Don’tuse raw types in new code # 47 Know and use Libraries # 63 Include failure-capture information in detailmesages # A short note on items 7, 31, 39, and 55