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