initialisation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • veremue
    New Member
    • Mar 2007
    • 14

    #1

    initialisation

    why is the following an illegal initialisation

    java code:

    String s[];
    s = {"mmmmm","jjjjj j","nnnn"}
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by veremue
    why is the following an illegal initialisation

    java code:

    String s[];
    s = {"mmmmm","jjjjj j","nnnn"}
    You can have a look at the arrays entry in the JLS.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by veremue
      why is the following an illegal initialisation

      java code:

      String s[];
      s = {"mmmmm","jjjjj j","nnnn"}
      That's not initialisation, that's an assignment. Assignments go like this:
      Code:
      s= new String[] { "aaa", "bbb", "ccc };
      and initialisation goes like this:
      Code:
      String[] s= { "aaa", "bbb", "ccc" };
      kind regards,

      Jos

      Comment

      • emekadavid
        New Member
        • Mar 2007
        • 46

        #4
        that code is wrong because s is an array variable and to initialise arrays u use the array index which in java is index zero. got it.
        String[] s = {"n", "u", "any"};
        works because this indexing understood by your compiler but
        s = {"n", "u", "any"}; will not be understood by your compiler because, 1. there is no index to signify this is an array, 2. if you index it this late, what index will the compiler look up for?
        maybe try:
        s[1] = "n";
        s[2] = "u";
        got it.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by emekadavid
          that code is wrong because s is an array variable and to initialise arrays u use the array index which in java is index zero. got it.
          String[] s = {"n", "u", "any"};
          works because this indexing understood by your compiler but
          s = {"n", "u", "any"}; will not be understood by your compiler because, 1. there is no index to signify this is an array, 2. if you index it this late, what index will the compiler look up for?
          maybe try:
          s[1] = "n";
          s[2] = "u";
          got it.
          It has nothing to do with indexes whatsoever. See my previous reply.

          kind regards,

          Jos

          Comment

          • prometheuzz
            Recognized Expert New Member
            • Apr 2007
            • 197

            #6
            Originally posted by emekadavid
            that code is wrong
            ...
            No offense meant but like many of your replies, this one is again wrong. Perhaps it it a good thing to first check the things you post. You could've easily verified that there's nothing wrong with the line:
            [CODE=java]String[] array = {"Fu", "Bar"};[/CODE]

            If you post something wrong, a beginning programmer might take your word for it and gets side tracked by your false claims.

            If I post sometthing I'm not 100% sure of it is true, I either do not post it, or I tell the OP that I am not 100% sure about my claims. That way s/he knows to be carefull with the information provided.

            Again: no offense meant.

            Comment

            Working...