12 mar 2010

Static int


static int x = getValue();
static int y = 5;
static int z = getValue();

static int getValue() {
return y;
}

@Test
public void staticTest() {
prn("x=" + x);//x=0
prn("z=" + z);//z=5
}
  1. Z tego wychodzi ,że inicjalozowane jest z góry na dół!

Unreachable statement


@Test
public void unreachableTest() throws Exception {

int x = 7;

try {
x += x *= x;
prn(x);
} finally {
throw new Exception("sonk");
}
x=8;
}

  1. Poieważ x=8 nigdy nie zostanie wykonany!

Haczyki


enum INDEX{JEDEN,DWA,TRZY};
public void hello() {

int x[][] = new int[2][];
int x[0]={1,2}; //zonk :( compile error

int x[]={1,2,3};
int y= x[INDEX.DWA]; //zonk compile error!!!
}
  1. int a[]={1,2,}; //ok
  2. int a[]; a={1,2,3} //Compile error

Switch,case


@Test
public void swicz() {

//jako argument: byte, short, char, int i enum!
// NIE long ;(

//błąd nr 1
// wartość CASE jest większa niż typu !!!!
byte b = 10;
// switch(b){ case 150: break; }


//błąd nr 2
//zmienna jako CASEnie znana w czasie kompilacji
final int x = 7;
//sytuacja błędówa :D
final int y;
y = 7;
switch (y) {
case x: {
}
//! case y:{} // constant expr required!!!!!
}

//błąd nr 3 => powtarzajace się wartości case
// uwaga na obliecznia wartości case przez consty!
}

@Test
public void testDefaultInSwitch() {

/**
* WNOSIKI:
* 1) Jeśli istnieje case o wartości x to nei bedzie default
* 2) Jeśli nie istnieje case ... to bedzie default
* BEZ wezględu na miejsce!!!
*/
int x = 6;

switch (x) {
default: {
prn("Default");
break;
}
case 6: {
prn("6");
break;
}
}
/*WYNIK 6*/

/////////switch 2
x=5;
switch (x) {
default: {
prn("Default");
}
case 6: {
prn("6");
break;
}
}
/*WYNIK: Default 6*/
}

If &&,||,|,&


@Test
public void test1() {


//kwestia && i & w if
if(True("1") &&True("2") & True("3")){} // 1 2 3
if(False("1") &&True("2") & True("3")){} // 1 !!!!!!!!
if(True("1") &&False("2") & True("3")){} // 1 2 3
if(True("1") &&False("2") && True("3")){} // 1 2
if(False("1") & True("3")){} // 1 3

//kwestia || i |
if(True("1") || False("2")){}//1
if(True("1") | False("2")){}//1 2

if(False("1") ||False("2") && False("3")){} // 1 2
if(False("1") ||False("2") | False("3")){} // 1 2 2

}

public boolean True(String msg ){
System.out.println(msg);
return true;

}
public boolean False(String msg ){
System.out.println(msg);
return false;
}