1
In the following loop, how many times the variable x
will be created at the end of loop? Why?
for (int i = 0; < 10; i++)
{
double x = i + 10;
System.out.println(x);
}
1
In the following loop, how many times the variable x
will be created at the end of loop? Why?
for (int i = 0; < 10; i++)
{
double x = i + 10;
System.out.println(x);
}
14
The word "created" there is a little complicated. Informally we use the word, but creation is not quite the term that occurs there in this code. There is a variable declaration x
, and only once, so you wonder how many times it happens one. If you want to know how many variables are created there, it’s just one. If you want to know how many spaces in memory are allocated to this variable, it’s just one space. For all purposes there is only creation of a variable.
If by variable we understand what it really is, namely a storage place with a name for easy access, so there is only the creation of one variable.
There are 10 assignments in this only variable and therefore are created 10 values (object instances) that are successively stored in the same variable, so despite the positive votes and acceptance, the other answers are wrong and confused with the idea of variable and value (one of them even answers the question).
When you declare a local variable to a method a space in the pile. A space is reserved for each variable and the space depends on the size of the object, and the objects by reference have the size of a pointer. In your case, there’s only one statement and one space. It is guaranteed that each of the 10 passed in this variable statement only needs one space and has no reason to reserve more than one space for it.
The same way if you have different levels of scope only need to reserve space for the largest amount of variables that exist at the same time (considering their sizes). So if a variable is used in a block with defined scope and then another variable is created in another distinct, non-nested block, although having two statements of variables only takes one space for one of the variables, even if they are different. The compiler always creates the space as optimally as possible. The variable statement is not a code execution mechanism, it is a space structuring command in memory and only needs to do this once in the loop. It’s the same as doing this, but keeping scope:
double x;
for (int i = 0; i< 10; i++) {
x = i + 10;
System.out.println(x);
}
I don’t know if you have any way to show Java-generated Assembly online, but in C++ you have how and I will show how it makes no difference (Java is no different than C++):
main:
push rbx
mov ebx, 10
.L2:
pxor xmm0, xmm0
mov edi, OFFSET FLAT:_ZSt4cout
cvtsi2sd xmm0, ebx
add ebx, 1
call std::basic_ostream<char, std::char_traits<char> >& std::basic_ostream<char, std::char_traits<char> >::_M_insert<double>(double)
cmp ebx, 20
jne .L2
xor eax, eax
pop rbx
ret
Behold in the Compiler Explorer.
main:
push rbx
mov ebx, 10
.L2:
pxor xmm0, xmm0
mov edi, OFFSET FLAT:_ZSt4cout
cvtsi2sd xmm0, ebx
add ebx, 1
call std::basic_ostream<char, std::char_traits<char> >& std::basic_ostream<char, std::char_traits<char> >::_M_insert<double>(double)
cmp ebx, 20
jne .L2
xor eax, eax
pop rbx
ret
Behold in the Compiler Explorer.
Note that gives the same generated code.
-2
10 times, i starts at 0 and goes up to smaller (<) than 10 ie 9 times
i= 0
i= 1
i= 2
i= 3
i= 4
i= 5
i= 6
i= 7
i= 8
i= 9
But the question is about the variable x
, nay i
. Are the same things? Why?
They are not the same thing, but the variable i that determines how many times it will enter the loop, and within that loop the variable x is being created, printed and removed.
Then the variable will be created at runtime and not at compile time?
every variable that is instantiated within a FOR or WHILE has the life cycle within FOR or WHILE
Browser other questions tagged java variables loop variable-declaration
You are not signed in. Login or sign up in order to post.
In fact, the correct term would be to create an instance of the Java double class.
– Lucas Augusto Coelho Lopes
@Lucasaugustocoelholopes not exactly,
double
is not a class, but in fact there is the creation of 10 instances of an object of the typedouble
and all placed in only one variable.– Maniero
@Lucasaugustocoelholopes It would be a class if it were
Double
(with a capital "D").double
(with tiny "d") is a primitive type, not a class.– hkotsubo
Truth. Pardon the misunderstanding.
– Lucas Augusto Coelho Lopes
@Maniero "got confused with the idea of variable and value." is correct, but sometimes for being a new user he wants to know how many values will be created, I understood this way and focused on this explanation to be more didactic
– Luiz Augusto
@Luizaugusto in fact his answer does not make the mistake of the others, precisely why nor answers what was asked, he does not want to know what is wrong in the code, it was a typo not have put the variable there. So there’s no mistake because it doesn’t answer the question. The people who voted for the answer probably didn’t read the question. She would be a great answer if the question was what the mistake is in the code, and then I voted for her and wouldn’t even have answered it. Then the question would be bad and should be closed. Sopt is getting very bad, people vote without any criteria more.
– Maniero