arrays in JAVA

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 30081986
    New Member
    • Nov 2007
    • 2

    arrays in JAVA

    Hello Everyone..
    The following code module is giving 13 as the answer in JAVA
    class Number
    {
    public static void main(string args[])
    {
    int arr[] = {4,8,16};
    int i = 1;
    arr[++i] = --i;
    System.out.prin tln(arr[0]+arr[1]+arr[2]);
    }

    I m getting the same output n c# as well...

    But the same code in c and c++ is giving the output as 21.It is as below...

    #include <stdio.h>
    void main()
    {
    int arr[] = {4,8,16};
    int i = 1;
    arr[++i] = --i;
    printf("%d",arr[0]+arr[1]+arr[2]);
    }

    How can the same program give different outputs in diffrent languages when the concept of array remains the same inall languages?
    Can anyone tell me please?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Java evaluates expressions strictly from left to right, so first (on the left of the
    assignment operator) i will be equal to 2 and on the right side of that same operator
    the value will be one (1) again, effectively assigning a[2] = 1. 4+8+1 == 13.

    In C and C++ this expression causes 'undefined behaviour' by definition of the ISO
    Standard that is, so nothing can be said about the value of the expression, no
    matter that your implementations returned 21. Even daemons could fly out of
    your nose because of the undefined behaviour.

    C# was simply stolen from Sun's Java so I'm rather confident that it'll return 13 too.

    kind regards,

    Jos

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by 30081986
      Hello Everyone..
      The following code module is giving 13 as the answer in JAVA
      class Number
      {
      public static void main(string args[])
      {
      int arr[] = {4,8,16};
      int i = 1;
      arr[++i] = --i;
      System.out.prin tln(arr[0]+arr[1]+arr[2]);
      }

      I m getting the same output n c# as well...

      But the same code in c and c++ is giving the output as 21.It is as below...

      #include <stdio.h>
      void main()
      {
      int arr[] = {4,8,16};
      int i = 1;
      arr[++i] = --i;
      printf("%d",arr[0]+arr[1]+arr[2]);
      }

      How can the same program give different outputs in diffrent languages when the concept of array remains the same inall languages?
      Can anyone tell me please?
      Yep, C# would give 13 for the reasons given by Jos above.

      P.S In Java you want to have the argument to main as String args[] not string args[]

      Comment

      • 30081986
        New Member
        • Nov 2007
        • 2

        #4
        Originally posted by JosAH
        Java evaluates expressions strictly from left to right, so first (on the left of the
        assignment operator) i will be equal to 2 and on the right side of that same operator
        the value will be one (1) again, effectively assigning a[2] = 1. 4+8+1 == 13.

        In C and C++ this expression causes 'undefined behaviour' by definition of the ISO
        Standard that is, so nothing can be said about the value of the expression, no
        matter that your implementations returned 21. Even daemons could fly out of
        your nose because of the undefined behaviour.

        C# was simply stolen from Sun's Java so I'm rather confident that it'll return 13 too.

        kind regards,

        Jos


        Thanks for the reply..
        I have a small doubt..Since we are using the same 'i' as the index to access the array elements,should n't reflect back on the left hand side when the decrement operation is done on the right hand side..i.e we are using 'i' as the index to access the array elemts and 'i' becomes 2 first on the right side and the same 'i' becomes 1 on the left hand side ,so effectively i now has 1 and arr[i] is now arr[1] and hence it should replace the element in the first position of the array by 1 is it?because we are altering the same i(same memory location) on the both sides is it?

        can u assist me please?


        regards
        30081986

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by 30081986
          Thanks for the reply..
          I have a small doubt..Since we are using the same 'i' as the index to access the array elements,should n't reflect back on the left hand side when the decrement operation is done on the right hand side..i.e we are using 'i' as the index to access the array elemts and 'i' becomes 2 first on the right side and the same 'i' becomes 1 on the left hand side ,so effectively i now has 1 and arr[i] is now arr[1] and hence it should replace the element in the first position of the array by 1 is it?because we are altering the same i(same memory location) on the both sides is it?

          can u assist me please?


          regards
          30081986
          Let's go through this code fragment step by step:

          [code=java]
          int i= 1;
          a[++i]= --i;
          [/code]

          - first i is initialized to the value 1, the easy part.

          - Java evaluates everything from left to rightl no exception to the rule so first
          ++i is evaluated; the result is 2 and that's the value of i as well.

          - a[2] is the target of the assignment.

          - next --i is evaluated and its value is 1 which is the value of i as well now.

          - a[2]= 1 is evaluated; it is an assignent so a[2] is set to the value 1.

          Does that clarify things a bit more?

          kind regards,

          Jos

          Comment

          Working...