Win a copy of Penetration Testing Basics this week in the Security forum!

# b*=b1 vs b=b*b1

Jeremy Chan
Greenhorn
Posts: 12
As you can see
Will the following code compile ?
1. byte b=2;
2. byte b1=3;
3. b*=b1;
But
Will the following code compile ?
1. byte b=2;
2. byte b1=3;
3. b=b*b1;
The Answer is No , explicit cast from int to byte needed
Why?
b*=b1; does not equal to b=b*b1 ?

irisly
Greenhorn
Posts: 2
I want to know how to explain it. Who can tell us? thanks!

Ashik Uzzaman
Ranch Hand
Posts: 2373
Hi Chan,
byte b,b1=1; b *= b1;
Here b *= b1 means b = (byte)b*b1; implicit casting is happening here (in ur case-1), which is not happening in the second case.
------------------
azaman
[This message has been edited by Ashik uzzaman (edited August 20, 2001).]

Ragu Sivaraman
Ranch Hand
Posts: 464
Originally posted by Jeremy Chan:
As you can see
Will the following code compile ?
1. byte b=2;
2. byte b1=3;
3. b*=b1;
But
Will the following code compile ?
1. byte b=2;
2. byte b1=3;
3. b=b*b1;
The Answer is No , explicit cast from int to byte needed
Why?
b*=b1; does not equal to b=b*b1 ?

b*=b1;
It is Arithmetic Extended Assignment Operation
Implicit Casting is OK
b=b*b1;
Binary Operation with simple assignment
Implicit casting not applicable