Forums Register Login

Doubt on byte literals

+Pie Number of slices to send: Send
Doing some code snippets during my preparation and i encountered this. The below code throws a compiler error

but if i put the modifier final to both s and s1 it is compiling.

Am i missing something here? why is the compiler not throwing an error?
+Pie Number of slices to send: Send
Please check this one.
+Pie Number of slices to send: Send
Hello,

It is because i believe this conversion is taking place at compile time rather then at run-time as variables are declared final.
As they are within the range of the byte, that is why it is not giving any compilation error. Lets say



It will start giving compile time error as it has gone out of range. But above conversion i suppose is happening at compile
time rather then at run-time. I would love if someone else will comment.

Am i right or not.

Best Regards,
+Pie Number of slices to send: Send
In the second case, both s and s1 are compile time constants. This means that the compiler knows their value at compile time. So when you say s+s1, the compiler is able to find the result of that expression i.e. the compiler knows that s+s1 will be 20, so it replaces the expression by its value. So your code after compilation becomes
+Pie Number of slices to send: Send
I got it now. Thank you Devaka, Prithvi, Ankit for your replies.
+Pie Number of slices to send: Send
You are welcome Bhanu.

Cheers,
No thanks. We have all the government we need. This tiny ad would like you to leave now:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 870 times.
Similar Threads
Why doesn't the subclass variable hide it?
confusion on casting
polymorphism question
Compile time constant
return type
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 05:20:01.