I am just confused between operator
vs keywords
. I know this doesn't matter for programming, but I want to know the difference.
In Java new
is called as keyword. But in c++ new
is called as an operator, Why?
I am just confused between operator
vs keywords
. I know this doesn't matter for programming, but I want to know the difference.
In Java new
is called as keyword. But in c++ new
is called as an operator, Why?
new
is a keyword in C++ (see the table in C++11 N3337 2.12/1). It then uses operator new
as its implementation, a side effect of which allows programmers to create their own overloaded operator new
to inject custom memory management.
Keywords are grammatical constructs. They are specific sequences of characters that the parser inherently recognizes, which cannot be used as identifiers. if
is a keyword. So is new
, even in C++.
The term "operator" is used to identify things which are used as parts of expressions. +
is an operator; it is acts on two expressions and creates a value from them. new
is also an operator; it takes parameters and a typename+constructor parameters, and performs various operations on them, resulting in the creation of a new object of that type.
But if
is not an operator. Oh sure, an if
statement involves expressions, but if
itself is not part of an expression.
Your specific example is a bit misleading. new
is both a keyword and an operator, in both C++ and Java. These terms also aren't strictly defined: they're contextually defined.
keyword
s are any tokens that appear in a program that are defined by the compiler (though custom keywords can be set by the user by tweaking the compiler, if the compiler allows it). Something like typedef
in C/C++ is a keyword, and depending on the language, fundamental types can be treated as keywords as well. The usual signifier of a keyword is that the compiler won't let you name variables or functions after them (though there are exceptions and caveats to that rule).
operator
s are any action performed on your data or objects in code. The most basic are the arithmetic operators associated with the fundamental data types, like + - * / % = == || &&
and so on. new
(and, in C++, delete
) are also operators, because they perform specific tasks on the objects they target (specifically, they allocate and delete heap memory). The .
(dot) is an operator, and there's even been a few proposals in C++ to allow the user to overload the dot-operator!
A lot of this is a distinction without difference though. It's way more useful to know what new
and new[]
do than to know whether they are strictly classified as operators or not (though to be clear, they are), or whether all operators are keywords or not (I think they are, but I'm not sure) or whether all keywords are operators (I don't believe they are). If you find a case study where those distinctions matter, I'd love to know of them.
In my understanding keywords are reserved word of the language, while operators are "implemented language functions".
It happens that some keywords are also operators.
instanceof
is another example of keyword that is also an operator.
Other example are the arithmetic operators, +
-
*
%
are operators too but not keywords.
I believe it applies to most languages, Java, C++ and others.