a description of not so well known java features
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};
void foo() throws BadException
{
try
{
// something throws BadException here
}
finally
{
// you can put cleanup code here
}
}
int n = (float)2000000999; // 2000001024 will be the result of the implicit conversionalthough 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
for (int i=0, j=10; j=i*2, i<10; i++, j++) { }
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
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;
}
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;
}
}
}