19 lut 2010

inicjalizacja


@Test
public void testInicjalizacji(){
Init it = new Init();

}

public class Init {

static String ss1;
String sm1;
String sm2;

static {
ss1=new String("'=ok");
prn("Klauzula static");
}
{
sm1=new String("ok");
sm2=new String ("ok");
prn("Klauzula instancyjna{}");
}

public Init() {
prn("Konstruktor");
}
}


Wynik:

Klauzula static
Klauzula instancyjna{}
Konstruktor

Przeciążanie metod


@Test
public void testPrzeciazanieMetod(){

f(5); //int
f(3/2); //int

short s=12;
f(s); //short
f(s-1); //int
f(1-1.0f); //float
f(s-1.0f); //float

byte b=10;
f(b/2); //int
f(b*b); //int
}

public void f(float x){ prn("float");}
public void f(byte x){ prn("byte"); }
public void f(short x){prn("short");}
public void f(int x){prn("int");}

Działania arytmetyczne


@Test
public void testDzialan(){

int x=-3%2;
assertEquals(-1,x ); //pass
x=-3/2;
assertEquals(-1, x); //pass

float y=1.1f;
y++;
assertTrue(y==2.1f); //pass

int i=1;
float f=i; //automatyczna promocja

f=3/2;
prn(f); //1.0
assertTrue(f==1); //pass

x=-1<<1; //przesunięcie ze znakiem
assertTrue(x==-2); //pass

x=4>>>1; //przesuniecie w lewo bez zbaku
assertEquals(x,2); //pass


}

Try,catch,finally


@Test
public void testTry(){

//!try{ } //musi być catch or finally
//! catch(Exception e){ } //musi być try
//!finally{} musi być try przed

try{ }finally{} //ok
try{}catch(Exception e){} //ok
}