<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Chan Chen Coding...

    Start Avoiding Some Java 5 Coding Pitfalls

    You may want to keep in mindpitfalls described in this page. They may cause problems at run-time  You may want to keep in mind
    that weren't present in previous Java releases.

    Remember that the new language features are implemented at the compiler level.
    Internally, the compiler generates code that's similar to Java 1.4 code.
    This may result in non-obvious performance or run-time issues.

    AutoboxingAutoboxing conversion is the mechanism for wrapping/unwrapping primitives into/out of their box types (i.e. int --> Integer).

    Pitfalls:

       Don't abuse the auto-boxing features by letting the compiler insert a
    lot of conversions in your code automagically. By doing that you may
    create an out of memory exception or force more frequent or lengthy
    garbage collection cycles.
       Don't test equality of boxed objects through the = or operators.
    Remember that they will work fine only for scalars in range -127 <= n
    <= 128. Use instead:
           if (x.equals(y)) {  }
           if (x.intValue() == y.intValue()) {  }
           if (42 == x.intValue()) {  }
       Arrays of primitives or ArrayList? objects of boxed types are not
    automatically auto-boxed. You must convert back and forth between them
    by traversing the array.
       Overloaded methods will resolve using Java 1.4 instead of Java 5
    rules for argument resolution. You will need to cast the correct type to
    resolve this correctly.
       Null pointer exceptions may result if your code tries to unbox a
    reference to a boxed object that points to null. Add as much
    error-checking code as possible in suspect code!

         public Integer f(int nX) {
           Integer retVal = null;

           if (nX >= 42)
             retVal = 2*nX;

           return retVal;
         }

         // examples:
         Integer y = f(33); // works fine; y can be null
         int nY = f(33); // NullPointerException if f(33) returns null !  Compiler won't notice or care.
       Be careful when writing to an ObjectOutputStream (or reading from an
    ObjectInputStream), especially when implementing readObject() and
    writeObject() for Serializable classes. The following lines are not
    equivalent, because ints and Integers are represented differently in the
    binary data being written:

           int i=;
           objectOutputStream.writeInt(i);     // writes an int
           objectOutputStream.writeObject(i);  // writes an Integer
    Generics (parameterized types)

       A subclass of a generic class must match the superclass' definition exactly or the code won't work. If you want to extend

       class SomeClass<T>

       , the subclass must include the same generic parameter (i.e.

       class NewSubClass<T> extends SomeClass<T>

       ).
       Generic parameters don't follow the same inheritance rules as
    classes. Use bounded types or wildcards instead. In other words, in Java
    1.4 you'd use

       void f(Number n)

       and the argument could be any subclass of Number. In Java 5,

       void f(T t) { }

       will only work with T arguments, so if you defined T as Number in
    the class declaration, it won't work with Integer, Double, etc. If you
    want something to work with the parameter type and with its subclasses,
    you'll have to extend the type parameter explicitly.
       Generic classes don't operate on primitives. Object declarations
    using generics rely on auto-boxing to convert int to Integer; this is
    transparent to the programmer but it may result in run-time resource
    abuse.
       Legacy code may not compile without warnings; use

       Xlint or @SuppressWarnings

       to avoid the warning messages.
       Don't feel like you have to update every collection class to use
    generics! The code we have in production is tried-and-tested. Only move
    the code to generics if it makes sense when you have lots of time to
    test or when you're actually fixing a bug. Otherwise, suppress the
    warnings and move on.

    Static imports

       In general, if you feel like you want to use static imports, let
    your IDE resolve the correct name and scoping. Don't import using the
    wildcard (i.e. import static java.lang.*;) because it may result in
    shadowing or collision issues.

    Enumerated types

       Enumerated types are first-class objects and allow you to define
    methods, constructors, variables, etc. just like any other class. If
    your enumeration gets lots of methods and instance variables, though,
    that may be a sign that you need helper objects instead. Revise your
    implementation.
       Don't rely on the values returned by

       compareTo()

       for scalar operations. The values may change in subsequent implementations/revisions of your enum.
       Double-check any code that relies on enums that you maintain/modify.
    The compiler will not generate any errors if a symbolic constant is
    removed, so the compiler won't detect that you removed it. Code using
    the enum and relying on the enum and that isn't compiled with the new
    version will throw an invalid reference exception when executed with the
    new version of the enum.

    Enhanced 'for' statement

       Convenience may result in performance penalties if you have to
    traverse a big collection or array. In the case of ArrayList? , for
    example, using a basic 'for' statement with an index to the element that
    you want to fetch will run faster than en enhanced 'for' statement.
       Enhanced 'for' loops have no way of keeping track of counters for each iteration.
       You cannot iterate backwards with a for


    -----------------------------------------------------
    Silence, the way to avoid many problems;
    Smile, the way to solve many problems;

    posted on 2012-02-18 15:46 Chan Chen 閱讀(211) 評論(0)  編輯  收藏 所屬分類: Scala / Java

    主站蜘蛛池模板: 亚洲欧美日韩中文无线码 | 狠狠色婷婷狠狠狠亚洲综合| 91亚洲精品视频| 国产一级在线免费观看| 四虎国产精品免费久久影院| 亚洲AV无码专区国产乱码不卡| 成人免费毛片视频| 亚洲变态另类一区二区三区| 女性自慰aⅴ片高清免费| 亚洲一区二区三区亚瑟| 妞干网免费视频观看| 亚洲欧美国产日韩av野草社区| 午夜成人免费视频| 国产精品无码亚洲精品2021| 亚洲美女在线国产| 日批视频网址免费观看| 亚洲黄色在线观看视频| 国产电影午夜成年免费视频| 精品日韩99亚洲的在线发布| 韩国18福利视频免费观看| 免费国产va视频永久在线观看| 亚洲日韩VA无码中文字幕| 中文字幕av无码不卡免费| 亚洲精品成人片在线播放| 1000部啪啪毛片免费看| 456亚洲人成影院在线观| 亚洲国产精品第一区二区三区| 久久久久久噜噜精品免费直播| 91亚洲国产成人久久精品网站 | 久久免费看少妇高潮V片特黄| 国产亚洲综合久久系列| 久久久久久AV无码免费网站下载| 亚洲videos| 免费女人18毛片a级毛片视频| 丁香花在线视频观看免费| 2022年亚洲午夜一区二区福利 | 有码人妻在线免费看片| 亚洲国产精品久久久久| 成人免费视频观看无遮挡| ww在线观视频免费观看w| 亚洲区精品久久一区二区三区|