• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Small Doubt

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks hassel, it clears my doubt...
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic