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