i would understand why the output is 3 and not a char ('5' unicode character)
char c='5';
c = (char) (c - 2);
System.out.println(c);
and could you please explain what's the difference beetween code ASCII and unicode character ?
thank you in advance :)
It is a character. It's a character representing the numeral 3, just as '3' is a character on your keyboard.
When you subtract one from a character, you get the character immediately before that. For example, 'B' - 1 = A.
You start with the character '5' and subtract two, giving the character '3'. If you subtracted 6, you would not get -1, you'd get a random character ('/' I think).
Basically this subtraction works because the characters 0 - 9 are stored contiguously.
The output is the char '3', not the number 3.
When you subtract 2 from the char '5' and cast the result to char, you get the char '3'.
The char type is a numeric primitive type. Each character has a corresponding integer value between 0 and 2^16-1. The integer value of the character '3' is smaller by 2 than the value of the character '5'.
Related
I have a capital letter defined in a variable string, and I want to output the next and previous letters in the alphabet. For example, if the variable was equal to 'C', I would want to output 'B' and 'D'.
One way:
String value = "C";
int charValue = value.charAt(0);
String next = String.valueOf( (char) (charValue + 1));
System.out.println(next);
Well if you mean the 'ABC' then they split into two sequences a-z and A-Z, the simplest way I think would be to use a char variable and to increment the index by one.
char letter='c';
letter++; // (letter=='d')
same goes for decrement:
char letter='c';
letter--; // (letter=='b')
thing is that the representation of the letters a-z are 97-122 and A-Z are 65-90, so if the case of the letter is important you need to pay attention to it.
If you are limited to the latin alphabet, you can use the fact that the characters in the ASCII table are ordered alphabetically, so:
System.out.println((char) ('C' + 1));
System.out.println((char) ('C' - 1));
outputs D and B.
What you do is add a char and an int, thus effectively adding the int to the ascii code of the char. When you cast back to char, the ascii code is converted to a character.
All the answers are correct but none seem to give a full explanation so I'll try. Just like any other type, a char is stored as a number (16-bit in Java). Unlike other non-numeric types, the mapping of the values of the stored numbers to the values of the chars they represent are well known. This mapping is called the ASCII Table. The Java compiler treats chars as a 16-bit number and therefore you can do the following:
System.out.print((int)'A'); // prints 65
System.out.print((char)65); // prints A
For this reason, the ++, -- and other mathematical operations apply to chars and provide a way to increment\decrement their values.
Note that the casting is cyclic when you exceed 16-bit:
System.out.print((char)65601); // also prints A
System.out.print((char)-65471); // also prints A
P.S. This also applies to Kotlin:
println('A'.toInt()) // prints 65
println(65.toChar()) // prints A
println(65601.toChar()) // prints A
println((-65471).toChar()) // prints A
just like this :
System.out.printf("%c\n",letter);
letter++;
String c="12345";
for(char k:c.toCharArray())
System.out.print(k+4);
This program outputs:
5354555657
I don't really understand why this is out putting those numbers. The only pattern I see is that it prints a "5" then takes the "1" from the string and adds 2 to make "3". Then prints a "5" then takes the "2" from the string and adds 2 to make "4" then prints a "5" and so on.
The characters in the array, when promoted to int for the addition of 4, take on their underlying Unicode value, of which the ASCII values are a subset. The digits 0-9 are represented by codes 48-57 respectively. Characters '1' through '5' are 49-53, then you add 4 and get 53-57.
After adding, cast the sum back to char so print can interpret it as a char.
System.out.print( (char) (k+4));
Output:
56789
This is because you're adding an int to a character (it's casting your character to an int and then adding it to 4, then printing it out).
You need to do:
System.out.println(Character.getNumericValue(k) + 4);
I want to know that how to recognize and print next character in ASCII sequence if input is a non- string value like "space" or "!".
I know that for string value we can convert it into ASCII value by using
char character = 'a';
int ascii = (int) character;
Then adding 1 to it and converting it back to char , we can get next value in the sequence .
You can use:
char character = 'a';
int ascii = (char)((int)character+1);
It should work. But I have haven`t tested it.
I want to know why char ch =5; (for example)
is not error ? but if I print
System.out.println(Character.isDigit(ch));
// output
false
it will be false ?
Because 5 is an integer literal that can be converted to a char. It is not the character '5' however.
A character is represented by two bytes in memory. Java converts 5 to a character.
'5' is not the 6th character (its hexadecimal code is 35 and not 5) in the ASCII table and is thus not a "digit".
try this example :
char ch = 97;
JOptionPane.showMessageDialog(null,"ch = "+ch);
The answer would be : ch = a
It simply won't give an error even though 97 is without (' ') because 97 represent the ASCII code for the character 'a' so it's not a digit , and that's why you are getting false as a result.
if you give ch = 5, it's automatically covert to char based on ASCII value.
In Currency.java file there is a line.
private static final int A_TO_Z = ('Z' - 'A') + 1;
What means is this? I didn't see this before. What is A_TO_Z's value and why it using 'Z' instead number.
With this expression you are treating chars as ints, using character's Unicode value instead of the character itself.
'Z' - 'A' + 1
Will become
90 - 65 + 1 (=26)
'Z' is a char with an integral value of 90.
'A' is a char with an integral value of 65.
90 - 65 + 1 = 26
Nasty. 'A' is the char literal for ASCII value of A (65 in decimal). 'Z' is 90. So A_TO_Z is 26, the number of letters in the English alphabet.
Characters have numeric values according to their value in the character table. That expression exploits the fact that all the letters form A to Z have consecutive values in the underlying encoding table thus subtracting the first value from the last ( + 1) gives the length of the English alphabet. The actual numerical values are unimportant in this case and the code is more or less self-explainable to the reader. In case the used encoding spreads the letters differently, the expression will become incorrect.