Unicodes & Naming Variables
Introduction
In this unit, we will learn about character unicodes and variable naming conventions to be followed in Java.
Characters and Unicodes
Unicode Ranges
Below are the few most commonly used Unicode values of the characters:
48 - 57 -> Number Digits (0 - 9)
65 - 90 -> Capital Letters (A - Z)
97 - 122 -> Small Letters (a - z)
And the rest of the characters include Special Characters, Other Languages, etc.
Let's see how to get the Unicode value of a character and vice-versa.
Getting Unicode Value of a Character
In Java, we can get the Unicode value of a character using implicit type conversion and explicit type conversion (type casting).
Using Implicit Type Conversion
Using implicit type conversion we can get the Unicode Value of a character by assigning a variable of type char to a variable of type int.
class UnicodeValue {
public static void main(String[] args) {
char ch = 'A';
int unicodeValue = ch;
System.out.println(unicodeValue);
}
}
output:
65
In the above code, when a char value is assigned to an int variable, Java automatically converts the char value to int and returns the Unicode value of the given character(65).
Using Explicit Type Conversion
class UnicodeValue {
public static void main(String[] args) {
char ch = 'A';
System.out.println((int)ch);
}
}
ouput:
65
Getting Character Representation of a Unicode Value
To find the character representation of a Unicode value, we have to explicitly typecast the int value to char. The explicit type conversions between int and char occur based on the Unicode value.
class CharacterValue {
public static void main(String[] args) {
int unicodeValue = 65;
char ch = (char)unicodeValue;
System.out.println(ch);
}
}
output:
A
Printing Characters using Unicode Values
The below code will print the characters from A to Z
class Alphabets {
public static void main(String[] args) {
for (int unicodeValue = 65; unicodeValue < 91; unicodeValue = unicodeValue + 1) {
System.out.println((char)unicodeValue);
}
}
}
output:
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
Naming Variables
Use only the below characters
Capital Letters ( A – Z )
Small Letters ( a – z )
Digits ( 0 – 9 )
Underscore(_), this is used only for variables storing a constant value (ex - variables declared with the final keyword)
Below characters cannot be used
Blanks ( )
Commas ( , )
Special Characters
( ~ ! @ # % ^ . ?, etc. )
Variable names must begin with small letters, except for the variables storing a constant value (ex - variables declared using the final keyword).
Cannot use keywords, which are reserved for a special meaning.
int
if, etc
In addition to this, programmers generally use camel case (Eg: canLearn, ageLimit, etc) notation for naming variables in Java.
Keywords
Keywords are words that are reserved for a special meaning. We cannot use this keywords as variable names.
Summary
- Every character has a Unicode value.
- The implicit and explicit type conversions between int and char occur based on the Unicode value.
- Variable names cannot use keywords.
- The camel case is preferred for naming the variables in Java.