Muhammad ADel Muhammad ADel - 25 days ago 6
Java Question

how to use assignment operator with classes instead of new () Like wrapper class in java?

how to use assignment operator with classes instead of new() Like wrapper class

Integer i = 3;

newClass c = 3; // didn't work


newClass c = 3; --> didn't work

I know there are overloading operators in c++ which do this, but how to overload assignment operator in java ?!

Answer
  1. It's not the "equal operator," it's the assignment operator.

  2. No, you can't do that. You must use new (or reflection).

  3. Java doesn't have operator overloading. (The reason you can do Integer i = 3; is because autoboxing of primitives is built into the language, not because the Integer class does something with overloading.)