Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By JosAH

Thread: Increment array and got unexpecting result

  1. #1
    sheng8 is offline Member
    Join Date
    Aug 2014
    Posts
    2
    Rep Power
    0

    Default Increment array and got unexpecting result

    Hi All,
    I have a newbie question which makes me struggle couple hours and search for nothing. will you please kindly help me on this? i have no one to ask since i am study by my own. thank you for your time and support.

    Java Code:
    char array[] = new char[10];
    int i = 0;
    array[i++] = (char)('A' + i); 
    System.out.println("char for array is: " + array[0]);
    However, the result for array[0] is B. Not A. from my understanding, the array[0] should be 'A'.

    can anyone welling to share the opinion? appreciate for your help.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default Re: Increment array and got unexpecting result

    'B' should be printed: Java evaluates strictly from left to right, so array[0] should be assigned, i equals 1 now and the value to be assigned is 'A'+1.

    kind regards,

    Jos
    sheng8 likes this.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    sheng8 is offline Member
    Join Date
    Aug 2014
    Posts
    2
    Rep Power
    0

    Default Re: Increment array and got unexpecting result

    Thank you Jos, this is really helpful and now i totally understand the concept.

    Best Regards.

Similar Threads

  1. remove duplicates in array gives wrong result
    By hacikho in forum New To Java
    Replies: 1
    Last Post: 11-25-2011, 04:31 AM
  2. Replies: 1
    Last Post: 11-13-2011, 12:25 PM
  3. Struts 2 error : No result defined for action / result
    By sameerk in forum Web Frameworks
    Replies: 1
    Last Post: 05-17-2011, 11:15 AM
  4. Replies: 4
    Last Post: 08-19-2010, 02:06 AM
  5. Replies: 4
    Last Post: 03-20-2010, 05:17 AM

Tags for this Thread

Posting Permissions

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