Why this simple code gives outofmemory exception? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why this simple code gives outofmemory exception?

public class Program { public static void main(String[] args) { String [] arr={"if this simple string gives error?"}; int i=0; while(i<10) arr[0]+=arr[0]; System.out.println(arr[0]); } }

16th Mar 2017, 5:08 AM
Navin Koyee Rai
Navin Koyee Rai - avatar
3 Answers
+ 11
It's an infinite loop. You have to increment i inside the loop.
16th Mar 2017, 5:13 AM
Tashi N
Tashi N - avatar
+ 1
Because you have unlimited string concatenation, as far as I can see. Your while loop never ends.
16th Mar 2017, 5:16 AM
Ivan G
Ivan G - avatar
+ 1
Ha ha ha, what a pity of me. Such a foolish mistake. Thank you 'Ivan G' and 'Tashi N'.😆
16th Mar 2017, 5:21 AM
Navin Koyee Rai
Navin Koyee Rai - avatar