This week's giveaways are in the MongoDB and Jobs Discussion forums. We're giving away four copies of Mongo DB Applied Patterns and 4 resume reviews from Five Year Itch and have the authors/reps on-line! See this thread and this one for details.
How can method m1() can return char whereas its "contract" is to return byte ? static byte m1() { final char c = 'b'-'a'; return c; // 1 } static byte m2() { final short s = 2; return s; // 2 } static byte m3(final char c) { return c; // 3 } static byte m4(final short s) { return s; // 4 } public static void main(String[] args) { char c = 'd'-'a'; short s = 4; System.out.print(""+m1()+m2()+m3(c)+m4(s)); } }
Deep Chand
Ranch Hand
Joined: Dec 17, 2002
Posts: 133
posted
0
In m1(), the value of 'c' requires only one byte. So compiler allows that and automatically does the casting while returning. Same reason is true for m2(). However, compiler doesn't allow m3() & m4() as the size of the argument will be decided during run time, which can be more than 1 byte. So compiler complains about 'loss of precision'. I have recently started preparing for the certification exam...Please correct me, if I'm wrong here. Thanks, Dg
Valentin Crettaz
Gold Digger
Sheriff
Joined: Aug 26, 2001
Posts: 7610
posted
0
dg, Welcome to Javaranch, a friendly place for Java greenhorns We ain't got many rules 'round these parts, but we do got one. Please change your displayed name to comply with the JavaRanch Naming Policy. Thanks Pardner! Hope to see you 'round the Ranch!
Valentin, Sorry about that pardner...I have corrected it. One more thing...what this membership status 'greenhorns' means. Like can I choose my membership status or ??? cheers Deep
Ambapali Pal
Ranch Hand
Joined: Dec 17, 2002
Posts: 47
posted
0
Hi Salim and Deep, Does this code compile ? It is giving me error for all 4 methods. I thought atleast m2() should compile because s is a compile time constant and its value can be fitted in a byte. I donot know if something is wrong with my compiler. I am using jdk1.2.1
Ambapali
Deep Chand
Ranch Hand
Joined: Dec 17, 2002
Posts: 133
posted
0
It compiles for me for m1() and m2(). One more thing, if you remove 'final' from 'c' & 's' in m1() and m2() respectively, then you will again get the same error. Reason is: if they are declared as final and values are initialized as compile time constant then compiler calculates their values (whether any precision loss) would be there or not. If yes, it gives errors. I bet even if they are declared as final but not initialized then compiler will complain! btw, i'm using java "1.4.1_01" cheers, deep
Valentin Crettaz
Gold Digger
Sheriff
Joined: Aug 26, 2001
Posts: 7610
posted
0
One more thing...what this membership status 'greenhorns' means. Like can I choose my membership status or ??? You will be considered a greenhorn until you reach 30 posts. After that, you'll be implicitely promoted to a ranch hand
Harry Kong
Ranch Hand
Joined: Dec 06, 2002
Posts: 41
posted
0
Yea, that was also the question that confused me for a minute. But when I read the remark on the question everything became clear. I am guessing it is not a common knowledge or obvious that declaring a variable "final" makes it "tight" variable. Now this is just how I think of it. I guess it would be easy to think of "final" as a shrink wrap in this case. [ December 31, 2002: Message edited by: Harry Kong ]