Results 1 to 3 of 3

Thread: Array of Char's

  1. #1
    tariqm is offline Member
    Join Date
    Apr 2012
    Posts
    13
    Rep Power
    0

    Default Array of Char's

    Hi All,
    I am having difficulty to understand the difference between:

    Java Code:
    char [] myOriginalArray = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'};
    and

    Java Code:
     char [] myOriginalArray;
    		
    		myOriginalArray = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'};
    Why eclipse wont compile the second but will compile the first one.........
    both are exactly the same to me.
    any thought????

    Tariq

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default Re: Array of Char's

    The first form is an array initialization, while the second form is an assignment and the compiler doesn't know what its type is; you think you can see that its an array of chars, but to the compiler it can also be an array of doubles, or longs, or ints. Because it is ambiguous to the compiler it doesn't allow it. You can do this though:

    Java Code:
           myOriginalArray = new char[] {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'};
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    tariqm is offline Member
    Join Date
    Apr 2012
    Posts
    13
    Rep Power
    0

    Default Re: Array of Char's

    Quote Originally Posted by JosAH View Post
    The first form is an array initialization, while the second form is an assignment and the compiler doesn't know what its type is; you think you can see that its an array of chars, but to the compiler it can also be an array of doubles, or longs, or ints. Because it is ambiguous to the compiler it doesn't allow it. You can do this though:

    Java Code:
           myOriginalArray = new char[] {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'};
    kind regards,

    Jos
    Thanks for the quick reply Jos much appreciated.....

    Tariq

Similar Threads

  1. convert byte array into char array
    By kgkamaraj in forum New To Java
    Replies: 4
    Last Post: 09-13-2011, 11:32 AM
  2. Replies: 2
    Last Post: 03-08-2011, 06:20 AM
  3. create a 2d char array from a 1D string array
    By jschmall12 in forum New To Java
    Replies: 1
    Last Post: 04-27-2010, 09:01 PM
  4. array of char
    By sinisab in forum New To Java
    Replies: 9
    Last Post: 01-05-2010, 09:48 AM
  5. Convert Char Array to String Array
    By Mayur in forum New To Java
    Replies: 8
    Last Post: 10-12-2009, 11:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •