Search code examples
javac++placement-new

what is placement new operator in C++ and does java has it?


I heard about placement new operator of C++. I am confused what it is. However, I can see where it can be used under a question in stackoverflow. I am also confused whether we have this in java or not. So my question is very precise: What is placement new operator and do we have something like it in java?

Note please, don't be confused with other questions on stackoverflow: they are not duplicate of this question.


Solution

  • The following article explains the meaning of placement new in C++: http://www.glenmccl.com/nd_cmp.htm

    This term itself is relevant for overloaded new statement. Since Java does not allow to overload operators at all and specifically new operator the placement new is irrelevant for Java.

    But you have several alternatives.

    1. Using factory or builder pattern
    2. Using wrapper/decorator pattern (probably together with factory) that allows changin some class functionality by wrapping its methods.
    3. Aspect oriented programming. It works almost like decorator pattern but can be implemented using byte code modifiction.
    4. Class loader interception