📜 ⬆️ ⬇️

Nontrivial Java features

Java is a simple language. And after a year of active use, there are no secrets for you. Quite by chance, I discovered that on stackoverfow people decided to share Hidden Features of Java . It turned out very entertaining, it turned out a kind of rating of non-trivial possibilities, which I will then log in a free translation into Russian.

double brace


The “double brace”, already discussed earlier in the article “The effect of“ double brace ” by zeroed, is leading with a large margin. Detailed description of the method -
http://www.c2.com/cgi/wiki?DoubleBraceInitialization
undoubtedly this is the most fun and unobvious from the list. However, as already noted, the method has its drawbacks in the form of an anonymous class for each use of this method. As well as the impossibility of using the equals () method for such objects.


')

Threadlocal


ThreadLocal - http://java.sun.com/javase/6/docs/api/java/lang/ThreadLocal. html
Undeservedly forgotten class, existing since version 1.2. And becoming even more attractive, making friends with generics. A class allows having one variable to have a different value for each of the threads.

Instance Initializers


If this opportunity is gone from your view, then the example below is excellent and without unnecessary words it will demonstrate it. JLS Link

public class Foo {
public Foo() {
System. out .println( "constructor called" );
}

static {
System. out .println( "static initializer called" );
}

{
System. out .println( "instance initializer called" );
}
}


* This source code was highlighted with Source Code Highlighter .


Perform:
new Foo ();
new Foo ();

At the output we get:
static initializer called
instance initializer called
constructor called
instance initializer called
constructor called

Intersection of class sets as generic type


public class Baz <T extends Foo & Bar> {} - link to JLS

Named Blocks and Labels


Something of little use, but everything is also compiled:
twoCycle:
{
while ( true ) {
while ( true ) {
break twoCycle;
}
}

}


* This source code was highlighted with Source Code Highlighter .


By the way, a funny fact comes to mind about the reserved words const and goto. Which are key, but they can not be used. More tags give a funny effect. The code below compiles perfectly (great loudly, the compiler will not miss such a grunt)
class Example {
public static void main( String [] args) {
System. out .println( "Hello World!" );
http: //Phi.Lho.free.fr

System.exit(0);
}
}


* This source code was highlighted with Source Code Highlighter .


Enum is a class


And in it it is possible to define the designer, static and not so methods. More in detail in JLS. And also any enum has methods that return all values, and also return an object by the string name of the element:

public static E [] values ​​();
public static E valueOf (String name);

finally and return


finally can eat any event - jamesjava.blogspot.com/2006/03/dont-return-in-finally-clause.html
public static int f() {
try {
throw new RuntimeException();
} finally {
return 0;
}
}


* This source code was highlighted with Source Code Highlighter .


Defeat finally can only System.exit (..)

Collections



The asList method in java.util.Arrays has changed significantly since version 1.5. The expression below would not have been possible without a list of variable-length arguments, autoboxing, and generics.
List <Integer> ints = Arrays.asList (1,2,3); </ p>

Types for parameterized methods can be specified as follows:
Collections. < String , Integer> emptyMap ()

static import'y with all the ambiguity can help create the effect of amazing lokanichnosti.
List < String > ls = List ( "a" , "b" , "c" );
List <Map< String , String >> data = List (Map( o( "name" , "michael" ), o( "sex" , "male" )));


Learn more gleichmann.wordpress.com/2008/01/13/building-your-own-literals-in-java-lists-and-arrays and code.google.com/p/google-collections
For jquery fans, use the $ sign: garbagecollected.org/2008/04/06/dollarmaps

And finally, another sub-item - List.subList (int fromIndex, int toIndex) returns the view of the original object.
Documented, but less common possibility. Allows you to work with a sub-list, while changes in the sub-list will be reflected in the parent object.

Class url


Value of expression
  new URL ("http://www.yahoo.com") .equals (new URL ("http://209.191.93.52")) 
- true

Initializing final variable


Final variable initialization may be delayed.
public Object getElementAt( int index) {
final Object element;
if (index == 0) {
element = "Result 1" ;
} else if (index == 1) {
element = "Result 2" ;
} else {
element = "Result 3" ;
}
return element;
}


* This source code was highlighted with Source Code Highlighter .


Thread dump


dump all streams to stdout:
windows: CTRL-Break in the console where the application is running
unix: kill -3 PID

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


All Articles