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