-5

It may sound so silly to ask but still I want to know what will happen if i assign value of string within for loop. Let's say

 String name = " darsha" ;

 for ( i = 0 ; i < 10 ; i ++ )
{
     name = darsha ; 
}

What will happen internally? Will there be only one name instance in string pool or 10

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
ani
  • 446
  • 1
  • 9
  • 31

3 Answers3

1

This will unnecessary utilize memory as well cpu of your machine. As output going to ramain the same that name="darsha" So unneccesary wasting of memory,cpu utilization 7 wastage of java heap nothing else. String name = " darsha" ;

 for ( i = 0 ; i < 10 ; i ++ )
{
     name = "darsha" ; 
}
NIket
  • 914
  • 1
  • 6
  • 19
0

yes there will only be ONE name instance. :)

and there is a mistake in your internal name as well. It should be corrected with inverted commas as the darsha is a string value. name = " darsha" ;

Iresha Rubasinghe
  • 913
  • 1
  • 10
  • 27
0

You must add int before "i" in the loop,and it should be " darsha", instead of just darsha,if you modify like that the last result is name = " darsha"

three
  • 5
  • 3