• 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
Pokazywanie postów oznaczonych etykietą promocja typów. Pokaż wszystkie posty
Pokazywanie postów oznaczonych etykietą promocja typów. Pokaż wszystkie posty
6 mar 2010
Promocja typów - rozszerzająca
19 lut 2010
Przeciążanie metod
@Test
public void testPrzeciazanieMetod(){
f(5); //int
f(3/2); //int
short s=12;
f(s); //short
f(s-1); //int
f(1-1.0f); //float
f(s-1.0f); //float
byte b=10;
f(b/2); //int
f(b*b); //int
}
public void f(float x){ prn("float");}
public void f(byte x){ prn("byte"); }
public void f(short x){prn("short");}
public void f(int x){prn("int");}
18 lut 2010
Cast
@Test
public void testCastowania(){
float _45f=45.0f;
char $='\u0000';
//!$=$*$; //promowane do int!!
$=(char)($*$); //poprawnie
//! $=-$; także int
//_45f=_45f/3;
//assertTrue(_45f==15); //pass
//porównanie inta z floatem
int _45i=45;
assertTrue(_45f==_45i);//pass
assertTrue(_45i==_45f);//pass
//inne promowania
long _45l=_45i; //autmoatyczne promowanie
// możliwa utrata informacji ->
//błąd kompilacji
//!_45i=_45l;
_45i=(int)_45l; //poprawnie!
float a12=(float)1/3;
prn(a12); //0.33333
int jestemIntem=(int)a12;
prn(jestemIntem); //0
//a12=1.1; //daje daoubla
a12=1.6f; //ok
jestemIntem=(int)a12;
prn(jestemIntem); //1 (zaokragla w dół)
}
Subskrybuj:
Posty (Atom)