wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in assignment 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 "Doubt in assignment" Watch "Doubt in assignment" New topic
Author

Doubt in assignment

saran bala
Greenhorn

Joined: Oct 17, 2007
Posts: 8
public class Test9{
public static void main(String a[]){
final byte b = 1;
char c = 2;
short s = 3;
c = b; // 1
}
}
In this code,if byte is declared as final,while assigning byte to char,there is no problem in compilation.But if byte is not final,then compilation fails at lime marked 1.Why is it so?
Doug Slattery
Ranch Hand

Joined: Sep 15, 2007
Posts: 294
Hi Saran,

Originally posted by saran bala:

In this code,if byte is declared as final,while assigning byte to char,there is no problem in compilation.But if byte is not final,then compilation fails at lime marked 1.Why is it so?


By declaring a variable final means it gets special treatment at compile time. To add to the confusion, it also gets treated differently as a local variable as opposed to an instance variable.

Try fiddling around with the following snippet to see how it works:



Aloha,
Doug

-- Nothing is impossible if I'mPossible
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in assignment
 
Similar Threads
doubt in primitive assignment...
Doubt in implict conversion
Dan's Mock Exam
doubt in method argument declared final
Assignments