This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Junior/Associate Certification (SCJA,OCAJ 5/6/8) and the fly likes Compiler error ! Not sure why Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Certification » Junior/Associate Certification (SCJA,OCAJ 5/6/8)
Bookmark "Compiler error ! Not sure why" Watch "Compiler error ! Not sure why" New topic

Compiler error ! Not sure why

P Teng

Joined: Nov 20, 2009
Posts: 16

why does line 1 not give the same error as line 2 (possible loss of precision). Why is the 2 in line 1 not treated as an integer.
Sreedhar Sivan

Joined: Mar 07, 2011
Posts: 1
When you apply mathematical operations on byte, they will always result in int. To protect from overflow it will not allow you to store an int into a byte.

That is the reason you are getting the error as "possible loss pf precision".

Change the code like this and it will work,

Class ByteTest {

public static void main(String[] args) {

int b = 100;

Only change is byte to int.

Good that you posted in Javaranch. All the best...

Sreedhar S
Java developer

P Teng

Joined: Nov 20, 2009
Posts: 16

Thank you very much for your reply.
but why does this not apply to b *= 1 as that too is a mathematical action.

Matthew Brown

Joined: Apr 06, 2010
Posts: 4541

The operator *= (and similarly +=, -= etc) have an implicit cast built in, which is why it works. So:
is equivalent to
P Teng

Joined: Nov 20, 2009
Posts: 16
That explains it.
Perhaps thats why it does not like b = b * 2 but likes the former b *= 2

Thanks Matthew.
Mala Gupta
Ranch Hand

Joined: Sep 27, 2002
Posts: 251
P Teng,

Apart from the shortcut assignment operators (example +=, -=, *=) and unary operators (+, -, ++, --), the following rules are applied to a binary expression:

1) If either operand is of type double, the other is converted to double.
2) Otherwise, if either operand is of type float, the other is converted to float.
3) Otherwise, if either operand is of type long, the other is converted to long.
4) Otherwise, both operands are converted to type int.

As visible from the above list, the operands of type char, byte and short are atleast promoted to an int, when an expression is evaluated.

In the following example, the type of the resultant expression is double, because one of the operands is of type double.


Author of Manning's OCA Java SE 7 Programmer I Certification Guide and OCP Java SE 7 Programmer II Certification Guide
Vijitha Kumara

Joined: Mar 24, 2008
Posts: 3899

Welcome to CodeRanch, Sreedhar Sivan

And, please UseCodeTags while posting code which makes it much easier to read.

[How to ask questions] [Twitter]
P Teng

Joined: Nov 20, 2009
Posts: 16
Thank you all.
Have you checked out Aspose?
subject: Compiler error ! Not sure why
jQuery in Action, 3rd edition