This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assignments Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assignments" Watch "Assignments" New topic


Satya Baswa

Joined: May 06, 2008
Posts: 3
1. final int i=127;
2. byte b=i;

This piece of code works...

1. final int i;
2. i=127;
3. byte b=i;

This piece of code is not working.

What is the difference in the above two pieces of code?
Tarun Kumar

Joined: Apr 30, 2008
Posts: 26
in first case since final object is assigned it's value at the declarion time hence compiler knows its values is <=127 ,thus allowing to compile.
in second case since final varible is not assigned at the declaration time,hence compiler can not be known about the later assigned value.
thus compiler is not sure wheather value is <=127,thus not allowing to compile.

Tarun Kumar
I agree. Here's the link:
subject: Assignments
Similar Threads
Doubt in assignment
Final variable
I cant believe my eyes?
why I cann't assign FINAL INT to Byte