Why can two variables have the same name?

ziMtyth :

I execute the following code and I get no errors, and in the output I see the Success! message. Can you please explain this strange behaviour.

public class Main {

    public static void main(String[] args) {
        int р = 0;
        int p = 1;
        if(р == 0 && p == 1) {
            System.out.println("Success!");
        }

    }

You can check the online demo

Saravana :

both are different variables (but looks similar), you can see the UTF-16 is different

    int р = 0;
    int p = 1;
    if (р == 0 && p == 1) {
        System.out.println("Success!");
        System.out.println("p UTF-16 is " + (int) 'p');
        System.out.println("р UTF-16 is " + (int) 'р');
    }

output

Success!
p UTF-16 is 112
р UTF-16 is 1088

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=475235&siteId=1