📜 ⬆️ ⬇️

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 runs out of itself - a call to the habra-java-community : “Don't be jealous!”, Add this list in the comments, share your experience. For now - a continuation of the previous article , after editing, dubbing and diluting with your own experience:

JDK tools


Almost everyone knows that an impressive number of tools are included in the JDK distribution. However, in everyday work, I think many people do not use it except java, javac, jar. What else can be taken from the bin folder:


Java VM


JVM can run code compiled into byte code 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 you to define a new type at runtime (runtime) using the specified interface.

Do you know that:



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

- This keyword allows access to fields and methods from an inner class. For example, in the case where 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) {
same as
if (aObject instanceof String)

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

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

- The class String is the only type, besides the primitives, for which + and + = work. In this case, the str = str + "1" construction is replaced by the compiler by 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. You can check double for NaN using Double.isNan () - gribozavr

Semi-closed constructors



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

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

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

public class User { // User final
protected User( int id) {…}
public User( String name) {
this (getId(name));
}
}


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

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


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

Self-bound generics



class SelfBounded <T extends SelfBounded> {
}

From Bruce Ekkel's staty www.artima.com/weblogs/viewpost.jsp?thread=136394
In the load HUGE FAQ for generics - all that you were afraid to ask: www.angelikalanger.com/GenericsFAQ/FAQSections/TypeParameters.html

And again about enum



Enum can implement 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


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

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

A 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 .

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


All Articles