Other Java features

    I continue to translate the source of the first article . More and more obvious things, in spite of this, I nevertheless decided that repetition is not such a bad thing.

    And if the source exhausts itself - a call to the  habr-java community : “Don’t be jealous!”, Add this list in the comments, share your experience. In the meantime, the continuation of the previous article , after installation, dubbing and dilution with our own experience:

    Jdk tools


    Almost everyone is aware that an impressive number of tools are included in the JDK package. However, in everyday work, I think many people except java, javac, jar do not use. What else can you take from the bin folder:


    Java VM


    JVM can run code compiled into bytecode from other programming languages ​​such as Jython , JRuby , Groovy , Scala

    System tray


    Starting with Java 1.6, you can add an icon to the system tray: http://java.sun.com/docs/books/tutorial/uiswing/misc/systemtray. html

    Dynamic proxies


    Dynamic proxies added in 1.3 allow to determine at runtime a new type by the specified interface.

    Do you know that:



      - Fast copying of arrays is possible through the native method System.arraycopy (...)

      - The keyword this allows you to access fields and methods from the inner class. For example, in the case when the inner class has a method or field with the same name: OuterClass.this.methodName () or OuterClass.this.fieldName.
    * Only in the case of a non-static inner class -  gribozavr

      - Java 1.5 received variable-length argument lists:
    public void foo(String... bars) {
      for (String bar: bars)
       System.out.println(bar);
    }


      - checking for null before instanceof is optional:
    if (null! = AObject && aObject instanceof String) {the
    same as
    if (aObject instanceof String)

      - There is a magic serialization of a Serializable, but not Externalizable object via private writeObject, readObject
    java.sun methods. com / developer / technicalArticles / Programming / serialization

      - Classes for primitives can be obtained using int.class, float.class ...

      - The String class is the only type, in addition to primitives, for which + and + = work. The construction str = str + “1” is replaced by the compiler with str = new StringBuffer (str) .append (“1”). ToString (). Are you still concatenating strings in a loop?
    * Starting with Java 1.5, StringBuilder is used . -  malkolm

      - C-style sprintf is possible in java, try String.format () .
    String w = «world»;
    String s = String.format(«Hello %s %d», w, 3);


      - For float i = Float.NaN - the expression i == i is false
    * This is an IEEE 754 requirement. Double can be checked for NaN using Double.isNan () -  gribozavr

    Semi-Closed Constructors



    Source: javaekspert.blogspot.com/2007/11/semi-private-constructors.html

    Very often there is a need to make the creation of an object inaccessible to other classes (for example, when using factories) or to block access to some of the constructors, declaring them “private property” object. However, you have to step on your own rake during unit testing - it is impossible to create an object.

    Semi-closed constructors come to the rescue, using protected constructors and anonymous classes. Example:
    If the User class wants to hide one of its User (int id) constructors, leaving only User (String name) available, this is its right. However, declaring the class as follows (protected, not private):

    public class User { // важно что User не final
      protected User(int id) {…}
      public User(String name) {
        this(getId(name));
      }
    }


    we leave a loophole for the subsequent use of the first constructor, namely:

    public class TestUser {  
      public void testUserIdConstructor() {    
        User hack = new User(1) {};  
      }
    }


    And the variable hack was assigned an object of an anonymous class inherited from User, in addition, by calling the constructor User (1).

    Self-bound generics



    class SelfBounded{
    }

    From the Bruce Eckel article www.artima.com/weblogs/viewpost.jsp?thread=136394
    The LOUGH FAQ on generics is all you were afraid to ask: www.angelikalanger.com/GenericsFAQ/FAQSections/TypeParameters.html

    And again about enum



    Enum can implement the interface

    public interface Room {
      public Room north();
      public Room south();
      public Room east();
      public Room west();
    }

    public enum Rooms implements Room {
      FIRST {
        public Room north() {
          return SECOND;
        }
      },
      SECOND {
        public Room south() {
          return FIRST;
        }
      }

      public Room north() { return null; }
      public Room south() { return null; }
      public Room east() { return null; }
      public Room west() { return null; }
    }


    Black magic


    In another way, to call the language does not turn. Drum roll ...
    The sun.misc.Unsafe class allows you to implement direct memory management in your application. You can:
    1. Create an object without calling the constructor
    2. Throw an Exception without defining this Exception in the throws list.
    3. Allocate, free, copy memory blocks
    4. Take and return object monitor (lock / unlock) without synchronized declaration
    5. Declaring a class from bytecode  - this item upsets people working with custom ClassLoader

    Why is this all, I want to ask? I can’t find an answer, and I think pampering with this class can kill the JVM

    Source -
    http://www.docjar.com/html/api/ClassLib/Common/sun/misc/Unsafe.java. html
    and another interesting link, already in Russian -
    http://wasm.ru/article.php?article=unsjav1
    http://wasm.ru/article.php?article=unsafe_ii
    use in open source:
    google code search

    * Source code was highlighted with Source Code Highlighter .

    Also popular now: