Monday, March 15, 2010

Casting for Primitives

Casting

int

float

float f = 6.1;

int i = (int)f;

double

double d = 25.0;

int i = (int)d;

long

long l = 25.0;

int i = (int)l;

String

String str = "25"; int i = Integer.valueOf(str).intValue();    or  int i = Integer.parseInt(str);



Casting

float

int

int i = 6;

float f= (float)i;

double

double d = 25.0;

float f = (float)d;

long

long l = 25.0;

float f = (float)l;

String

float f = Float.valueOf(str).floatValue();

Casting

double

int

int i = 6;

double d =(double)i;

float

float f = 6.1;

double d =(double)f;

long

long l = 25.0;

double d = (double)l;

String

double d = Double.valueOf(str).doubleValue();



Casting

long

int

int i = 6;

long l = (long)i;

float

float f = 6.1;

long l = (long)f;

double

double d =25.0;

long l= (long)d;

String

long l = Long.valueOf(str).longValue(); or   Long l = Long.parseLong(str);

Casting

String

int

int i = 42; String str = Integer.toString(i);  or  String str = "" + i;

float

String str = Float.toString(f);

double

String str = Double.toString(i);

long

String str = Long.toString(l);

No comments:

Post a Comment