×

Loading...

Topic

This topic has been archived. It cannot be replied.
  • 工作学习 / IT技术讨论 / What's the difference between "int a[] = new int[5]" and "int[] a = new int[5]"?
    • same
      • Can u explain the following codes for me more detailly?
        public class example {
        int i[] = {0};
        public static void main(String args[]) {
        int i[] = {1};
        change_i(i);
        //When the code is executed,first this change_i's i[] points to the i[] which is declared and instanciated in main(), then the main call the routine change_i,after executing "i=j" in it ,the i(a local variable of change_i routine) point to j,in the meantime ,there are two array instance in the memory,so it has no relation with "i" that is declared in main()
        //anser is c,the output is 1
        System.out.println(i[0]);
        }
        public static void change_i(int i[]) {
        int j[] = {2}; //"j[]"'s visibility is local
        i = j; //"i" points to "j"
        }
        }


        A. The program does not compile.
        B. The program prints 0.
        C. The program prints 1. (right)
        D. The program prints 2.
        E. The program prints 4.
        • Is my question too dummy that no body want to answer it?Jabber and sailor, could u please explain the above codes for me? thanks..
          • java method passes object parameter (such as Array) by reference.
            • I just want to know how the change_i(i) works and why the answer is print 1 not print 2.. What does it mean-- it has no relationship with"i" that is declared in main()--in the comments.
              • if you replace "i=j" with "i[0]=j[0]", the answer will be "D". You can think i is a pointer.
              • I'm still confused how comes the method change_i(int i[]){ ... i=j; } is valid..
                • more detailed.
                  In C/C++, i of i[] is a constant pointer. In JAVA, instead, i of i[] is a variable, which points to a Array Object. When you call change_i(i), the value of i (not i itself) is passed into the methed, within which a new parameter variable i is created and assigned by the passed value.
            • thanx, u answered my question but i didn't understand b4..
    • Jabber's explanation
      本文发表在 rolia.net 枫下论坛Let me try to explain this quiz

      Here are premises:

      1) This example is designed a little bit confusing--- All of its Arrays have only one element.

      2) An int like x=7 is an int, not an Object. Once an int is put into an Array, that Array
      is an Object--- of course, an Object (Array) containing the int.

      3) In normal cases, Java passes Object by reference. (In RMI, it can pass by value).

      Now let's come to the quiz itself.

      1) As JVM loads example class, i[], which is an instance varaible, is initialized to {0} in the JVM;

      2) JVM finds main() method in example class and executes it. Now i[] is pointed to another Array
      {1} in the JVM.

      3) Executes change_i(int i[]). This is where confusion may arise. What is passed into the method
      call is "a copy of the reference of i[]". In the execution, this copy is directed to j[].
      Eventually, this copy is pointed to {2} in the JVM.

      4) Reminder: change_i just manipulates on " that copy of the reference of i[]".

      5) As change_i is finished, that "copy of reference of i[]" is out of scope. It is gone. But, but, but, ..... but this does no harm on the original i[], which still points to {1} in the JVM.

      6) Now we come to the conclusion that System.out will show us number "1".

      7) That's it.更多精彩文章及讨论,请光临枫下论坛 rolia.net
      • Jabber, i have been waiting for ur reply from yesterday..i believe u can give a clearer expalination and u did..(i thought my question here is too dummy that u don't want to give any reply)..:-)
        most of others can get the right answer but gave me different versions of explain that confused me even more. I think your explaination is to the point. it reminds me of an example i tried b4.
        in this example, an object Day d works as Function argument:

        static void changeDay(Day d, int yearsDelayed)
        //won't work
        { int month = d.getMonth();
        int day = d.getDay();
        int year = d.getYear();}
        d=new Day(year + yearsDelayed, month, day);
        }

        and suppose we call this function
        ely = new Day(1999,10,15);
        changeDay(ely,2);

        "ely" will not have the correct date i want because the function only did something on the "copy of the object" but not on the object itself, right?