Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between operator and keyword in Java

My understanding so far in Java has been that operators are things like:

+,-,*,/,%,<,<=,== and so on...

And keywords/reserved words are things like:

boolean,char,int,do,float,double,new et cetera.

However, reading through a Java book and an online tutorial I see that new is referred to as an operator? But how, I thought operators were specifically assigned to arithmetic types? which as I see are only 28. Why is the new keyword also referred to as an operator?

Thank you

like image 388
Foo Fighter Avatar asked Oct 16 '15 17:10

Foo Fighter


People also ask

Are operators keywords?

Operators are keywords that can take one or more arguments. They are usually associated with the standard mathematical operations, but for example new is considered a single argument operator as well.

What is the difference between and operator in Java?

Differences between | and || operators in Java| is a bitwise operator and compares each operands bitwise. It is a binary OR Operator and copies a bit to the result it exists in either operands. (A | B) will give 61 which is 0011 1101. Whereas || is a logical OR operator and operates on boolean operands.

What is difference between operator and method?

Many operators can be mapped by the compiler to simple machine language operations, so the compiler does this directly. Methods, on the other hand, are usually compiled to a call to the machine language translation of your code for that method, unless the compiler has inlined the method.

What is the difference between & operator and the && operator?

& is a bitwise operator and compares each operand bitwise. It is a binary AND Operator and copies a bit to the result if it exists in both operands. (A & B) will give 12 which is 0000 1100. Whereas && is a logical AND operator and operates on boolean operands.


1 Answers

Quoting the excellent tutorial from Oracle (emphasis mine):

Instantiation: The new keyword is a Java operator that creates the object.

Nobody ever said that you can only operate on primitive types in the first place. You operate on objects; and yes there are some numerical operators which are restricted to primitive types. Edit: examples for other operators in Java that work on non-primitives would be instanceof and + (for String concatenation).

Also, step back for a second and consider languages like Scala that do not distinguish between primitive and "reference" types. In Scala, everything is an object. And operators are just syntactical sugar around methods ... operating on those objects.

like image 124
GhostCat Avatar answered Sep 29 '22 20:09

GhostCat