Question is: What gets displayed in console?
And I really have some problems with understanding.
Here is the code:
public static void felda(){
char[] felda = {'2',0x31,48};
for (int i = 0; i< felda.length; i++){
System.out.println(" : " + felda[i]);
}
System.out.println();
}
public static void feldb(){
int[] feldb = {'2',0x31,48};
for (int i = 0; i< feldb.length; i++){
System.out.println(" : " + feldb[i]);
}
System.out.println();
}
public static void feldc(){
int [] feldc = {'2',0x31,48};
for (int i = 0; i< feldc.length; i++){
System.out.println(" : " + (char) feldc[i]);
}
System.out.println();
}
So if I run in the Solution is:
: 2
: 1
: 0
: 50
: 49
: 48
: 2
: 1
: 0
So I don't understand how it is even possible to have an int definded with ' '.
And I find it very confusing how int feldb = '2' results in being 50 and int feldb=0x31 results in being 49.. dam this is all so confusing. I hope someone can enlighten me.
Edit: Why is char feldc = 48; resulting in being 0?
In Java, a char represents a Unicode character. But it's also in fact an unsigned integer, on 2 bytes, which can go from 0 to 216 - 1.
So,
char c = '2';
initializes c with the character '2'. And the numeric value of the character '2', in Unicode, is 50.
So, if you print it as a character, '2' will be printed. If you print it as a numeric value (as an int, using int c = '2'), 50 will be printed.
When doing
char feldc = 48;
you initialize feldc with the character whose numeric Unicode value is 48, and that character is the character '0'. It's thus equivalent to
char feldc = '0';
0x31 is a number written as an hexadecimal literal (that's what the 0xprefix means). When you write 31, the value is in decimal. It's equal to 1 * 100 + 3 * 101.
In hexadecimal, the base is 16 rather than 10. So 0x31 is equal to 1 * 160 + 3 * 161, which is equal to 49.
50 is the ASCII value of the '2' character. Defined like that its not the number 2.. its giving the ASCII value of a character. See this ASCII table and find the '2' char
http://ascii.cl/index.htm?content=mobile
Related
I am Confused as to why this returns 1;
(char)('0' + 11) = ; why?
Full code below where ending = 1;
char[] ending;
char a = (char)('0' + 11/10);
ending = new char[]{a, (char)('0' + 11)};
System.out.println(ending);
Char value of '0' is 48.
48 + 11 = 59
Char value of 59 is ';'.
You can check char values in integer value in any ASCII Character Set in internet.
In Java, char can be use as a int, short, byte, long with values between 0 and 65535 without any casting.
A better explanation is found in: Java char is also an int?
You are assigning '1' to variable a. '0' + 11/10 => '0' + 1
You are assigning a two letter string to endings. Le first letter is a ('1') the second is a semi colon. ('0' + 11).
I have a bit of confusion regarding casting from int to char data type, this is what i have;
int k = 3;
System.out.println((char)k + " " + k)
The output should have been
3 3
yet, i got this instead
3
Could somebody explains to me why is this happening?
The char '3' doesn't have a numeric value of 3. Its numeric value is 51.
This will print 3:
int k = 51;
System.out.println((char)k);
The char having a numeric value of 3 is an invisible character.
If you want to convert a single digit int to the corresponding char, you can write
int k = 3;
char three = (char)('0' + k);
System.out.println(three);
In addition to the other answers: If you know for sure that 0 <= k <= 9, you can use
System.out.println((char)(k + '0'));
to print the 'charified' version of your integer.
If k < 0 or k > 9, there isn't a single char (character) describing it. In that case, you'll have to use a string, which is basically an array of chars:
System.out.println(Integer.toString(k));
ASCII value of (char) 3 is : End of text
If you want to get the numeric value you need to use numeric value 51 which is the ASCII value of 3:
int k = 51;
System.out.println((char) k + " " + k);
This gives output :
3 51
Complete ASCII table can be found here
Can someone explain to me why the following code prints the char 'u' ?
int p = 9;
int q = 5;
int r = p - q;
double x = p;
double y = q;
String s = "Question";
System.out.println ((char)(s.charAt(r) + 1));
1) p-q = 4
2) character at index 4 is t (s.chartAt(4) gives character at index 4 in string s).
3) you have added 1 to it so 1 was added to its ASCII value making the ASCII value equal to ASCII value of u.
4) then the integer was cast to char which will be 'u', which was printed.
Because s.charAt(r) = 't' and 't' as int is 116. 116 + 1 is 117 and 117 as char is u;
If you split up the oneliner it might be more clear:
char charAtR = s.charAt(r);
int plusOne = charAtR + 1; // char will be converted to int
char toPrint = (char) plusOne;
System.out.println (toPrint);
In System.out.println() you receive character 't' from string "Question" and output next codePoint that is 'u'.
You take char at position 4 (since r is p - q, which is 4) in string s, and its t. In s.charAt('t') you take its char value (ASCII code), which is 116, and you add 1 to it, so you will 117, which is ASCII code of u and you turn it in char, so it will print u.
For example, if you do this:
String s = "Question";
System.out.println((int)s.charAt(r));
It will print 116. If you do this:
String s = "Question";
System.out.println((char) 177);
It prints u.
If you're wondering why is t at position 4, and not s, thats because charAt works from 0, not from 1.
The value of r is 4. Therefore,
s.charAt(r); = s.charAt(4); = ELEMENT AT 4th INDEX OF "Question" = 't'
When an integer is added to a char (eg. 'A'+1) or the post increment operator is used with a char (eg. char_variable++), the ASCII value of the char is incremented.
So,
s.charAt(r)+1 = (char)((ASCII value of 't')+1) = (char)(116+1) = 'u'
Since 117 is the ASCII value of 'u', the program displays 'u'.
I was guessing an output 1 for this code but instead of that i am getting output 49,
The code is
public static void main(String[] args) {
String str = "1+21";
int pos = -1;
int c;
c = (++pos < str.length()) ? str.charAt(pos) : -1;
System.out.println(c);
}
The result of someCondition ? a : b is the common type for a and b. In this case, the common type of str.charAt(pos) (a char) and -1 (an int) is int. That means your str.charAt(pos) value is being cast to an int -- basically, being converted to its unicode code point, which in this case is the same as its ASCII value.
49 is the code point for the character '1'.
If you're trying to get c to be the digit '1', the easiest thing to do is to subtract the code point for '0':
c = (++pos < str.length()) ? (str.charAt(pos) - '0') : -1;
This works because all of the numbers are sequential in unicode, starting with '0'. By subtracting the value of the char '0' from these -- that is, the int 48 -- you get the value you want:
'0' = 48 - 48 = 0
'1' = 49 - 48 = 1
...
'9' = 57 - 48 = 9
charAt method return the char value of the position that you pass. Here you are assigning that to a int variable. So that means you are getting the integer representation of particular char value.
In your case
int c = "1+21".charAt(0); -> actual char is 1 and the ASCII of that is 49
Here is a short program that counts the letters of any given word entered by the user.
I'm trying to figure out what the following lines actually do in this program:
counts[s.charAt(i) - 'a']++; // I don't understand what the - 'a' is doing
System.out.println((char)('a' + i) // I don't get what the 'a' + i actually does.
import java.util.Scanner;
public class Listing9_3 {
public static void main(String[] args) {
//Create a scanner
Scanner input = new Scanner (System.in);
System.out.println("Enter a word to find out the occurences of each letter: ");
String s = input.nextLine();
//Invoke the count Letters Method to count each letter
int[] counts = countLetters(s.toLowerCase());
//Display results
for(int i = 0; i< counts.length; i++){
if(counts[i] != 0)
System.out.println((char)('a' + i) + " appears " +
counts[i] + ((counts[i] == 1)? " time" : " times"));
***//I don't understand what the 'a' + i is doing
}
}
public static int[] countLetters(String s) {
int[] counts = new int [26]; // 26 letters in the alphabet
for(int i = 0; i < s.length(); i++){
if(Character.isLetter(s.charAt(i)))
counts[s.charAt(i) - 'a']++;
***// I don't understand what the - 'a' is doin
}
return counts;
}
}
Characters are a kind of integer in Java; the integer is a number associated with the character on the Unicode chart. Thus, 'a' is actually the integer 97; 'b' is 98, and so on in sequence up through 'z'. So s.charAt(i) returns a character; assuming that it is a lower-case letter in the English alphabet, subtracting 'a' from it gives the result 0 for 'a', 1 for 'b', 2 for 'c', and so on.
You can see the first 4096 characters of the Unicode chart at http://en.wikibooks.org/wiki/Unicode/Character_reference/0000-0FFF (and there will be references to other pages of the chart as well). You'll see 'a' there as U+0061 (which is hex, = 97 decimal).
Because you want your array to contains only the count of each letter from 'a' to 'z'.
So to index correctly each count of the letter within the array you would need a mapping letter -> index with 'a' -> 0, 'b' -> 1 to 'z' -> 25.
Each character is represented by a integer value on 16 bits (so from 0 to 65,535). You're only interested from the letters 'a' to 'z', which have respectively the values 97 and 122.
How would you get the mapping?
This can be done using the trick s.charAt(i) - 'a'.
This will ensure that the value returned by this operation is between 0 and 25 because you know that s.charAt(i) will return a character between 'a' and 'z' (you're converting the input of the user in lower case and using Character.isLetter)
Hence you got the desired mapping to count the occurences of each letter in the word.
On the other hand, (char)('a' + i) does the reverse operation. i varies from 0 to 25 and you respectively got the letters from 'a' to 'z'. You just need to cast the result of the addition to char otherwise you would see its unicode value be printed.
counts[s.charAt(i) - 'a']++; // I don't understand what the - 'a' is doing
assume charAT(i) is 'z'
now z-a will be equal to 25 (subtract the unicode / ASCII values).
so counts[25]=counts[25]+1; // just keeps track of count of each character