Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem related to assignment

 
Jolly Tiwari
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the answer...

Now i got it.

Regards

Rajesh
 
Ram Manoj
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please see this beginner's thread about the same problem.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic