Effective Java 26 Favor generic types】的更多相关文章

Use generic types to replace the object declaration Add one or more type parameters to its declaration. Replace all the uses of the type Object with the appropriate type parameter. Handle the new E[] errorwith two ways : Use the explicit type casting…
Static utility methods are particularly good candidates for generification. The type parameter list, which declares the type parameter, goes between the method's modifiers and its return type. // Generic method public static <E> Set<E> union(S…
Inheritance disadvantage Unlike method invocation, inheritance violates encapsulation. Since you don't know the super class implementation which may involve some unpredictable calling between different methods of super class. And it is difficult to m…
No. Primitives Boxed Primitives 1 Have their own values Have identities distinct from their values 2 Have only fully functional values Have one nonfunctional value which is null 3 Time and space efficient Time and space inefficient Note Applying the…
Benefits to reuse preexisting exceptions It makes your API easier to learn and use. Programs using your API are easier to read. Fewer exception classes mean a smaller memory footprint and less time spent loading classes. Common Exceptions Exception O…
Hi guys, I am happy to tell you that I am moving to the open source world. And Java is the 1st language I have chosen for this migration. It's a nice chance to read some great books like "Effective Java 2nd Edition" and share the note for what I…
Chapter 5 Generics Item 23: Don't use raw types in new code 虽然你可以把一个List<String>传给一个List类型(raw type)的参数,但你不应该这么做(因为允许这样只是为了兼容遗留代码),举个例子: // Uses raw type (List) - fails at runtime! public static void main(String[] args) { List<String> strings…
<Effective Java>目录摘抄. 我知道这看起来很糟糕.当下,自己缺少实际操作,只能暂时摘抄下目录.随着,实践的增多,慢慢填充更多的示例. Chapter 2 Creating and Destroying Objects Consider static factory methods instead of constructors Consider a builder when faced with many constructor parameters Enforce the s…
Java写了很多年,很惭愧,直到最近才读了这本经典之作<Effective Java>,按自己的理解总结下,有些可能还不够深刻 一.Creating and Destroying Objects Consider static factory methods instead of constructors (factory方法可以拥有名称,可以避免重复创建,比如单例模式) Consider a builder when faced with many constructor parameter…
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将近8年的时间,但随着Java 6,7,8,甚至9的发布,Java语言发生了深刻的变化. 在这里第一时间翻译成中文版.供大家学习分享之用. 自Java 5以来,泛型已经成为该语言的一部分. 在泛型之前,你必须转换从集合中读取的每个对象. 如果有人不小心插入了错误类型的对象,则在运行时可能会失败. 使用泛型,你告…