简体   繁体   中英

How does java distinguish that the character is full-width or half-width?

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);
    }
}

Java supports Unicode in source code, so ( u'\x' ) 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.

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

我猜您的源代码是用UTF-8(或其他“扩展”字符集)编码的,因此xx和x的代码是不同的(机器不在乎字符的图形表示,而在乎内部代码)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM