代码改变世界

Effective Java 51 Beware the performance of string concatenation

2014-04-14 10:47 by 小郝(Kaibo Hao), 354 阅读, 0 推荐, 收藏, 编辑
摘要:Don't use the string concatenation operator to combine more than a few strings unless performance is irrelevant. Use StringBuilder's append method instead. Alternatively, use a character array, or process the strings one at a time instead of combining them. 阅读全文

Effective Java 50 Avoid strings where other types are more appropriate

2014-04-13 18:41 by 小郝(Kaibo Hao), 389 阅读, 0 推荐, 收藏, 编辑
摘要:Avoid the natural tendency to represent objects as strings when better data types exist or can be written. Used inappropriately, strings are more cumbersome, less flexible, slower, and more error-prone than other types. Types for which strings are commonly misused include primitive types, enums, and aggregate types. 阅读全文

Effective Java 49 Prefer primitive types to boxed primitives

2014-04-12 08:58 by 小郝(Kaibo Hao), 794 阅读, 0 推荐, 收藏, 编辑
摘要:Use primitives in preference to boxed primitives whenever you have the choice. Primitive types are simpler and faster. Autoboxing reduces the verbosity, but not the danger, of using boxed primitives. When your program compares two boxed primitives with the ==operator, it does an identity comparison, which is almost certainly not what you want. When your program does mixed-type computations involving boxed and unboxed primitives, it does unboxing, and when your program do 阅读全文

Effective Java 48 Avoid float and double if exact answers are required

2014-04-11 14:49 by 小郝(Kaibo Hao), 390 阅读, 0 推荐, 收藏, 编辑
摘要:Don't use float or double for any calculations that require an exact answer. Use BigDecimal if you want the system to keep track of the decimal point and you don't mind the inconvenience and cost of not using a primitive type. 阅读全文

Effective Java 47 Know and use the libraries

2014-04-10 07:25 by 小郝(Kaibo Hao), 317 阅读, 0 推荐, 收藏, 编辑
摘要:Don't reinvent the wheel. If you need to do something that seems like it should be reasonably common, there may already be a class in the libraries that does what you want. If there is, use it; if you don't know, check. Economies of scale dictate that library code receives far more attention than most developers could afford to devote to the same functionality. 阅读全文

Effective Java 46 Prefer for-each loops to traditional for loops

2014-04-09 08:01 by 小郝(Kaibo Hao), 340 阅读, 0 推荐, 收藏, 编辑
摘要:The for-each loop provides compelling advantages over the traditional for loop in clarity and bug prevention, with no performance penalty. You should use it wherever you can. 阅读全文

Effective Java 45 Minimize the scope of local variables

2014-04-08 19:45 by 小郝(Kaibo Hao), 435 阅读, 0 推荐, 收藏, 编辑
摘要:By minimizing the scope of local variables, you increase the read-ability and maintainability of your code and reduce the likelihood of error. 阅读全文

Effective Java 44 Write doc comments for all exposed API elements

2014-04-07 17:45 by 小郝(Kaibo Hao), 405 阅读, 0 推荐, 收藏, 编辑
摘要:Documentation comments are the best, most effective way to document your API. Their use should be considered mandatory for all exported API elements. Adopt a consistent style that adheres to standard conventions. Remember that arbitrary HTML is permissible within documentation comments and that HTML meta characters must be escaped. 阅读全文

Effective Java 43 Return empty arrays or collections, not nulls

2014-04-06 19:44 by 小郝(Kaibo Hao), 604 阅读, 0 推荐, 收藏, 编辑
摘要:There is no reason ever to return null from an array or collection-valued method instead of returning an empty array or collection. 阅读全文

Effective Java 42 Use varargs judiciously

2014-04-05 18:21 by 小郝(Kaibo Hao), 476 阅读, 0 推荐, 收藏, 编辑
摘要:Varargs methods are a convenient way to define methods that require a variable number of arguments, but they should not be overused. They can produce confusing results if used inappropriately. 阅读全文

Effective Java 41 Use overloading judiciously

