12 mar 2010

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;
}

9 mar 2010

Overriding i overloading

Following code fragment is given (Java 5):

1: class Human {
2:
Human getIt() {
3:
return this;
4:
}
5:
}
6:
7:
class Worker extends Human {
8:
// insert a single method here
9:
}

Which of the following methods would compile if inserted at line 8

Human getIt() { return this; }
Worker getIt() { return this; }
Object getIt() { return this; }
int getIt() { return 1; }
int getIt(int x) { return 1; }
Object getIt(int x) { return this; }


Try catch finally



  1. A try block can be followed either by a catch block or a finally block, or both.

    A catch block must always be associated with a try block.

    A finally block can never stand on its own (that is, without being associated with try block).

Haczyki v2


if(20%4){} //zonk bo wynikiem jest int!!


String s = null; /* Nie bedzie NullPointerException!!!!!!!!!*/
String result = s != null && s.equals("test") ? "yes" : "no";

  1. Kwestia || i | w jednym ifie + kwsestia && i & w jednym ifie
  2. Jeśli casy w switchu się powtórzą to! => compilation error!!!!!!!!!

8 mar 2010

Println


System.out.println(1+2+"sd");//3sd
System.out.println("_"+12+3); //_123
System.out.println("_"+(1+2+3)); //_6