Friday, February 20, 2009

Addition of Two byte-int-char-long-float-double data Type example



package funds;

public class ByteArith {
public static void main(String[] arg)
{
//byte arithmetic operation
byte a=10;
byte b=20;
System.out.println(a+b);
byte r1=a+b; // error (cannot convert int to byte)
final byte c=30;
final byte d=80;
byte r2=c+d; //no error c and d are compile time constants.

/* If we apply arithmetic operation between two operands a and b
the result is always max(int,type of a,type of b) */

// see this how it works
long l=30;
long l2=l+a;

float f=10.5f;
char c1='g';
float f1=f+c1;
float f2=f+f1;

double d1=23.5;
double d2=d1+b;
double d3=d1+c;
double d4=d1+d2;

}
}


0 comments:

Post a Comment