Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Small Doubt

 
Vamsi Krishna Uppalapati
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
byte a = 3; //1
byte b = 4; //2
byte c = a + b; //3

Line 3 will give compilation error Because the compiler knows that the result may come as int.

My Question here is.
if i give literals instead of variable in the expressions like
byte c1 = 3 + 4; // No compilation error
byte c2 = 300 + 4; // compilation error
byte c3 = 126 + 1; // No Compilation error
byte c4 = 126 + 2; // Compilation error

Why it is like this? Does the compiler executes the expression?
I think execution of expression will happen at run time.

I am very new to java. Please clarify my doubt.
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vamsi Krishna Uppalapati:
byte a = 3; //1
byte b = 4; //2
byte c = a + b; //3

Line 3 will give compilation error Because the compiler knows that the result may come as int.

My Question here is.
if i give literals instead of variable in the expressions like
byte c1 = 3 + 4; // No compilation error
byte c2 = 300 + 4; // compilation error
byte c3 = 126 + 1; // No Compilation error
byte c4 = 126 + 2; // Compilation error

Why it is like this? Does the compiler executes the expression?
I think execution of expression will happen at run time.

I am very new to java. Please clarify my doubt.


What you will say now....

You can easily understand the difference now.....

Code speaks itself the difference..



See there is a lot of difference in a normal variable and a final Constant.

This may helps you a lot.
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vamsi Krishna Uppalapati:

Why it is like this? Does the compiler executes the expression?
I think execution of expression will happen at run time.



Ofcourse this is runtime processing, but atleast compiler wants to ensure itself not to run in short of conversions.

Please correct me If I am wrong.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vamsi Krishna Uppalapati (whose Member # 135497 is a prime) wrote:
Why it is like this? Does the compiler executes the expression?
I think execution of expression will happen at run time.



Yes, these integer literal calculations will be calculated at compile time.
Imagine integer (or double, float ...) literals as constants, they never change.
I mean, the literals never change, not the variables that hold them.
So the compiler checks, what comes out, and if it fits into the byte.

Also this will compile:
final int i=126, j=1;
byte b=i+j;

But only with the final modifier.



Yours,
Bu.
 
Vamsi Krishna Uppalapati
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My question is why it is giving compilation error when we declare like
byte b = 100 + 200;
ant not giving compilation error when we declare like
byte b = 100 + 1;

Does the complier calculates the value on RHS?
 
Vamsi Krishna Uppalapati
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks hassel, it clears my doubt...
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vamsi Krishna Uppalapati:
My question is why it is giving compilation error when we declare like
byte b = 100 + 200;
ant not giving compilation error when we declare like
byte b = 100 + 1;

Does the complier calculates the value on RHS?


100, 1, and 200 are all compile-time constants, so the values of 100+200, and 100+1 can be computed at compile-time.

100+200 is an int that is too large to fit into a byte.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic