简体   繁体   中英

Loops in serializing arrays

Example 1

         Person person = (Person)os.readObject();
         System.out.println(person);
         person = (Person)os.readObject();
         System.out.println(person);
         person = (Person)os.readObject();
         System.out.println(person);

Example 2

for(int i=0;i<num;i++) {
            Person person = (Person)os.readObject();
            System.out.println(person);
        }

Obviously, they both work fine when reading objects since one is just a looped version of another, but my main curiosity lies in why in Example 2, the Person object is being looped but there is no error, but if I do this:

Example 3

     Person person = (Person)os.readObject();
     System.out.println(person);
     Person person = (Person)os.readObject();
     System.out.println(person);
     Person person = (Person)os.readObject();
     System.out.println(person);

I get errors for duplicate variables. I thought that Example 3 was the literal same thing as what the for loop was doing in Example 2, anyone mind explaining?

You are re-declaring the variable with the same name (illegal)

try

 Person person = (Person)os.readObject();
 System.out.println(person);
 person = (Person)os.readObject();
 System.out.println(person);
 person = (Person)os.readObject();
 System.out.println(person);

You can declare variables in a loop. Every time the loop is executed, it reads the declaration of the variable, and all is ok. Otherwise, you would not be able to declare variables in a loop. Effectively, you declare it only once. But in example 3 you are declaring the same variable three times.

Actually your question is not about serializing arrays, but about very basic java knowledge.

In example 1 you reuse the variable, so it's ok. In example 3 you try to declare an already existing variable which gives a compile error. Your problem is your understand of example 2. In 2, the code does not get duplicated on compile time or something. You create a block of code that is being executed several times in a loop.

If you declare a variable within a block, it only exists within that block. At the end of one loop of your for-loop, every variable you declared in the block will get "deleted"/thrown away.

You can even use "final" for your variable. Sample:

public static void main(String[] args) {

    final int[] samples = { 1, 3, 5, 7, 11 };
    for (int i = 0; i < samples.length; i++) { // Start of block
        // works because value is gone after this block
        final int value = samples[i];
        System.out.println(value);
    } // end of block

    // Won't work:
    System.out.println(value);
}

See my comment in cannot be resolved to variable For another example on how the curly brackets work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM