About Question enthuware.ocajp.i.v7.2.1268 :
Moderator: admin
About Question enthuware.ocajp.i.v7.2.1268 :
In the answer it says that option 3 is wrong because 'i' is not initialized, however aren't all primitives initialized to 0?
-
- Site Admin
- Posts: 10386
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1268 :
No, local variables (i.e. variables in a method) are not initialized automatically. Only instance and static variables are initialized automatically.
HTH,
Paul.
HTH,
Paul.
-
- Posts: 25
- Joined: Thu Jan 10, 2013 8:45 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1268 :
How do we know that this is in a method given that we can only see 2 lines of code?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.
-
- Site Admin
- Posts: 10386
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1268 :
No matter where you put these 2 lines, they are invalid.Ambiorix wrote:How do we know that this is in a method given that we can only see 2 lines of code?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.
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1268 :
From the explenation:
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)
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
Code: Select all
int i=0
2) j = j + 5: This to me is not a declaration statement but initialization.(or just assignment)
-
- Site Admin
- Posts: 10386
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1268 :
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.
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.
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1268 :
Aha I understand now, thanx
-
- Posts: 3
- Joined: Thu Jan 14, 2021 9:57 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1268 :
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;
int j, var j = 0;
-
- Site Admin
- Posts: 10386
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1268 :
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.
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.
-
- Posts: 3
- Joined: Thu Jan 14, 2021 9:57 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1268 :
Yeah, sorry, sorry)
I didn't see that: int i = 0, j = 0;
thanks.
I didn't see that: int i = 0, j = 0;
thanks.
Who is online
Users browsing this forum: No registered users and 9 guests