Learning Java: What is the point of the Char datatype?
Understanding concept of character [] in java.
What is the difference between char and Character in Java? - Stack Overflow
Reading a single char in Java - Stack Overflow
Apologies as I come from Python, but the Char datatype seems extremely pointless...
I'm just starting to learn Java, so can you shed some light on this?
I am confused as to the meaning behind a character [] in java when we add things to it utilizing ca[c-'a'] ++ syntax. Essentially what is the purpose of -'a' in this context.
char is a primitive type that represents a single 16 bit Unicode character while Character is a wrapper class that allows us to use char primitive concept in OOP-kind of way.
Example for char,
char ch = 'a';
Example of Character,
Character.toUpperCase(ch);
It converts 'a' to 'A'
From the JavaDoc:
The Character class wraps a value of the primitive type char in an object. An object of type Character contains a single field whose type is char. In addition, this class provides several methods for determining a character's category (lowercase letter, digit, etc.) and for converting characters from uppercase to lowercase and vice versa.
Character information is based on the Unicode Standard, version 6.0.0.
So, char is a primitive type while Character is a class. You can use the Character to wrap char from static methods like Character.toUpperCase(char c) to use in a more "OOP way".
I imagine in your program there was an 'OOP' mistake(like init of a Character) rather than char vs Character mistake.
You can either scan an entire line:
Scanner s = new Scanner(System.in);
String str = s.nextLine();
Or you can read a single char, given you know what encoding you're dealing with:
char c = (char) System.in.read();
You can use Scanner like so:
Scanner s= new Scanner(System.in);
char x = s.next().charAt(0);
By using the charAt function you are able to get the value of the first char without using external casting.