Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: array question

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default array question

    hi sorry but im a complete noob at this programming lark.
    im learning about arrays

    char[][] hhh={{'a','b'},{'c','d'}};
    System.out.println(hhh[1][1]+hhh[1][1]);

    i think this should output d d
    but instead it outputs 200

    can anyone elplain this thanks


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: array question

    Adding 2 char's doesn't concatenate them into a string, it adds their underlying numerical representations.

    To concatenate the two you need to use strings.

    System.out.println("" + hhh[1][1] + hhh[1][1]); // kind of a cheater way to get strings, but it's quite commonly used

  3. #3
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: array question

    Quote Originally Posted by thirddigit View Post
    ... learning about arrays...
    ...
    i think this should output d d
    but instead it outputs 200
    ...
    The result is not some peculiar quirk of arrays. It is a function of how println() works with numerical values and with Strings connected to numerical values with '+'

    Here: I'll do a few examples using a simple char variable instead of an array:
    // Notes about println with characters and numerical
    // expressions involving characters.
    //
    //   Zaphod_b
    //
    public class Z {
        public static void main(String [] args) {
            char ch = 'd';
            int intch = (int)ch;
     
            System.out.println("The (integer) numerical value of ch is " + intch);
     
            System.out.println("\nWith \"println(ch)\", you get the character: ");
            System.out.println(ch);
     
            System.out.println("\nWith \"println((int)ch)\", you get the integer value of the character: ");
            System.out.println((int)ch);
     
            System.out.println("\nWhen you add a character to another character, the characters are");
            System.out.println("\"promoted\" to integers, and integer arithmetic is used to obtain");
            System.out.println("an integer result.");
     
            System.out.println("\nSo...");
     
            System.out.println("\nWith \"println(ch+ch)\", it prints the integer value of the expression:");
            System.out.println(ch+ch);
     
            System.out.println("\nHowever...");
            System.out.println("\nWhen you print a String + a character, it appends the character to the String.");
            System.out.println("With \"println(\"ch + ch = \" + ch + ch)\", here's the result:");
            System.out.println("ch + ch  = " + ch + ch);
     
            System.out.println("\nYou can even use an empty String to force character output to be characters.");
            System.out.println("With \"println(\"\" + ch + ch)\", you get this:");
            System.out.println("" + ch + ch);
        }
    }

    Output:

    The (integer) numerical value of ch is 100

    With "println(ch)", you get the character:
    d

    With "println((int)ch)", you get the integer value of the character:
    100

    When you add a character to another character, the characters are
    "promoted" to integers, and integer arithmetic is used to obtain
    an integer result.

    So...

    With "println(ch+ch)", it prints the integer value of the expression:
    200

    However...

    When you print a String + a character, it appends the character to the String.
    With "println("ch + ch = " + ch + ch)", here's the result:
    ch + ch = dd

    You can even use an empty String to force character output to be characters.
    With "println("" + ch + ch)", you get this:
    dd




    Cheers!

    Z
    Last edited by Zaphod_b; October 1st, 2012 at 02:41 PM.

  4. #4
    Junior Member
    Join Date
    Oct 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: array question

    thanks allot

Similar Threads

  1. Array question
    By knightmetal in forum Collections and Generics
    Replies: 3
    Last Post: April 7th, 2012, 12:05 AM
  2. 2 array question
    By w.kit in forum Java Theory & Questions
    Replies: 1
    Last Post: January 16th, 2012, 07:56 AM
  3. Easy array question
    By surfbumb in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 3rd, 2011, 09:44 PM
  4. 2D Array Question
    By gmorris1986 in forum Java Theory & Questions
    Replies: 2
    Last Post: June 18th, 2010, 11:40 AM
  5. A question about an array
    By faizana2006 in forum Collections and Generics
    Replies: 2
    Last Post: October 28th, 2009, 04:36 PM