3

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?

4 Answers4

9

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.

Mark B
  • 91,641
  • 10
  • 102
  • 179
  • It's §2.11/1. §2.12/1 lists `new` as an operator. – Christian Hackl Feb 08 '17 at 17:36
  • But C++ also allows symbol based operators to be overloaded. So the difference is a symbol based operator versus a name based operator. – rcgldr Feb 08 '17 at 17:40
  • @ChristianHackl, `new` is confusing in that it's an overloadable operator as well as part of the language. To see what I mean, check out http://en.cppreference.com/w/cpp/memory/new/operator_new vs. http://en.cppreference.com/w/cpp/language/new – chris Feb 08 '17 at 17:42
  • @chris: Yet, I just mentioned the correct sources. At least in the N4296 draft. Did the numbers change? – Christian Hackl Feb 08 '17 at 17:48
  • @Christian Hackl added a reference to the standard document I was referencing. – Mark B Feb 08 '17 at 17:48
  • @MarkB: That draft is from 2012. The paragraph numbers did indeed change. [lex.key] is now §2.11, and [lex.operators] §2.12. – Christian Hackl Feb 08 '17 at 17:52
5

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.

Nicol Bolas
  • 378,677
  • 53
  • 635
  • 829
3

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.

keywords 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).

operators 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.

Xirema
  • 18,577
  • 4
  • 26
  • 60
  • If Java considers `new` an operator, why is it absent from the [table of operators](http://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.12)? – meriton Feb 10 '17 at 20:59
  • @meriton Oracle, for whatever reason, seems to have a narrower definition of the word 'operator' than C++, and thus excludes it. This is one of those "a tomato is not a vegetable" arguments, where in a strictly technical sense, no, `new` is not an operator as defined by the Java programming language, but if you go around saying "`new` is not an operator", you're actually spreading more noise than signal. For all *practical* purposes, `new` is an operator, and my advice to Oracle is they consider revising that section. – Xirema Feb 10 '17 at 21:04
1

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.

Adilson Cabral
  • 381
  • 3
  • 10