Reputation:
I'm a beginner in java and this code was used in a book I'm reading, however I can't seem to figure out how it works.
The code:
public class NumberTriangleWhile {
public static void main(String[] args) {
int number = 0;
String output = "";
while (number < 10) {
output = output + number;
System.out.println(output);
number++;
}
}
}
The output:
0
01
012
0123
01234
012345
0123456
01234567
012345678
0123456789
I don't understand why each number is printed and then somehow stored and reused in the next line, can someone explain this please?
Upvotes: 0
Views: 130
Reputation: 1
Each number is getting printed because its getting concatenated.
When number=0
,
And you are storing the value of number into the output as:
Output=output+number
Then the number which is an integer gets stored as a string in the output
Each time the loop runs the new incremented number gets concatenated with the previous value in the string output.
That's why all numbers are getting printed each time.
make this change in your code and you'll get your output
public static void main(String[] args)
{
int number=0;
String output="";
while(number <10)
{
output=output+number;
System.out.println(output);
number++;
output="";
}
}
Upvotes: 0
Reputation: 2719
In every step inside while
loop, output
added with number
, in Java adding something with String
will result in String
for example:
String str = "a" + 2;
results in str="a2";
.
If we start our loop, in first step number = 0
and output=""
hence output = "" + 0
that make output = "0"
in second run number=1
hence output = "0"
(Old output value) + 1
that make output = "01"
and so on
Upvotes: 1
Reputation: 271050
output
is a string variable. When you add something to it like this:
output = output + number;
It does not add the numerical value of the number
, but instead just joins the number with the original string. For example, if output
was originally 1
and number
is 2
, the above line will change output
to 12
, not 3
.
The loop keeps looping until number
is 10. In the first iteration, output
changed from an empty string to 0
. In the second iteration (number
has now increased to 1), output
changed to 01
(the original 0
joined with the current value of number
- 1). In the third iteration, number
is incremented to 2. 2
is then added to the end of output
to form 012
. This carries on until number is 10.
The misconception you have might be that you think output
becomes empty after you print it. It does not. It will still hold the same value.
Upvotes: 2
Reputation: 163
The string output is defined outside of the while loop. It can be read from and written to inside of the while loop and its contents will be consistent in the next iteration.
If output would be defined inside of the while loop, for example:
while ( number < 10) {
String output = "";
output += number;
}
then your output would just be (with new line after each number) 0 1 2 3 4 5 6 7 8 9
Since the new number is consistent throughout each iteration of the while loop, it is "stored" as you say
Upvotes: 0