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

Moderator: admin

Post Reply
dannysantos1985
Posts: 12
Joined: Tue Nov 24, 2015 4:34 pm
Contact:

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

Post by dannysantos1985 » Wed Nov 25, 2015 1:27 pm

What is a constant expression?
Is it like 20 or 13434 that is assigned to a final variable. Give me examples, please

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

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

Post by admin » Wed Nov 25, 2015 7:00 pm

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

goncaloncpinto
Posts: 5
Joined: Mon Feb 15, 2016 5:43 am
Contact:

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

Post by goncaloncpinto » Mon Feb 15, 2016 6:22 am

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

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

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

Post by admin » Mon Feb 15, 2016 9:24 am

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

gussan
Posts: 3
Joined: Mon May 15, 2017 7:54 am
Contact:

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

Post by gussan » Mon May 15, 2017 8:00 am

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?

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

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

Post by admin » Mon May 15, 2017 11:53 pm

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.

gussan
Posts: 3
Joined: Mon May 15, 2017 7:54 am
Contact:

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

Post by gussan » Tue May 16, 2017 2:13 am

so what is then uninitialized i++ ?

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

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

Post by admin » Tue May 16, 2017 2:37 am

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

gussan
Posts: 3
Joined: Mon May 15, 2017 7:54 am
Contact:

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

Post by gussan » Tue May 16, 2017 3:33 am

no its fine, i misunderstood. its all good now. sorry for the bother

vidyats
Posts: 5
Joined: Wed Jul 12, 2017 11:58 pm
Contact:

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

Post by vidyats » Tue Jul 25, 2017 5:36 am

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 ?

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

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

Post by admin » Tue Jul 25, 2017 7:42 am

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

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests