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

Problem related to assignment

Jolly Tiwari
Ranch Hand

Joined: Mar 26, 2006
Posts: 77
Byte bt1=5;
int bt2=7;
bt1=bt2;

On compilation

I am getting ---->

incompatible types
found : int
required: java.lang.Byte



Byte bt1=5;
final int bt2=7;
bt1=bt2;

it is compiling properly
(due to FINAL key word i think....)

PLease clear this thing
balajee annamalai
Ranch Hand

Joined: May 09, 2007
Posts: 31
Yes you are right this is due to final keyword only.
Beacuse if a variable is declared as final it is compile time constant and the value will be known to the compiler that it is within the range.Try the same program with

final int i=1000; then you will be getting Compile time error.
Srikanth Basa
Ranch Hand

Joined: Jun 06, 2005
Posts: 241
In the first case, the compies see that it is non-final. The value can change anytime during execution so it concludes that integer to byte conversion is not safe.

In the second case, as it is final, it sees that the value falls into the range of byte and the value can never change in its lifetime. Hence it compiles properly.
Jolly Tiwari
Ranch Hand

Joined: Mar 26, 2006
Posts: 77
Thanks for the answer...

Now i got it.

Regards

Rajesh
Ram Manoj
Ranch Hand

Joined: Jan 12, 2008
Posts: 52
Considering,

final int i=1000;

Here is 'i' not treated as compile time constant.

If so are are there,

compile time and run time contants.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38859
    
  23
Please see this beginner's thread about the same problem.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38859
    
  23
Originally posted by Ram Manoj:
Considering,

final int i=1000;

Here is 'i' not treated as compile time constant.

If so are are there,

compile time and run time contants.
Of course i is a compile-time constant here. Read the other thread I quoted earlier.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem related to assignment