Reputation:
I have these codes with the same variable names but the only difference is one type is full-width and the other is half-width. I was shocked that Java supports full-width characters as variable names. I was wondering how can Java differentiate the two characters.
public class Main {
public static void main(String[] args) {
int[] x = { 1, 2, 3 };
int[] x = { 1, 2, 3 };
int[] y = { 4, 5, 6 };
int[] y = { 4, 5, 6 };
int[] z = new int[x.length];
int[] z = new int[x.length];
for (int i = 0; i < z.length; i++) {
z[i] = x[i] + y[i];
z[i] = x[i] + y[i];
}
System.out.println(z);
System.out.println(z);
}
}
Upvotes: 3
Views: 550
Reputation: 20913
From The Java Tutorials trail Learning the Java Language, lesson Language Basics, topic Variables
Variable names are case-sensitive. A variable's name can be any legal identifier — an unlimited-length sequence of Unicode letters and digits, beginning with a letter, the dollar sign "$", or the underscore character "_".
So in java code, you can even write variable names in Klingon
Upvotes: 2
Reputation: 14698
Java supports Unicode in source code, so x
(u'\uff58'
) is much more different than x
(u'x'
)
Essentially different characters, same as having int[] a
& int[] b
But for readability, this is a huge problem. Even though two different variables, it is very easy for another user to confuse these names. It is better stick to the basic Latin letters in their simple forms as much as possible within the source code.
Upvotes: 4
Reputation: 4065
I guess that your source code is encoded in UTF-8 (or some other "extended" character set) so the codes for x and x are different (the machine does not care about the graphical representation of the character but the internal code).
Upvotes: 1