Friday, March 6, 2009

arithmetic operators byte short datatypes



package funds;

public class DiffOpera {

public static void main(String ar[])
{
/* If we apply arithmetic operation between two operands a and b
the result is always max(int,type of a,type of b) */
// using arithmetic operation
byte b=10;
b=b+1; // error because max(int,short,int)

short s=40;
s=s+1; // error because max(int,short,int)

// The above error in case of byte and short only we will get

int i=20;
i=i+1; // no error because max(int,type of a,type of b)


// using increment decrement
byte b1=++b; // no error because It perform like this (type b1)b1+1;
short s1=++s; // no error
int i1=++i1; // no error

}
}

4 comments:

  1. Nice code. I need many programs like this.

    ReplyDelete
  2. Good explanation with simple examples..

    ReplyDelete
  3. ChandraSekhar SannamuriDecember 20, 2013 at 2:15 AM

    The example is very clear.. Thank you so much for providing these kind of example.

    Thanks brother.. :)

    ReplyDelete