Nil. Hatamova wrote:Then i change code like this for check answer is same or not. But it isn't same. Why?
Because there is a very important difference between both code snippets! Let's have a look at the relevant parts of both code snippets. Let's start with the first code snippetIn this for loop you are iterating through the array course using a seperate reference variable c to refer to the EJava object on each iteration. So at the first iteration, reference variable c is referring to the same object as course. But then a newly created EJava object is assigned to reference variable c. But this only affects the value of reference variable c, notcourse and therefore the value of course remains unchanged (and it is still referring to the object created on line5 (which instance member courseName was changed on line6).
Now let's take a look at the second code snippetIn this for loop you are iterating through the array course using the array reference variable course and an index to refer to the EJava object on each iteration. If you assign a new EJava object to this reference variable (e.g. course), it definitely affects this reference variable and this reference variable refers to another object (and thus the content of the array has been changed).
The first for loop is very similar to this code snippetAnd here is another alternative with the same outcome
Now let's see if you have understood all the above with a pop quiz question. What is the output of this code snippet?