2014-04-04 18:45 by 小郝(Kaibo Hao), 398 阅读, 0 推荐, 收藏, 编辑
摘要:You should generally refrain from overloading methods with multiple signatures that have the same number of parameters. You can name the method with same prefix rather than overloading the write method. Such as , these variants of ObjectOutputStream have signatures like writeBoolean(boolean), writeInt(int), and writeLong(long). 阅读全文

Effective Java 40 Design method signatures carefully

2014-04-03 08:53 by 小郝(Kaibo Hao), 432 阅读, 0 推荐, 收藏, 编辑
摘要:Choose method names carefully.Don't go overboard in providing convenience methods.Avoid long parameter lists.Break the method up into multiple methods. Such as sublist element of List interface.Create helper classes to hold groups of parameters.Adapt the builder pattern form object construction to method invocation.See item For parameter types, favor interfaces over classes.Prefer two-element enum types to boolean parameters. 阅读全文

Effective Java 39 Make defensive copies when needed

2014-04-02 08:44 by 小郝(Kaibo Hao), 529 阅读, 0 推荐, 收藏, 编辑
摘要:If a class has mutable components that it gets from or returns to its clients, the class must defensively copy these components. If the cost of the copy would be prohibitive and the class trusts its clients not to modify the components inappropriately, then the defensive copy may be replaced by documentation outlining the client's responsibility not to modify the affected components. 阅读全文

Effective Java 38 Check parameters for validity

2014-04-01 18:29 by 小郝(Kaibo Hao), 502 阅读, 0 推荐, 收藏, 编辑
摘要:Each time you write a method or constructor, you should think about what restrictions exist on its parameters. You should document these restrictions and enforce them with explicit checks at the beginning of the method body. 阅读全文

Effective Java 37 Use marker interfaces to define types

2014-03-31 09:03 by 小郝(Kaibo Hao), 556 阅读, 0 推荐, 收藏, 编辑
摘要:If you find yourself writing a marker annotation type whose target is ElementType.TYPE, take the time to figure out whether it really should be an annotation type, or whether a marker interface would be more appropriate. 阅读全文

Effective Java 36 Consistently use the Override annotation

2014-03-30 12:35 by 小郝(Kaibo Hao), 459 阅读, 0 推荐, 收藏, 编辑
摘要:The compiler can protect you from a great many errors if you use the Override annotation on every method declaration that you believe to override a supertype declaration, with one exception. In concrete classes, you need not annotate methods that you believe to override abstract method declarations(though it is not harmful to do so). 阅读全文

Effective Java 35 Prefer annotations to naming patterns

2014-03-29 22:25 by 小郝(Kaibo Hao), 477 阅读, 0 推荐, 收藏, 编辑
摘要:There is simply no reason to use naming patterns now that we have annotations. All programmers should, however, use the predefined annotation types provided by the Java platform(Items 36 and Item24). 阅读全文

Effective Java 34 Emulate extensible enums with interfaces

2014-03-28 16:54 by 小郝(Kaibo Hao), 526 阅读, 0 推荐, 收藏, 编辑
摘要:While you cannot write an extensible enum type, you can emulate it by writing an interface to go with a basic enum type that implements the interface. 阅读全文

Effective Java 33 Use EnumMap instead of ordinal indexing

2014-03-27 15:15 by 小郝(Kaibo Hao), 562 阅读, 0 推荐, 收藏, 编辑
摘要:It is rarely appropriate to use ordinals to index arrays: use EnumMap instead. If the relationship that you are representing is multidimensional, use EnumMap<..., EnumMap<...>>. 阅读全文

Effective Java 32 Use EnumSet instead of bit fields

2014-03-26 19:25 by 小郝(Kaibo Hao), 634 阅读, 0 推荐, 收藏, 编辑
摘要:Because an enumerated type will be used in sets, there is no reason to represent it with bit fields. The EnumSet class combines the conciseness and performance of bit fields with all the many advantages of enum types described in Item 30. You can wrap an EnumSet with Collections.unmodifiable Set, but conciseness and performance will suffer. 阅读全文
上一页 1 2 3 4 5 6 7 8 9 ··· 11 下一页