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
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.
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
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
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.
thanks allot