It’s a ‘. ” Member access is considered an operator in Java?

In Java, I can access public members of a class You can see in the second line of the main method of the following example (for this example, ignore the encapsulation I can't use)

public class Test {
    public static void main(String[] args) {
        Position p = new Position(0,0);
        int a = p.x; // example of member access
    }
}

class Position {
    public int x;
    public int y;

    public Position(int x,int y) {
        this.x = x;
        this.y = y;
    }
}

It's a It is considered an operator in the Java programming language, such as *, ~ and= Considered an operator?

Edit – extend the above example:

As mentioned earlier, the Java language specification seems to think As a delimiter, not an operator However, I would like to point out Show some seemingly quite operational behavior Consider extending the above example to the following:

public class Test {
    public static void main(String[] args) {
        Position p = new Position(0,0);
        int a = p . x; // a -> 0
        int x = 1;
        int b = p . x + x; // b -> 1
    }
}

class Position {
    public int x;
    public int y;

    public Position(int x,int y) {
        this.x = x;
        this.y = y;
    }
}

Obviously, some priorities are being implemented to evaluate member access before adding This seems intuitive, because if we want to evaluate addition first, then we will have p.2, which is nonsense However, it is obvious Show behavior that other separators do not

Solution

It is considered a delimiter, not an operator For a list of all separators and operators, see Java language specification sections 3.11 and 3.12

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>