Prev: Supply AFF CAPS and hat ( www.nike-black.com )
Next: Web Services in Java- Quick Start Tutorial
From: karthik on 9 Jul 2010 01:06 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??
From: karthik on 9 Jul 2010 01:08 On Jul 9, 10:06 am, karthik <karthikvasur...(a)gmail.com> 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??
From: Patricia Shanahan on 9 Jul 2010 01:53 karthik wrote: > On Jul 9, 10:06 am, karthik <karthikvasur...(a)gmail.com> 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?? > See http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.2 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
From: karthik on 9 Jul 2010 02:16 On Jul 9, 10:53 am, Patricia Shanahan <p...(a)acm.org> wrote: > karthik wrote: > > On Jul 9, 10:06 am, karthik <karthikvasur...(a)gmail.com> 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. :-) Thnx for the clarification..
From: Lew on 9 Jul 2010 10:14 Patricia Shanahan wrote: >> See <http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.2> >> >> 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. > karthik wrote: > 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. -- Lew
|
Next
|
Last
Pages: 1 2 Prev: Supply AFF CAPS and hat ( www.nike-black.com ) Next: Web Services in Java- Quick Start Tutorial |