java secrets


a description of not so well known java features

arrays

you can create anonymous arrays if you need to create them on the fly:
void doSomething(int[] array) { ... }
...
doSomething(new int[]{1,2,3,4});
you can use variables and expressions as part of the array initializers. this was not permitted in earlier versions of java.
int a=1;
int[] array = {a,2};

try/catch

you can have a try block without catch clause as long as a finally clause is present. the finally clause will always executed except System.exit() is called before. if you want to execute cleanup code regardless an exception is thrown or not and you don't want to catch it you can use a finally block.
void foo() throws BadException
{
 try
 {
   // something throws BadException here
 }
 finally
 {
   // you can put cleanup code here
 }
}

char

i do not have a reason for that but char is the only primitive datatype in java which is unsigned.

int/float and long/double conversion

int n = (float)2000000999; // 2000001024 will be the result of the implicit conversion
although int and float are bith 32 bit there is a significant precision loss. since between int and float the conversion is implicit you are fooled to think that the original value is preserved

comma expressions

in contrast to c++ in java are no comma expressions allowed in nomal code. but there is one place where they're still allowed: in for(;;) loops.
for (int i=0, j=10; j=i*2, i<10; i++, j++) { }

instance initializers and static initializers

static initializers (class initializers) are executed once when the class is loaded in textual order. instance initializers (nonstatic initializers) are executed everytime the class is instantiated in textual order.
class Test
{
 int myMember = 100; // simple instance initializer
 { System.out.println("instance initializer"); }
 static { System.out.println("static initializer"); }
 static int test = 1234; // this is a simple static initializer
 Test() { System.out.println("constructor"); }
}
consider the following code using the class Test:
Test t1 = new Test();
Test t2 = new Test();
following will be the output in this order:
static initializer
instance initializer
constructor
instance initializer
constructor

postponed initialisation of finals

final variables do not have to be initialized at declaration time. static finals can be initialized in static initializers and instance finals can be initialied in instance initializers or constructors.
class Test
{
 static final int sf;
 static { sf=100;  }
 final int f;
 { f = 200; }
// Test() { f = 200; } // can be initialized in ctor too
}
local finals can be initialized everywhere in the method after its declaration.
void methodFooBar()
{
 final COUNT;
 try
 {
  COUNT = getCountFromDangerousOperation();
 }
 catch (SomeException e)
 {
 }
 // use COUNT
}
note that for some reason you cannot use a postponed initialized final as case label:
//   final int CONSTANT=0; // only this can be used in switches
   final int CONSTANT; // this is not permitted as label in a switch
   CONSTANT=0;
   switch(n)
   {
    case CONSTANT: break;
   }

final static methods

it is possible to declare a static method as final. why? would you ask, because know know that a static method cannot be overridden. indeed, but you can hide a method of the superclass with a method with same signature in the subclass. but if the method in the superclass was declared final the compiler rejects this attempt with an errormessage.
by the way, you can use the keyword final in the main method too.

variable names

it is possible to use class names or method names as variable or label identifiers. so the following program will perfectly compile and run:
public class main
{
  main(){main='\u004a'+main()+'\u0061';}
  String main(){return"av";}
  static String main;
  static String String;
  public final static void main(String[]String)
  {
    main:for(int main=0; main++<new main().main.length(); )
    {
      System.out.println(new main().main);
      break main;
    }
  }
}