Prev: Supply AFF CAPS and hat ( www.nike-black.com )
Next: Web Services in Java- Quick Start Tutorial
From: Patricia Shanahan on 9 Jul 2010 12:08 On 7/8/2010 11:16 PM, karthik wrote: > 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. :-) 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
From: Roedy Green on 10 Jul 2010 09:36 On Fri, 9 Jul 2010 07:14:28 -0700 (PDT), Lew <lew(a)lewscanon.com> wrote, quoted or indirectly quoted someone who said : > >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. -- Roedy Green Canadian Mind Products http://mindprod.com You encapsulate not just to save typing, but more importantly, to make it easy and safe to change the code later, since you then need change the logic in only one place. Without it, you might fail to change the logic in all the places it occurs.
From: karthik on 15 Jul 2010 04:58 On Jul 9, 9:08 pm, Patricia Shanahan <p...(a)acm.org> wrote: > On 7/8/2010 11:16 PM, karthik wrote: > > > > > > > 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. :-) > > 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.
From: karthik on 15 Jul 2010 05:00 On Jul 9, 7:14 pm, Lew <l...(a)lewscanon.com> wrote: > Patricia Shanahan wrote: > >> See <http://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. > > 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- Hide quoted text - > > - Show quoted text - Fortunately Or Unfortunately, i had assumed that before patricia has provided that explanation Or it happened before even i post the message. : - )
First
|
Prev
|
Pages: 1 2 Prev: Supply AFF CAPS and hat ( www.nike-black.com ) Next: Web Services in Java- Quick Start Tutorial |