File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Conversion" Watch "Conversion" New topic
Author

Conversion

Venkatesh Gowri
Greenhorn

Joined: Aug 15, 2002
Posts: 12
Hey, i have a doubt regarding implicit conversion
in this example.
byte b=2,b1=3;
b=b*b1;
Why does this result in a compliation error.
If both the operands are bytes why is it necessary to convert it to an int to carry out the * operation.
Thanks in advance


Gowri Venkatesh
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by Venkatesh Gowri:
Hey, i have a doubt regarding implicit conversion
in this example.
byte b=2,b1=3;
b=b*b1;
Why does this result in a compliation error.
If both the operands are bytes why is it necessary to convert it to an int to carry out the * operation.
Thanks in advance

Try the following
byte b=2,b1=3;
b*=b1;
The compound assignment operator includes an implicit cast to the type of the left operand. However, the simple assignment operator that was used in your example has no such implicit cast.
If both operands of the multiplication operator can be converted to type int, then the result of the expression is of type int. If you want to assign the result to a variable of type byte using the simple assignment operator, then it must be cast to type byte.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
The Java Language Specification, Section 5.6.2, Binary Numeric Promotionshould answer your questions.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Conversion