Why does the following print 197, but not 'bc'?
System.out.println('b' + 'c');
Can someone explain how to do proper concatenation on Java?
P.S. I learnt some Python, and now transforming to learn Java.
'b' and 'c' are not Strings, they are chars. You should use double quotes "..." instead:
System.out.println("b" + "c");
You are getting an int because you are adding the unicode values of those characters:
System.out.println((int) 'b'); // 98
System.out.println((int) 'c'); // 99
System.out.println('b' + 'c'); // 98 + 99 = 197
'b' is not a String in Java it is char. Then 'b'+'c' prints 197.
But if you use "b"+"c" this will prints bc since "" used to represent String.
System.out.println("b" + "c"); // prints bc
Concatenating chars using + will change the value of the char into ascii and hence giving a numerical output. If you want bc as output, you need to have b and c as String. Currently, your b and c are char in Java.
In Java, String literals should be surrounded by "" and Character are surrounded by ''
Yes single quotation is Char while double quote represent string so:
System.out.println("b" + "c");
Some alternatives can be:
"" + char1 + char2 + char3;
or:
new StringBuilder().append('b').append('c').toString();
'b' and 'c' are not Strings they are characters.
197 is sum of unicode values of b and c
For Concatinating String you can use following 2 ways:
System.out.println("b"+"c");
System.out.println("b".concat("c"));
In Java, Strings literals are represented with double quotes - ""
What you have done is added two char values together. What you want is:
System.out.println("b" + "c"); // bc
What happened with your code is that it added the ASCII values of the chars to come up with 197.
The ASCII value of 'b' is 98 and the ASCII value of 'c' is 99.
So it went like this:
System.out.println('b' + 'c'); // 98 + 99 = 197
As a note with my reference to the ASCII value of the chars:
The char data type is a single 16-bit Unicode character.
From the Docs. However, for one byte (0-255), as far as I'm aware, chars can also be represented by their ASCII value because the ASCII values directly correspond to the Unicode code point values - see here.
The reason I referenced ASCII values in my answer above is because the 256 ASCII values cover all letter (uppercase and lowercase) and punctuation - so it covers all of the main stuff.
Technically what I said is correct - it did add the ASCII values (because they are the same as the Unicode values). However, technically it adds the Unicode codepoint decimal values.
Related
I'm trying to compare two char primitives ch1 and ch2. Both are assigned the value 1 as shown below.
But when compared using the "==" operator it returns false, which I don't understand how or what's happening behind the scenes.
char ch1 = (char)1;
char ch2 = '1';
System.out.println(ch1==ch2); //false
//further comparisions
System.out.println(ch1 == 1); //true
System.out.println(ch1 == '\u0031'); //false
System.out.println(ch2 == 1); //false
System.out.println(ch2 == '\u0031'); //true
'1' has the value 49 (31 hexadecimal).
(char)1 has the value 1.
A char is just a 16-bit integer. The notation 'x' means 'the character code for the character x', where the encoding used in Java is Unicode, specifically UTF-16.
The cast (char) does not change the value of the expression to its right, except that it truncates it from a full-size integer to 16 bits (which is no change for values 0 to 65535).
Basically what you are doing is casting the number one as a char, so ch1 is now equals to unicode character 1 (SOH or Start of Header)
So when you compare ch1 (SOH) to ch2 ('1') its going to return false
As well if you compare ch1 (SOH - \u0001) to `'1' - \u0031 is going to return false
That's the main reason why is returning false, the unicode value of ch1 that you expect is different from the one you assigned
Code point
The char type is essentially broken since Java 2, physically incapable of representing most characters.
Instead use code point integer numbers. Every character is permanently assigned a specific number, a code point.
int codePoint = "1".codePointAt( 0 ) ; // Annoying zero-based index counting.
The result is 49 decimal, 31 hexadecimal.
Make a string of that single character per the code point.
String s = Character.toString( codePoint ) ;
Or more specifically:
String latinDigitOneCharacter = Character.toString( 49 ) ;
As others pointed out, your code was mistakenly comparing the character defined as the Latin digit “1” with a code point of 1.
The character assigned to the code point of one is the control code SOH, Start of Heading. This is true in both Unicode and US-ASCII (Unicode is a superset of US-ASCII).
Getting invalid unicode error with below code
Uniocde want to print: unicode:0x16
PrintWriter pw = new PrintWriter(System.out, true);
char aa = "\u0x16";
pw.println(aa);
What's wrong happening here ?
\u0x16 is not a valid unicode character reference. There should be 4 hexadecimal digits (numbers 0-9 letters a-f) after \u - the "x" is not valid.
If you meant to use the character U+0016, it's written as \u0016:
char aa = '\u0016';
The following is equivalent, but it uses an integer constant rather than a character constant.
char aa = 0x16;
I'm looking for a straightforward answer and can't seem to find one.
I'm just trying to see if the following is valid. I want to take the integer 7 and turn it into the character '7'. Is this allowed:
int digit = 7;
char code = (char) digit;
Thank you in advance for your help!
This conversion is allowed, but the result won't be what you expect, because char 7 is the bell character whereas '7' is 55 (0x37). Because the numeric characters are in order, starting with '0' at 48 (0x30), just add '0', then cast the result as a char.
char code = (char) (digit + '0');
You may also take a look at the Unicode characters, of which the printable ASCII characters are the same codes.
'7' is Unicode code point U+0037.
Since it is a code point in the Basic Multiligual Plane, and since char is a UTF-16 code unit and that there is a one-to-one mapping between Unicode code points in this plane and UTF-16 code units, you can rely on this:
(char) ('0' + digit)
Do NOT think of '7' as ASCII 55 because that prevents a good understanding of char... For more details, see here.
Nope. The char '7' can be retrieved from int 7 in these ways:
int digit = 7;
char code = Integer.toString(digit).charAt(0);
code = Character.forDigit(digit, 10);
If digit is between 0 and 9:
int digit = 7;
char code = (char)(((int)'0')+digit);
So I just started reading "Java In A Nutshell", and on Chapter One it states that:
"To include a character literal in a Java program, simply place it between single quotes"
i.e.
char c = 'A';
What exactly does this do^? I thought char only took in values 0 - 65,535. I don't understand how you can assign 'A' to it?
You can also assign 'B' to an int?
int a = 'B'
The output for 'a' is 66. Where/why would you use the above^ operation?
I apologise if this is a stupid question.
My whole life has been a lie.
char is actually an integer type. It stores the 16-bit Unicode integer value of the character in question.
You can look at something like http://asciitable.com to see the different values for different characters.
In Java char literals represent UTF-16 (character encoding schema) code units. What you got from UTF-16 is mapping between integer values (and the way they are saved in memory) with corresponding character (graphical representation of unit code).
You can enclose characters in single quotes - this way you don't need to remember UTF-16 values for characters you use. You can still get the integer value from character type and put if for example in int type (but generally not in short, they both use 16 bits but short values are from -32768 to 32767 and char values are from 0 to 65535 or so).
If you look at an ASCII chart, the character "A" has a value of 41 hex or 65 decimal. Using the ' character to bracket a single character makes it a character literal. Using the double-quote (") would make it a String literal.
Assigning char someChar = 'A'; is exactly the same as saying char someChar = 65;.
As to why, consider if you simply want to see if a String contains a decimal number (and you don't have a convenient function to do this). You could use something like:
bool isDecimal = true;
for (int i = 0; i < decString.length(); i++) {
char theChar = decString.charAt(i);
if (theChar < '0' || theChar > '9') {
isDecimal = false;
break;
}
}
I have the following class:
public class Go {
public static void main(String args[]) {
System.out.println("G" + "o");
System.out.println('G' + 'o');
}
}
And this is compile result;
Go
182
Why my output contain a number?
In the second case it adds the unicode codes of the two characters (G - 71 and o - 111) and prints the sum. This is because char is considered as a numeric type, so the + operator is the usual summation in this case.
+ operator with character constant 'G' + 'o' prints addition of charCode and string concatenation operator with "G" + "o" will prints Go.
The plus in Java adds two numbers, unless one of the summands is a String, in which case it does string concatenation.
In your second case, you don't have Strings (you have char, and their Unicode code points will be added).
System.out.println("G" + "o");
System.out.println('G' + 'o');
First one + is acted as a concat operater and concat the two strings. But in 2nd case it acts as an addition operator and adds the ASCII (or you cane say UNICODE) values of those two characters.
This previous SO question should shed some light on the subject, in your case you basically end up adding their ASCII values (71 for G) + (111 for o) = 182, you can check the values here).
You will have to use the String.valueOf(char c) to convert that character back to a string.
The "+" operator is defined for both int and String:
int + int = int
String + String = String
When adding char + char, the best match will be :
(char->int) + (char->int) = int
But ""+'a'+'b' will give you ab:
( (String) + (char->String) ) + (char->String) = String
+ is always use for sum(purpose of adding two numbers) if it's number except String and if it is String then use for concatenation purpose of two String.
and we know that char in java is always represent a numeric.
that's why in your case it actually computes the sum of two numbers as (71+111)=182 and not concatenation of characters as g+o=go
If you change one of them as String then it'll concatenate the two
such as System.out.println('G' + "o")
it will print Go as you expect.