Effective Java 27 Favor generic methods】的更多相关文章

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…
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…
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…
Java Native Interface(JNI) allows Java applications to call native methods, which are special methods written in native programming languages such as C or C++. Historical usages of native methods Access to platform-specific facilities such as registr…
Principle readObject method is effectively another public constructor, and it demands all of the same care as any other constructor. Just as a constructor must check its arguments for validity (Item 38) and make defensive copies of parameters where a…
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…
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将近8年的时间,但随着Java 6,7,8,甚至9的发布,Java语言发生了深刻的变化. 在这里第一时间翻译成中文版.供大家学习分享之用. 27. 消除非检查警告 使用泛型编程时,会看到许多编译器警告:未经检查的强制转换警告,未经检查的方法调用警告,未经检查的参数化可变长度类型警告以及未经检查的转换警告. 你使…