This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Final variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Final variable" Watch "Final variable" New topic

Final variable

Lata Bagga
Ranch Hand

Joined: Jun 13, 2007
Posts: 35

By running the above code, the compiler gives error in "line 1" saying that "cannot convert from int to byte". It's fine, but when I compile the other way as:

Now it is not giving any error. I can't understand why its not giving any error here. Please help me.

Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2066

Here, the compiler doesnt know what value will be put in the int, a value which can be greater than 127, which cannot be automatically assigned to a byte.

In this one, the compiler knows that 'i' will have 127 always. The compiler allowed the assignment into b because he knows 127 fits into a byte.
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2066
Try use 128 and both codes will give error.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: Final variable
It's not a secret anymore!