Page 1 of 1
About Question enthuware.ocajp.i.v7.2.1268 :
Posted: Thu Dec 06, 2012 7:53 am
by ETS User
In the answer it says that option 3 is wrong because 'i' is not initialized, however aren't all primitives initialized to 0?
Re: About Question enthuware.ocajp.i.v7.2.1268 :
Posted: Thu Dec 06, 2012 12:53 pm
by admin
No, local variables (i.e. variables in a method) are not initialized automatically. Only instance and static variables are initialized automatically.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1268 :
Posted: Mon Mar 18, 2013 10:26 am
by Ambiorix
admin wrote:No, local variables (i.e. variables in a method) are not initialized automatically. Only instance and static variables are initialized automatically.
HTH,
Paul.
How do we know that this is in a method given that we can only see 2 lines of code?
Re: About Question enthuware.ocajp.i.v7.2.1268 :
Posted: Mon Mar 18, 2013 11:22 am
by admin
Ambiorix wrote:admin wrote:No, local variables (i.e. variables in a method) are not initialized automatically. Only instance and static variables are initialized automatically.
HTH,
Paul.
How do we know that this is in a method given that we can only see 2 lines of code?
No matter where you put these 2 lines, they are invalid.
Re: About Question enthuware.ocajp.i.v7.2.1268 :
Posted: Sat Nov 17, 2018 5:36 am
by flex567
From the explenation:
No 2.
uses 'j +=5'. Now, this statement is preceded by 'int i=0,' and that means we are trying to declare variable j.
1) What does this sentence mean? Everyhing after
is decleration?
2) j = j + 5: This to me is not a declaration statement but initialization.(or just assignment)
Re: About Question enthuware.ocajp.i.v7.2.1268 :
Posted: Sat Nov 17, 2018 8:03 am
by admin
It is talking about the initialization section of the if statement, which contains: int i=0, j+=5;
Since this statement starts with the type specification i.e. int, it is a declaration statement. This declaration will apply to i as well as j. i is ok, but j has already been declared before the if statement, that is why complier will complain.
Re: About Question enthuware.ocajp.i.v7.2.1268 :
Posted: Sat Nov 17, 2018 11:21 am
by flex567
Aha I understand now, thanx
Re: About Question enthuware.ocajp.i.v7.2.1268 :
Posted: Mon Jan 25, 2021 3:55 pm
by unranked
5 statement little confusing me. On the other statements there's declarations for variables. But on 5's, there's no declaration for j.
int j, var j = 0;
Re: About Question enthuware.ocajp.i.v7.2.1268 :
Posted: Mon Jan 25, 2021 11:16 pm
by admin
I see that option 5 has int i = 0, j = 0; and not int j, var j = 0;
int i = 0, j = 0; is valid because the Java language allows it. It is called "compound declaration". i and j are considered to be of the same type i.e. int.
int i, var j = 0; is invalid because var declaration is not allowed in a compound declaration.
Re: About Question enthuware.ocajp.i.v7.2.1268 :
Posted: Tue Jan 26, 2021 5:49 am
by unranked
Yeah, sorry, sorry)
I didn't see that: int i = 0, j = 0;
thanks.