About Question enthuware.ocajp.i.v8.2.1353 :
Moderator: admin
-
- Posts: 12
- Joined: Tue Nov 24, 2015 4:34 pm
- Contact:
About Question enthuware.ocajp.i.v8.2.1353 :
What is a constant expression?
Is it like 20 or 13434 that is assigned to a final variable. Give me examples, please
Is it like 20 or 13434 that is assigned to a final variable. Give me examples, please
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1353 :
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
2+4
final int a = 1; //if a is final
a+2 //then this is constant.
20 //this is obviously constant
If you like our products and services, please help us by posting your review here.
-
- Posts: 5
- Joined: Mon Feb 15, 2016 5:43 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1353 :
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
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
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1353 :
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.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
If you like our products and services, please help us by posting your review here.
-
- Posts: 3
- Joined: Mon May 15, 2017 7:54 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1353 :
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?
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1353 :
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);
If you like our products and services, please help us by posting your review here.
-
- Posts: 3
- Joined: Mon May 15, 2017 7:54 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1353 :
so what is then uninitialized i++ ?
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1353 :
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.
So there is no compilation issue for i++; in this situation.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 3
- Joined: Mon May 15, 2017 7:54 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1353 :
no its fine, i misunderstood. its all good now. sorry for the bother
-
- Posts: 5
- Joined: Wed Jul 12, 2017 11:58 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1353 :
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 ?
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 ?
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1353 :
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.
For example, int i = 20L; This will not compile even though 20 can easily fit into an int.
Paul.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 39 guests