Page 1 of 1

compile time constant

Posted: Fri Aug 13, 2021 1:00 pm
by nick12345
Hallo,

I have a question about an example from the book.

Code: Select all

 byte b = 200 - 100;
The result is 100 so the value fits into a byte.

Due to numeric promotion we should have two ints: 200 and 100.

A number without a decimal is in java considered an int literal.

It's due to compile time constat but I don't understand why it works.
The result of 200-100 must be executed, right?

So why

Code: Select all

 byte b = 200 - 100;
is a compile time constant
and

Code: Select all

 int i = 5;
is not a compile time constant?

Re: compile time constant

Posted: Fri Aug 13, 2021 8:22 pm
by admin
Both, 200 - 100 and 5 are compile time constants.

200 -100 is not executed at runtime because 200 as as well as 100 are compile time constants. Therefore, the compiler performs the calculation of 200 - 100 and finds out the result 100 to be within the range of a byte and so, determines the assignment to be ok.

Re: compile time constant

Posted: Sun Aug 15, 2021 3:13 am
by nick12345
Thank you for your reply :)

So consider this examples:

example 1:
chapter 5.3.3 (Uninitialized variables and default values):


Code: Select all

public static void main (String [] args) {

int val;
int i = 0;

if(i ==0) {
val = 10;
}

System.out.println(val);
}
Variable i is NOT a compile time constant.

example 2:

Code: Select all

int i2 = 5;
Why is int i = 0; NOT a compile time constant and in second example is the variable i2 a compile time constant?

Re: compile time constant

Posted: Sun Aug 15, 2021 4:48 am
by admin
You are getting confused between two different things - a variable and a value. i is a variable and 0 (and 200, 100 etc.) is a value (more precisely, a "literal"). Values are always compile time constants. Variables can be compile time constants if they are declared final.

In both the cases i.e. in :
int i = 0;
int i2 = 5;

Neither i nor i2 is a compile time constant. But 0 and 5 are compile time constants.

Another thing that you seem to be confused about is assigning a value to a variable in this statement: byte b = 200 - 100;

The value 200-100 is 100 and is of type int but 100 is a compile time constant and it can fit into a byte, that is why the assignment is ok. The variable b itself is not a compile time constant though.

Therefore, if you do this:
b = i; <-This will not compile because i is an int and is not a compile time constant while b is a byte (which is smaller than an int)

Compiler cannot know in advance what will be the value of i at runtime and so it cannot allow this statement without a cast.
Why is int i = 0; NOT a compile time constant and in second example is the variable i2 a compile time constant?
Now, you can see why your comparison does not make sense. Saying whether int i = 0; is a compile time constant or not does not make any sense. int i = 0; is a statement.

Re: compile time constant

Posted: Sun Aug 15, 2021 4:55 am
by admin
You may want to go through the section 5.3 really carefully once again. Also, read Section 4.2.1 Keywords, Literals, Reserved words, and Identifiers and 5.1 Data types in Java again as that might help you understand section 5.3 better.

Re: compile time constant

Posted: Sun Aug 15, 2021 12:02 pm
by nick12345
Thank you very much for your explanation :)
It's much clearer now :)

Re: compile time constant

Posted: Wed Sep 15, 2021 10:36 am
by hakim homsy
beautiful