• Post Reply Bookmark Topic Watch Topic
  • New Topic

Variables[code]  RSS feed

 
Kamani Ellegama
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


what is the reason for;
4th line's compilation error & 6th line compile perfectly???
 
swaraj gupta
Ranch Hand
Posts: 186
C++ Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi kamani,

"In java constant and final expressions are evaluated at compile time, however variable expressions are evaluated at run time".

In your code b2 is final i.e. compiler knows that its value is with in the range of char(0-65535) and that why line 6 wont cause any
problem.

Now, lets talk about
Its a variable expression(line 02.), and will be evaluated at run time, thats why compiler is not aware of this fact that b1 is also with in the
range of char. However it(compiler) just know one thing that b1 is byte, and that why gives compile time error as byte consist of
negative range also(-1 to -128) that can not be assigned to chars...

I hope you get my point...
 
Kamani Ellegama
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot.
 
Kamani Ellegama
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


what is the reason for getting result as "1.0 1.5 1.5"
d1 also a double and it assign to 3/2
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The division is still an integer division, without decimal part. 3/2 is always 1. Assigning that 1 to a double doesn't turn it into 1.5.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
swaraj gupta wrote: . . . "In java constant and final expressions are evaluated at compile time, however variable expressions are evaluated at run time".
Please always say where such information comes from, to avoid copyright problems. And try to avoid red print, which some people find difficult to read.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!