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
Printable View
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.
Code java: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:
Code java:// 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
thanks allot