Little-known Java features. Second part

    As promised, I bring to your attention the following five points.

    Little-known Java features. Part one

    6. Name conflict.

    If several classes with the same name from different packages are imported, a name conflict occurs. In this case, when referring to the class, its qualified name should be indicated, that is, the full name, including the name of the package, for example java.lang . String .

    Is there really nothing you can do about it? It turns out you can. The following code compiles without problems, despite the fact that the List class is present in both the java.awt package and the java.util package:

    import java.awt.*;
    import java.util.*;
    import java.util.List;
    public class Класс {
    	public static void main(String... аргументы) {
    		List простоСписок = Collections.emptyList();
    		System.out.println(простоСписок);
    	}
    }


    It is enough to import the necessary class, java.util.List in this example.

    Here, as you noticed, Cyrillic identifiers are used. Yes! For some it will be a revelation, but Java ... such Java. The identifier can consist of absolutely any letters, in addition to numbers, underscores and the US currency (however, the last character ($) is not recommended, it is intended for system needs). But do we need it? Unless for the purpose of obfuscation. Just imagine how many different identifiers you can generate from just the “A” characters of the English, Russian and Greek alphabets ...



    7. Initialization of collections.

    What tricks do not have to resort to to simplify the initialization of collections and facilitate the perception of code. Thanks to the variable number of arguments in the method ( varargs ), which appeared in the fifth version of the SDK, as well as a careful update by the developers of the standard API, the situation became a little better:

    List theNumbers = new LinkedList();
    Collections.addAll(theNumbers, 4, 8, 15, 16, 23, 42);


    But this code takes up two lines instead of one and does not seem logically related. You can use third-party libraries, such as Google Collections, or invent your own bike, but there is a neater option:

    List theNumbers = new LinkedList(Arrays.asList(4, 8, 15, 16, 23, 42));


    And with the advent of static imports in the same version of Java, you can shorten this construction by one more word:

    import static java.util.Arrays.*;
    // ...
    List theNumbers = new LinkedList(asList(4, 8, 15, 16, 23, 42));


    However, if the number of elements in the collection does not change, we can write quite simply:

    import static java.util.Arrays.*;
    // ...
    List theNumbers = asList(4, 8, 15, 16, 23, 42);


    These tricks allow you to initialize the collection heirs of the Collection interface, sorry for the pun, but this will not work with cards, unfortunately. Although there is a solution, see the previous part of the article.

    8. Subscriptions.

    The java.util.List interface, from which in particular ArrayList and LinkedList are inherited, has a wonderful method: List.subList (). It returns a new list is not as it may seem, a kind ( view ) list, for which the method was invoked, so that both lists would be to divide the stored items. From this, excellent properties follow:

    someList.subList(3, 7).clear();


    In this example, four elements from the third to the seventh (not inclusive) will be removed from the list of someList.

    Sublists can be used as ranges , which are a pretty powerful programming idiom. How often did you need to go around the collection, excluding the first or last element, for example? Foreach is now even more powerful:

    import static java.util.Arrays.*;
    // ...
    List theNumbers = asList(4, 8, 15, 16, 23, 42);
    int size = theNumbers.size();
    for (Integer number : theNumbers.subList(0, size - 1)) {
    	System.out.print(number + ", ");
    }
    System.out.println(theNumbers.get(size - 1));


    Sublists should be used with caution due to features arising from their essence (see documentation for details).

    9. Cafe babe.

    All compiled classes and interfaces are stored in special files with the extension .class. They contain bytecode interpreted by the Java virtual machine. To quickly recognize these files, they contain, in the first four bytes, a label that looks like this in hexadecimal: 0x CAFEBABE .

    Well, everything is clear with the first word - Java, as you know, was not named after a tropical island, but a coffee of the same name, and among the characters used in the hexadecimal number system, the letters "J" and "V" were not found. But what guided the developers, inventing the second word, we can only guess :)

    10. Exceptional situations.

    And finally, a small piece of code:

    try {
    	throw null;
    } finally {
    	return;
    }


    It throws a NullPointerException and ... gets lost, disappears without a trace! Be carefull.

    Also popular now: