aspose file tools*
The moose likes Associate Certification (SCJA,OCAJ 5/6) 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 » Associate Certification (SCJA,OCAJ 5/6)
Bookmark "Compiler error ! Not sure why" Watch "Compiler error ! Not sure why" New topic
Author

Compiler error ! Not sure why

P Teng
Greenhorn

Joined: Nov 20, 2009
Posts: 16
Hi


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
Greenhorn

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
Greenhorn

Joined: Nov 20, 2009
Posts: 16
Hello,

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
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

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

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

Thanks Matthew.
Mala Gupta
Author
Ranch Hand

Joined: Sep 27, 2002
Posts: 251
    
    9
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.



cheers
Mala


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

Joined: Mar 24, 2008
Posts: 3838

Welcome to CodeRanch, Sreedhar Sivan

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


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
P Teng
Greenhorn

Joined: Nov 20, 2009
Posts: 16
Thank you all.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Compiler error ! Not sure why