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
Said in simple terms, if multiple variables in a program have the same name, then there are fixed guidelines that the compiler follows as to which variable to pick for the execution.
It is legal for 2 variables in different scope to have same name.
We can declare two variables or member functions that have the same name within the same scope using namespace . This will cause several functions to have the same name and we can access all the functions from anywhere in the program by referencing the name of the namespace.
You can not have 2 variables at the same address, the standard specifically mandates against this. You can not change the address of an object once it has been assign by the compiler.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With