compile time constant

Oracle Certified Foundations Associate Java Certification Questions and Discussion
1Z0-811

Moderator: admin

Post Reply
nick12345
Posts: 17
Joined: Fri Aug 13, 2021 12:43 pm
Contact:

compile time constant

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: compile time constant

Post 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.
If you like our products and services, please help us by posting your review here.

nick12345
Posts: 17
Joined: Fri Aug 13, 2021 12:43 pm
Contact:

Re: compile time constant

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: compile time constant

Post 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.
If you like our products and services, please help us by posting your review here.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: compile time constant

Post 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.
If you like our products and services, please help us by posting your review here.

nick12345
Posts: 17
Joined: Fri Aug 13, 2021 12:43 pm
Contact:

Re: compile time constant

Post by nick12345 »

Thank you very much for your explanation :)
It's much clearer now :)

hakim homsy
Posts: 2
Joined: Wed Sep 15, 2021 10:20 am
Contact:

Re: compile time constant

Post by hakim homsy »

beautiful

Post Reply

Who is online

Users browsing this forum: No registered users and 44 guests