Is this legal?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Debabrata Jana
    New Member
    • Mar 2007
    • 8

    Is this legal?

    hi all,
    Please answer this and give reasons.
    Is this legal?
    long longArr[];
    int intArr[] = { 7 ,8 , 9};
    longArr = intArr;

    Thanks and regards
    Debabrata
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by Debabrata Jana
    hi all,
    Please answer this and give reasons.
    Is this legal?
    long longArr[];
    int intArr[] = { 7 ,8 , 9};
    longArr = intArr;

    Thanks and regards
    Debabrata
    Have you tried it? I guess you didn't (why not?) Of course it's not legal: an intArr
    refers to a sequence of four byte ints that make up the array. A long is
    supposed to refer to a sequence of eight byte longs that make up the array. Do
    you want those conversions to take place behind your back? That'd be quite
    dangerous ...

    kind regards,

    Jos

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by Debabrata Jana
      hi all,
      Please answer this and give reasons.
      Is this legal?
      long longArr[];
      int intArr[] = { 7 ,8 , 9};
      longArr = intArr;

      Thanks and regards
      Debabrata
      The best way to learn this is to try it on the compiler and pay attention to the error messages that you get.

      Comment

      • dmjpro
        Top Contributor
        • Jan 2007
        • 2476

        #4
        So Joash only Type Compatible Array Reference assignment is possible.
        Right???

        Kind regards,
        Dmjpro.

        Comment

        • dmjpro
          Top Contributor
          • Jan 2007
          • 2476

          #5
          Originally posted by Debabrata Jana
          hi all,
          Please answer this and give reasons.
          Is this legal?
          long longArr[];
          int intArr[] = { 7 ,8 , 9};
          longArr = intArr;

          Thanks and regards
          Debabrata
          I think u know how the Array is built in JAVA.
          Actually they r implicitly a class.
          And u don't get the class Name .. because this is managed by JVM.
          So here u r assigning two incompatible object reference and it causes a disaster.
          That's why u r getting a comipler error.
          Right??
          The expalination by Joash is very much effective but mine is the main reason behind it.
          Have a good day.

          Kind regards,
          Dmjpro.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by dmjpro
            So Joash only Type Compatible Array Reference assignment is possible.
            Right???
            There's a bit more to it; imagine the following little class hierarchy:

            [code=java]
            public class Transporter { ...}
            public class Car extends Transporter { ... }
            public class Submarine extends Transporter { ... }
            [/code]

            The two derived classes have an *is-a* relation with the base class: a Car *is-a*
            Transporter and a Submarine *is-a* transporter.

            But: an array of Car isn't an array of Transporter and an array of Submarine isn't
            an array of Transporter. If there were, you would end up with this:

            [code=java]
            Transporter[] temp;
            Car[] carpark= new Car[42];
            carpark[0]= new Car(); // ok
            temp= (Transporter[])carpark // error
            temp[1]= new Submarine(); // of if the previous line were ok
            [/code]

            You may end up with a submarine in your carpark. ;-)

            kind regards,

            Jos

            Comment

            • dmjpro
              Top Contributor
              • Jan 2007
              • 2476

              #7
              I didn't say that Joash.
              U explained with class example.
              Actually the array in java represented as Class implicitly.
              So two incompatible Array object reference assignment is not possible.
              M i right???

              Kind regards,
              Dmjpro.

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by dmjpro
                I didn't say that Joash.
                I know; you said:
                Originally posted by dmjpro
                So Joash only Type Compatible Array Reference assignment is possible.
                I explained that even if U is type compatible (assignable) from T that U[] and T[]
                are not.

                Originally posted by dmjpro
                U explained with class example.
                Actually the array in java represented as Class implicitly.
                So two incompatible Array object reference assignment is not possible.
                M i right???
                Yep, that's correct.

                kind regards,

                Jos

                Comment

                • dmjpro
                  Top Contributor
                  • Jan 2007
                  • 2476

                  #9
                  Ok.
                  Thanks for ur suggestions.
                  Lot of thanks

                  Kind regards,
                  Dmjpro.

                  Comment

                  Working...