Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login

# b*=b1 vs b=b*b1

Jeremy Chan
Greenhorn

Joined: Mar 28, 2001
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

Joined: Jul 26, 2001
Posts: 2
I want to know how to explain it. Who can tell us? thanks!

Helping you means helping me
Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
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).]

Ashik Uzzaman
Senior Software Engineer, TubeMogul, Emeryville, CA, USA.
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
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

I agree. Here's the link: http://aspose.com/file-tools

subject: b*=b1 vs b=b*b1