Page 1 of 1

About Question enthuware.ocajp.i.v8.2.1353 :

Posted: Wed Nov 25, 2015 1:27 pm
by dannysantos1985
What is a constant expression?
Is it like 20 or 13434 that is assigned to a final variable. Give me examples, please

Re: About Question enthuware.ocajp.i.v8.2.1353 :

Posted: Wed Nov 25, 2015 7:00 pm
by admin
Any expression that doesn't change its value at run time is a constant expression. For example,
2+4

final int a = 1; //if a is final
a+2 //then this is constant.

20 //this is obviously constant

Re: About Question enthuware.ocajp.i.v8.2.1353 :

Posted: Mon Feb 15, 2016 6:22 am
by goncaloncpinto
Short version:
Any variable that has an assigned value at compile time is a constant.
a = 5 + 4 <- Constant
a = b + 3 <- If the value of b is not known at compile time, than 'a' is a constant

Re: About Question enthuware.ocajp.i.v8.2.1353 :

Posted: Mon Feb 15, 2016 9:24 am
by admin
goncaloncpinto wrote:Short version:
Any variable that has an assigned value at compile time is a constant.
a = 5 + 4 <- Constant
a = b + 3 <- If the value of b is not known at compile time, than 'a' is a constant
No, that is not correct. In your example, a cannot be considered a constant if it is not defined as final. If it is not final, it is a variable because its value can be changed at run time.

Re: About Question enthuware.ocajp.i.v8.2.1353 :

Posted: Mon May 15, 2017 8:00 am
by gussan
I find it strange that option 4 is the only correct answer as line 3: i++; typically looks like it would not compile when i is not declared with/assigned a value? (int i;) How comes?

Re: About Question enthuware.ocajp.i.v8.2.1353 :

Posted: Mon May 15, 2017 11:53 pm
by admin
It is ok to have a uninitialized variable. The problem occurs only when you try to use an uninitialized variable. So int i; is ok as long as you don't try to use i, for example, in a statement System.out.println(i);

Re: About Question enthuware.ocajp.i.v8.2.1353 :

Posted: Tue May 16, 2017 2:13 am
by gussan
so what is then uninitialized i++ ?

Re: About Question enthuware.ocajp.i.v8.2.1353 :

Posted: Tue May 16, 2017 2:37 am
by admin
Not sure I understand your doubt. In this particular question, i is being initialized at line //2 i =c; before it is being used at line //3 i++;
So there is no compilation issue for i++; in this situation.
-Paul.

Re: About Question enthuware.ocajp.i.v8.2.1353 :

Posted: Tue May 16, 2017 3:33 am
by gussan
no its fine, i misunderstood. its all good now. sorry for the bother

Re: About Question enthuware.ocajp.i.v8.2.1353 :

Posted: Tue Jul 25, 2017 5:36 am
by vidyats
narrow conversion always explicit right?
In this problem, it is mentioned that narrowing conversion does not apply to long or double.
I am not clear about this point.

char c = (char)30L; // This is narrow conversion right ?

Re: About Question enthuware.ocajp.i.v8.2.1353 :

Posted: Tue Jul 25, 2017 7:42 am
by admin
Yes, it is a narrowing primitive conversion but also a casting conversion. The explanation is actually about the implicit narrowing conversions that happen in case of assignment. These conversions are not applicable for float, double and long even if the value is small enough to fit into a target variable.

For example, int i = 20L; This will not compile even though 20 can easily fit into an int.

Paul.