aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Small Doubt 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 "Small Doubt" Watch "Small Doubt" New topic
Author

Small Doubt

Vamsi Krishna Uppalapati
Greenhorn

Joined: Oct 05, 2006
Posts: 10
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.


Thanks and Regards,<br /> Vamsi Krishna U
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

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.


The Best way to predict your future is to create it - Every great individual common man
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

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

Joined: Aug 25, 2006
Posts: 1274
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.


all events occur in real time
Vamsi Krishna Uppalapati
Greenhorn

Joined: Oct 05, 2006
Posts: 10
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

Joined: Oct 05, 2006
Posts: 10
Thanks hassel, it clears my doubt...
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Small Doubt