I'm new to programming in java and I do not understand a println statement. I can run it but cannot understand what it does. Can anyone explain what this does?
System.out.println((char) ('a' + 4));
'a' is a char literal;
char is an intergral numeric type in Java, just like int;
+ occurring between two numeric values means addition;
(char) indicates a conversion into a char type;
a char, when printed, renders as the Unicode codepoint it represents.
That simply prints out the letter e. #wrongAnswer has referenced the ASCII character codes, which is a numbering system that assigns unique IDs to characters. Take a look at this one for example: http://www.asciitable.com/.
If you take a look at the way the lower case letters are arranged in the table, you'll see that the letter a is at 97, b is at 98, c is at 99 and so on. What the above code is doing is that we are finding what the ASCII code of a is, then adding by 4, then casting back to char. Therefore, what this is actually doing is taking 97 then adding with 4, producing 101. If you consult the table, 101 is actually e, and so the output is e after casting 101 with char.
the ascii value of a is 97 so 97+4=101 101 which is ascii value of alphabets e.
System.out.println((char) ('a' + 4));
so the above sop will print e.
Lets break this down into discrete statements:
char a = 'a'; // the character a
int aPlusFour = a + 4; //characters are really an integer value for the ASCII code, so we can add four
char aPlusFourChar = ((char) aPlusfour); //cast our integer back to a char
System.out.println(aPlusFourChar); //this should print the character e
'a' == 97
97+4 == 101
(char)101 == 'e'
System.out.println('e'+""); // prints 'e'
First a ascii value is added to the value 4. and the character respective to the value 101 is printed..it is 'e'.You can refer ascii table
Related
I want to find an integer representation of a character, then later on find a character representation of an int.
My current solution is this, but it doesn't work:
String s = "A"
Integer b = Character.getNumericValue(s.toCharArray()[0]); // 10
char c = Character.toChars(b)[0]; // (blank)
How should i do this?
There's a misunderstanding in how Character.getNumericValue(char) works.
Returns the int value that the specified Unicode character represents. For example, the character '\u216C' (the roman numeral fifty) will return an int with a value of 50.
The letters A-Z in their uppercase ('\u0041' through '\u005A'), lowercase ('\u0061' through '\u007A'), and full width variant ('\uFF21' through '\uFF3A' and '\uFF41' through '\uFF5A') forms have numeric values from 10 through 35. This is independent of the Unicode specification, which does not assign numeric values to these char values.
from the java docs.
In other words:
The method getNumericValue does not produce the UTF-16 value of the specified character, but attempts to produce a numeric value - in the sense that '0' will produce 0 - from the given value. And 'A' corresponds to 10 in hex.
But 10 is not the UTF16 value corresponding to 'A'.
A correct way of converting a char to it's corresponding UTF16-value would be to use one of the codepoint-methods from Character. Or if you're absolutely positive, all values will be of the BMP, you can use
char c = 'A';
int i = (int) c;
In this case Character.toChars(int) will also work.
int b = s.charAt(0); // 65
char c = (char) b // 'A'
As a Java beginner I'm playing around with a case statement at this point.
I have set: int d = '1'; And with: System.out.println("number is: " + d);, this returns 51.
Now I found out that if I set it as: int d = 1;, it does return 1.
Now my question is why does it return 49 when I set it as '3'? What difference do the ' ' make?
My code that returns 49:
int a = '1';
switch(a)
{
case '1' :
System.out.println("Good");
break;
case '2' :
case '3' :
System.out.println("great");
break;
default :
System.out.println("invalid");
}
System.out.println("value: " + a);
'1' is the character '1', whose integer value is 49. Each character has a numeric value in the range from 0 to 2^16-1. Therefore 1 and '1' are different integer values.
With ' (single quotes) around a character you tell the compiler that the value in between is a character (variable type char in Java). Generally, computers only understand numbers so characters get represented as numbers in memory as well. The value of the character '1' is 49. You want to actually save the value 1 in memory and use that to calculate things. So, you have to tell the compiler that you want this to be interpreted as an actual number and not as a character.
To summarize:
'1' is the character that prints the number 1
1 is the actual number 1
This means 1 and '1' have different integer values.
You can look at the integer value of the most commonly used characters in coding here: ASCII Table
In Java the character values are stored as int internally. That means when you assign a char value using 'A' to an int type, the compiler casts the char value into int and thus the UTF-16 Code unit value is stored into the int variable.
Try this one as well:
int yourInt = 33;
char ch = (char) yourInt;
System.out.println(yourInt);
System.out.println(ch);
1 is interpreted as a character whose value is 49.
Do:
int a = 1;
switch(a) {
case 1:
...
Or, if you go with your current code:
System.out.println("value: " + Integer.parseInt((char)a+'');
It is happens because for some weird reason java treat char type as short.
Your '1' expression is constant expression of type char. It is stored internally as some numeric value. Usually all goes fine with this approach for example System.out.println('1') will print exactly what you expect.
But when you write int a = '1'; you char value is converted to int value because char behave like short int there.
PS: if there was no implicit conversion between char and int (which anyway have no sense) you just got compilation error.
When you declare int d='1'; then 1 is not treated as integer it is a character. It is converted to 49 according its unicode value. similarly character '3' will coverted to 51. Jvm do implicit type casting from character to integer.
you should try this code
char c = '3';
int a ='1';
System.out.println(c);
System.out.println((int)c);
You will get output as
3
51
49
int x=(int)compressedText.charAt(one1+1);
int y=(int)compressedText.charAt(one1+2);
count=x+y;
count1=(char)count;
the craracter value for compressedText.charAt(one1+2) and compressedText.charAt(one1+1) are each equal to 1 but when I try to debug my code it says count is equal to 98.
Casting a char that represents a numeric character to an int doesn't do what you think it does. It takes the Unicode value of the char (which is 49 for '1'). That explains why you get 98 instead of 2.
Because the code values for the characters '0' through '9' are 48 through 57, you can subtract '0' (48) from each char instead, e.g.
int x = compressedText.charAt(one1+1) - '0';
You'll need to undo this conversion if you are converting an int back to a char that is meant to represent the numeric character. Also you'll need to account for multiple characters if count is more than one digit (>= 10).
This method works in C, C++ and Java. I would like to know the science behind it.
The value of a char can be 0-255, where the different characters are mapped to one of these values. The numeric digits are also stored in order '0' through '9', but they're also not typically stored as the first ten char values. That is, the character '0' doesn't have an ASCII value of 0. The char value of 0 is almost always the \0 null character.
Without knowing anything else about ASCII, it's pretty straightforward how subtracting a '0' character from any other numeric character will result in the char value of the original character.
So, it's simple math:
'0' - '0' = 0 // Char value of character 0 minus char value of character 0
// In ASCII, that is equivalent to this:
48 - 48 = 0 // '0' has a value of 48 on ASCII chart
So, similarly, I can do integer math with any of the char numberics...
(('3' - '0') + ('5' - '0') - ('2' - '0')) + '0') = '6'
The difference between 3, 5, or 2 and 0 on the ASCII chart is exactly equal to the face value we typically think of when we see that numeric digit. Subtracting the char '0' from each, adding them together, and then adding a '0' back at the end will give us the char value that represent the char that would be the result of doing that simple math.
The code snippet above emulates 3 + 5 - 2, but in ASCII, it's actually doing this:
((51 - 48) + (53 - 48) - (50 - 48)) + 48) = 54
Because on the ASCII chart:
0 = 48
2 = 50
3 = 51
5 = 53
6 = 54
In C the + and - operators apply integer promotion*1 to their arguments, thus the result of subtracting (or adding) two chars is an int*2.
From the C-Standard:
5.1.2.3 Program execution
[...]
10 EXAMPLE 2 In executing the fragment
char c1, c2;
/* ... */
c1 = c1 + c2;
the ‘‘integer promotions’’ require that the abstract machine promote the value of each variable to int size and then add the two ints [...]
Applying this to the OP's implicitly given use case of
char c = 42;
... = c - `0`;
This would be lead to the above being the same as:
... = (int) c - (int) `0`; /* The second cast is redundant, as per Jens' comment. */
^ ^
+------ int -----+
*1: If the operators arguments have a lower rank than int they are promoted to an int.
*2: char has a lower rank than int.
There's no change going on. '0' is an int in C. It is a fancy way to write 48 (assuming ASCII).
You can convince yourself of this fact by computing the size of '0':
printf ("sizeof '0' is %zu\n", sizeof '0');
printf ("sizeof(char) is %zu\n", sizeof(char));
which in the first line very likely prints 4 (or 2) but probably not 1 like in the second row (again: in C; it's different for C++).
The numeric constant 0, without any qualifications, has type int. The result of the binary subtraction operation on a char and an int also has type int due to the usual type promotion process.
I read that "When an integer is cast into a char, only its lower 16 bits of data are used; the other part is ignored". Based on this shouldn't i get the char value for '0041' as output.Instead i get 'A' as output ,which has an ASCII value of 65. Why does this happen??
public class practice {
public static void main(String[] args) {
char ch = (char)0XAB0041;
System.out.println(ch);
char ch1= (char)65.25;
System.out.println(ch1);
}
}
Will i get the same output if i myself consider only the lower 16 bits for casting.As below:
char ch = (char)0041;
System.out.println(ch);
Guys could anyone clear this problem that i am facing in comprehending the relation between unicode,ASCII and hexadecimal values...
Thanks..
0x0041 is decimal 65 which is ASCII 'A'.
65.25 would be truncated to 65, so it's still 'A'.
What were you expecting?
0XAB0041 in decimal is:
11206721
in binary, it becomes:
101010110000000001000001
So taking last 16 bits,
we have: 0000000001000001=65 in decimal
If you see the ASCII table in this link, it is for 'A'
Hence, 0XAB0041 on casting to char, becomes 'A'
If you consider
char ch = (char)0041;
System.out.println(ch);
0041 is taken by java as octal literal with decimal value 4*8+1=33.So ASCII code for 33 decimal is !.
Hence the output will be:
!
Hence if you ask "Will i get the same output if i myself consider only the lower 16 bits for casting.", your answer is !(not) :)
You are dealing with 0x41 which is 65(16 * 4 + 1) in decimal system. 'A' corresponds to ascii 0x41.