In Java, I can access a public member of a class using .
as can be seen in the second line of the main method in the following example (for the sake of this example, ignore my poor use of encapsulation).
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;
}
}
Is the .
considered an operator in the Java programming language, just as *
, ~
, and !=
are considered operators?
Edit - Extending the above example:
As has been pointed out, it seems that the Java language specification considers .
to be a separator and not an operator. Yet, I would like to point out that .
exhibits some behavior that does seem rather operator-ish. Consider the above example extended 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;
}
}
It is clear that some precedence is being enforced such that the member access is evaluated before the addition. This seems intuitive because if the addition were to be evaluated first, then we would have p.2
which is nonsense. Nevertheless, it is clear that .
is exhibiting behavior that the other separators don't.
The member access operators . and -> are used to refer to members of struct , union , and class types. Member access expressions have the value and type of the selected member.
The not operator is a logical operator, represented in Java by the ! symbol. It's a unary operator that takes a boolean value as its operand. The not operator works by inverting (or negating) the value of its operand.
It's considered a separator, not an operator. See the Java Language Specification sections 3.11 and 3.12 for a list of all separators and operators.
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