File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes strange things happen???Check Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "strange things happen???Check" Watch "strange things happen???Check" New topic
Author

strange things happen???Check

Janet Yap
Greenhorn

Joined: Mar 13, 2004
Posts: 24

The above code is compile and run finely and print b=10;
Why casting is not required here???

case 2::: But if i tried this one it will not work !!!

But this code will give compiler error - Casting is required .Why this things is happen???

[I added UBB CODE tags to your source code to make it more readable. - Ajith]
[This message has been edited by Ajith Kallambella (edited August 29, 2000).]
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5781
In the first case, since i is declared as a final variable, it is treated as a compile time constant ( the number 10 ) instead of an integer variable. Since the number 10 can be fit into a byte variable without losing precision the compiler takes it happily. If you comment the final qualifier and try to compile the code, you will get a compiler error because then, 'i' is treated as an integer variable and the compiler is concerned about loss of value when it is assigned to a byte.

The second case needs a little study. The rules of assignment conversion change when you cross the integer domain and step into the world of longs, floats and doubles.
Section 5.2 Assignment Conversion of JLS says

Assignment conversion occurs when the value of an expression is assigned (�15.26) to a variable: the type of the expression must be converted to the type of the variable. Assignment contexts allow the use of an identity conversion (�5.1.1), a widening primitive conversion (�5.1.2), or a widening reference conversion (�5.1.4). In addition, a narrowing primitive conversion may be used if all of the following conditions are satisfied:

  • The expression is a constant expression of type byte, short, char or int.
  • The type of the variable is byte, short, or char.
  • The value of the expression (which is known at compile time, because it is a constant expression) is representable in the type of the variable.


Clearly the expression int j = i; is a constant expression of type long and hence the compiler cannot apply 'automatic' narrowing conversion here. Eventhough i is declared as a 'final' variable, since it belongs to the 'rank of the long', the compiler becomes picky about narrowing it down to integer and asks for an explicit cast.
Hope I have made things clear.
Ajith
[This message has been edited by Ajith Kallambella (edited August 29, 2000).]


Open Group Certified Master IT Architect.
Sun Certified Architect(SCEA).
MahaAdd
Greenhorn

Joined: Aug 28, 2000
Posts: 28
Hi Ajith,
can u be a little more clear, please..
as such i am unable to comprehend the JLS..
thanks
 
 
subject: strange things happen???Check
 
Threads others viewed
Operators and Assignment
Using Final Keyword & Casting
arithmetic promotion
implicit casting of final int to byte ???
explain why the following will compile
IntelliJ Java IDE