Auto Type conversions during Bitwise AND

K

karthik

Hi,

Code snippet:

1. byte a=1,b=2;
2. byte c = a & b;

Line No.2 is a compile time error. It needs a type casting to byte.

This will work fine: byte c = (int) (a&b)

However this is not a compile time error, working perfectly fine: byte
c = 1 & 2;

Same is applicale for Modulo operator too.

Simple inference from this is, auto type conversion (to integer) for
such operations [bit AND, Modulo] takes place for vales stored
variables. But not for direct literal values.

Anybody knows about this, the actual reason??
 
K

karthik

ONE CORRECTION:: Result should be type casted to byte. Wrongly casted to int in previous message.

Code snippet:

1. byte a=1,b=2;
2. byte c = a & b;

Line No.2 is a compile time error. It needs a type casting to byte.

This will work fine: byte c = (byte) (a&b)

However this is not a compile time error, working perfectly fine: byte
c = 1 & 2;

Same is applicale for Modulo operator too.

Simple inference from this is, auto type conversion (to integer) for
such operations [bit AND, Modulo] takes place for vales stored
variables. But not for direct literal values.

Anybody knows about this, the actual reason??
 
K

karthik

karthik said:
ONE CORRECTION:: Result should be type casted to byte. Wrongly casted to int in previous message.
Code snippet:
1. byte a=1,b=2;
2. byte c = a & b;
Line No.2 is a compile time error. It needs a type casting to byte.
This will work fine: byte c = (byte) (a&b)
However this is not a compile time error, working perfectly fine: byte
c = 1 & 2;
Same is applicale for Modulo operator too.
Simple inference from this is, auto type conversion (to integer) for
such operations [bit AND, Modulo] takes place for vales stored
variables. But not for direct literal values.
Anybody knows about this, the actual reason??

Seehttp://java.sun.com/docs/books/jls/third_edition/html/conversions.htm....

There is a special rule for using a constant expression as the right
hand side in an assignment or initialization:

"A narrowing primitive conversion may be used if the type of the
variable is byte, short, or char, and the value of the constant
expression is representable in the type of the variable."

1 & 2 is a constant expression of type int whose value is representable
as a byte, so the rule applies and the compiler handles the conversion
from int to byte. On the other hand, a & b is a non-constant int
expression, so it needs a cast.

Patricia- Hide quoted text -

- Show quoted text -

So java compiler is working in a way that we assumed. :)

Thnx for the clarification..
 
L

Lew

So java compiler is working in a way that we assumed. :)

No need to assume when you can be certain by reading the section to
which Patricia alluded.

The JLS is the ultimate authority for questions of Java language
semantics. By linking you to it, Patricia gave you the tool to find
these answers in a way even more reliable than asking on Usenet.
 
R

Roedy Green

The JLS is the ultimate authority for questions of Java language
semantics. By linking you to it, Patricia gave you the tool to find
these answers in a way even more reliable than asking on Usenet.

It is certainly a good idea to read the JLS, however, it is probably
wise for a newbie to verify their interpretation of the wording by
asking other more experienced programmers and by doing some
experiments.

Interpreting it is a bit like constitutional law. Such documents are
written in a peculiar way. To the initiated, there is no problem, but
to the novice, it reads like Greek. The intended audience is compiler
writers, not people learning Java.

Perhaps some day someone like Lew could translate the JLS into less
formal language to make all the fine points accessible to even the
newbie. It would primarily use examples to illustrate the fine
points.

The Annotated JLS. The O'Reilly book would have a picture of a
Phorusrhacidae with little labels pointing to the various bones.
 
K

karthik

karthik wrote:
ONE CORRECTION:: Result should be type casted to byte. Wrongly casted to int in previous message.
Code snippet:
1. byte a=1,b=2;
2. byte c = a&  b;
Line No.2 is a compile time error. It needs a type casting to byte.
This will work fine: byte c = (byte) (a&b)
However this is not a compile time error, working perfectly fine: byte
c = 1&  2;
Same is applicale for Modulo operator too.
Simple inference from this is, auto type conversion (to integer) for
such operations [bit AND, Modulo] takes place for vales stored
variables. But not for direct literal values.
Anybody knows about this, the actual reason??
Seehttp://java.sun.com/docs/books/jls/third_edition/html/conversions.htm...
There is a special rule for using a constant expression as the right
hand side in an assignment or initialization:
"A narrowing primitive conversion may be used if the type of the
variable is byte, short, or char, and the value of the constant
expression is representable in the type of the variable."
1&  2 is a constant expression of type int whose value is representable
as a byte, so the rule applies and the compiler handles the conversion
from int to byte. On the other hand, a&  b is a non-constant int
expression, so it needs a cast.
Patricia- Hide quoted text -
- Show quoted text -
So java compiler is working in a way that we assumed. :)

No, rather differently from the way you described.

1, 2, and (1 & 2) are all int expressions, so no auto type conversions
are needed. However, even if you had written ((byte)1 & (byte)2) the
byte values would have been converted to int and the result would be an int.

The rule I quoted allows initialization of a byte variable with an
int expression if, and only if, the int expression is a constant and its
value is in the byte range.

You use this rule, probably without noticing it, when you write
something like:

byte a = 1;

rather than:

byte a = (byte)1;

1 is an int expression, but can be used to initialize a byte without an
explicit cast because it is a constant expression and its value is in
the byte range.

Patricia- Hide quoted text -

- Show quoted text -

From the rule what you have given,

What i tried to mean was Auto conversion(Narrow down) occurs
automatically

when the compiler evaluates a constant expressions like 1&2 [obviously
if the result can be narrowed down - lies within the range]

but not in case if that has been carried out by storing the constants
into variables and operating on them.
 
K

karthik

No need to assume when you can be certain by reading the section to
which Patricia alluded.

The JLS is the ultimate authority for questions of Java language
semantics.  By linking you to it, Patricia gave you the tool to find
these answers in a way even more reliable than asking on Usenet.

Fortunately Or Unfortunately, i had assumed that before patricia has
provided that explanation Or it happened before even i post the
message. : - )
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,375
Latest member
FelishaCma

Latest Threads

Top