ā¬†ļø ā¬‡ļø

Little known Java features. The second part of

As promised, I offer you the following five points.



Little known Java features. First part



6. Conflict of names.

')

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



Is there really nothing you can do about it? It turns out you can. The following code will compile without problems, despite the fact that the List class is present in the java.awt package and in 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 additionally import the required class, java.util.List in this example.



Here, as you noticed, Cyrillic identifiers are used. Yes! For some, this will be a revelation, but Java ... such Java. The identifier may consist of absolutely any letters, in addition to numbers, underscores and US currency (however, the last sign ($) is not recommended, it is intended for system needs). But we need it? Is it just for obfuscation. Just imagine how many different identifiers you can generate just some of the characters "A" 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 the code. Thanks to the variable number of arguments in the method ( varargs ), which appeared in the fifth version of the SDK, as well as the careful updating by the developers of the standard API, the situation became a little better:



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




But this code takes 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 also a neater option:



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




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



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




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



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




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



8. Sublists.



The java.util.List interface, from which ArrayList and LinkedList inherit from, has a remarkable method: List.subList (). It does not return a new list, as it may seem, but a view of the list for which this method was invoked, so that both lists will separate the stored items. This results in excellent properties:



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




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



Sublists can be used as ranges ( ranges ), which are a fairly powerful programming idiom. How often did you need to bypass the collection, excluding the first or last element, for example? Now foreach becomes even more powerful:



 import static java.util.Arrays.*; // ... List<Integer> 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 the nature of their nature (see the 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 format: 0x CAFEBABE .



Well, with the first word, everything is clear - Java, as you know, was named not in honor of the tropical island, but of the same kind of coffee, and among the characters used in 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; } 




This is where NullPointerException is thrown and ... lost, disappears without a trace! Be carefull.

Source: https://habr.com/ru/post/133357/



All Articles