13 mar 2010

String haczyk


String s1 = "abc";
String s2 = "abc";
if(s1 == s2)
System.out.println(1); //true!!!!!


//zaś
String test2 = "abcd";
String test = new String("abcd");
/*to 2 różne referencje !!!!!!!*/

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

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

6 mar 2010

== i Wrappery

  1. == zwraca true jesli prymitywy są równe i wrapper={Boolean,Byte, Integer<-127;128>}
  2. Jeśli wrapper nie należy do powyższego to == porównuje referencje!


//uwaga na != i equals na wrapperach
//!= referencje
//equals -> prymitywy!
Integer i=1000;
Integer j=1000;
if(i!=j)
prn( "rózne obiekty"); //wyświetli
assertEquals(i, j); //pass bo 100==100

Integer i2=100;
Integer j2=100;
if(i2!=j2)
prn( "rózne obiekty"); // NIE wyświetli
assertEquals(i, j); //pass bo 100==100

Wrappers


//ok
Boolean b=true;
Boolean b2 = new Boolean("True");
Boolean b22 = new Boolean("trUe");
assertEquals(b22, b2); //pass
assertTrue(b22==true); //pass

Boolean b3= new Boolean(null);
assertTrue(b3==false); //pass

Boolean b4= new Boolean("Yes");
assertTrue(b4==false); //pass

if(b4){/*Do sth:D*/} //ok

//Integer
Integer x=Integer.valueOf("1011", 2);
prn(x); //11
//Float
float f= Float.valueOf("3.14f");
prn(f); //3.14

Inicjalizacja tablic

Jak mamy tablicę intów to nie możemy do niej przypisać tablucy charów!!!


zaś jeśli chodzi o tablice referencji to można przypisać tablicę subtypu do base typu!!



// assertEquals(x, y); //fail ok2 ok
//al ok!!
int[][][] xx= new int[9][5][];
int [][][] xxx= new int[9][][];

int[][][][] xxxx= new int[9][][][];
xxxx[3]=new int[2][][]; //ok
//! xxxx[3]=new int[2][][][]; //nie ok

//uwaga nr 2
//int y=-4;
// x[y]=2; //runtime!! exception

//uwaga nr 1
int[] u2 = new int[5];
// u2[29]=4; // runtime exception!!!!v

//init
int zm=9;
int zmTab[] = {zm,zm,9}; //ok


//size test
int[] tab1={1,};
int[] tab2={1};
assertEquals(tab1.length, tab2.length);//pass

//! int x[] = new int[3]{1,2,3}; bez rozmiaru!!!

Inicjalizacja tablic


// assertEquals(x, y); //fail ok2 ok
//al ok!!
int[][][] xx= new int[9][5][];
int [][][] xxx= new int[9][][];

int[][][][] xxxx= new int[9][][][];
xxxx[3]=new int[2][][]; //ok
//! xxxx[3]=new int[2][][][]; //nie ok

//uwaga nr 2
//int y=-4;
// x[y]=2; //runtime!! exception

//uwaga nr 1
int[] u2 = new int[5];
// u2[29]=4; // runtime exception!!!!v

//init
int zm=9;
int zmTab[] = {zm,zm,9}; //ok


//size test
int[] tab1={1,};
int[] tab2={1};
assertEquals(tab1.length, tab2.length);//pass

//! int x[] = new int[3]{1,2,3}; bez rozmiaru!!!

String, kopia referencji

  // @Test
public void test1(){

String m="male";
String d=m.toUpperCase();

//! assertEquals(d, m); //fail

//test 2
String x="ok";
String y=x;

assertEquals(x, y);//pass
x+="2";
assertEquals(x, y);//fail ok2 ok
x=x+"2";

// assertEquals(x, y); //fail ok2 ok
}

@Test
public void test2(){
String x= "Out";
reassignStr(x);
assertEquals(x, "In"); // fail Out In
//Wniosek:
//Metoda bierze KOPIE referencji!
//nie ma wpływwu na referencje x!!
}
private void reassignStr(String ref){
ref= "In";
}

Promocja typów - rozszerzająca


• byte to short, int, long, float, or double
• short to int, long, float, or double
• char to int, long, float, or double
• int to long, float, or double
• long to float or double
• float to double

1 mar 2010

Haczyki


  1. Metoda o identycznej nazwie jak konstruktor ( np z void)
  2. Return char gdy metoda zwraca inta -> ok dozwolone
  3. Zwracanie null kiedy powinniśmy zwracać prymitywa.
  4. Do tablicy prowadzi referencja.
  5. Jeżeli stworzyłeś konstruktor z argumentami to już nie ma tego domyślnego bez argumentów
  6. Czy klasa abstrakcyjna ma konstruktor? -> Tak!
  7. Czy interfejs ma konstrutor? -Nie!
  8. Domyślny konstruktor ma taki sam access mod jak klasa!
  9. W konstruktorze nei może być jednocześnie this(..) i super(..)
  10. Metody static nie sa overriden lecz tylko po typie referencji!!

  11. instanceof tylko dla rferencji i klasy z tego samego drzewa dziedziczenia !

  12. long is illegal in switch statement!
  13. null isntance of [jakakolwiek kalsa] => false !! ( także pośrednio)
  14. [Dog object] instance of Cat => compilation error!
  15. argument byte not passing to method(Long)
  16. int x => wybierze Object x niż Integer... (var-arg)
  17. List nie możesz dodać Dog or Cat !!
  18. List poprawne!!
  19. List oznacza każdy rodzaj Listy!!!



//haczyki
String[] czyDozwolonyNull(){ return null; } //ok
int[] czyDozwolonyNullDlaPrymiTywa(){ return null; } //ok
}

abstract class CzykonstruktorMa {

public CzykonstruktorMa() {
}
}

class Konkret{
int dupeczka;

public Konkret() { }

/*To jest metoda a nie konstruktor!!!*/
public void Konkret(){}

public Konkret(int dupeczka) {
Konkret(); //metoda e nie konstruktor
this.dupeczka = dupeczka;
}
}


class Base{
int x; int y;

public Base(int x, int y) {
this.x = x; this.y = y;
}
}

//!class Extended extends Base{ } zonk bo
//nie ma domyślnego konstruktora

/*Teraz jest ok*/
class Extended extends Base{

public Extended(int x, int y) {
super(x, y);
}
}

Poliporfizm


  1. Tylko metody sąwywoływane dynamicznie ( po typie obiektu na który wskazuje referencja)
  2. Zmienne static i nonstatic nie są dynamicznie wybierane, wybieran są ząś po typie referencji!!
  3. Overloading nie po typie referencji!!
  4. Przy oveloading można dowolnie zmieniać modyfikator dostępu jak i zwracaną wartość.

instanceof


@Test
public void testInstanceOf() {

//assertTrue(null instanceof Object); false
//assertTrue(null instanceof String); false

//!!assertTrue(int instanceof Object); compilation Error
//@assertTrue(String instanceof Object); błąd kompilacji

//primitives
int x = 6;
//assertTrue(x instanceof Object); zonk :)
/**
* Wnioske:
* referencja instanceof Klasa
*/

int[] tab ={1,2,3};
assertTrue(tab instanceof Object); //ok
}




  1. Pisane tylko małymi literami
  2. Zasada: po lewej stronie referencja
  3. Zasada: po proawej stronie Klasa Name