Paranthesis use

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • adityajain
    New Member
    • Sep 2007
    • 5

    #1

    Paranthesis use

    Well ! I have a confusion.When we length of an array we just use a.length; but to find length of an string we use S.length();.Why paranthesis are not used in case of array,however to call any method we use paranthesis?

    I have a other problem too,i created an object of self defined class MyObject.Now how can i give the string behavior to the object i made,for e.g. the objects can be concated(concat ed because object should behave like string)?
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by adityajain
    Well ! I have a confusion.When we length of an array we just use a.length; but to find length of an string we use S.length();.Why paranthesis are not used in case of array,howvr to call any method we use paranthesis??
    [CODE=java]Class Why {
    static int aField = 0;
    static int aMethod() {
    return 0;
    }
    }[/CODE]

    You say Why.aField
    and
    Why.aMethod();

    Comment

    • adityajain
      New Member
      • Sep 2007
      • 5

      #3
      No!! take this as example:

      class Why
      { int a[] ={1,5,8,3,0,6};
      String S ="Why it happen";
      int l_array,l_strin g;
      l_array = a.length; // return 6
      l_string = S.length(); // return 13
      }

      I mean Why a.length;
      and
      S.length();

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by adityajain
        No!! take this as example:

        class Why
        { int a[] ={1,5,8,3,0,6};
        String S ="Why it happen";
        int l_array,l_strin g;
        l_array = a.length; // return 6
        l_string = S.length(); // return 13
        }

        I mean Why a.length;
        and
        S.length();
        But if you look at my example again, you would see why.
        One is a field and the other is a method.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by adityajain
          No!! take this as example:

          class Why
          { int a[] ={1,5,8,3,0,6};
          String S ="Why it happen";
          int l_array,l_strin g;
          l_array = a.length; // return 6
          l_string = S.length(); // return 13
          }

          I mean Why a.length;
          and
          S.length();
          As r035198x already wrote: the length field for arrays is, well, a field, whille
          the length() method for a String is a method. Arrays are a bit funny: they're all
          objects and direct decendants of the Object class; they're all final (non-extendable)
          as well and they don't have methods defined themselves (you can use Object's
          methods on arrays though).

          Arrays always have been a difficult thing when it comes to object oriented
          programming. A 'Derived' may be a 'Base' but an array of 'Derived' isn't an
          array of 'Base'; don't get me started on this ;-)

          Just remember that arrays are a bit funny.

          kind regards,

          Jos

          Comment

          Working...