What is the main difference in object creation between Java and C++?

后端 未结 7 555
感动是毒
感动是毒 2021-01-31 19:12

I\'m preparing for an exam in Java and one of the questions which was on a previous exam was:\"What is the main difference in object creation between Java and C++?\"

I t

7条回答
  •  情歌与酒
    2021-01-31 20:10

    Assuming that c++ uses alloc() when the new call is made, then that might be what they are looking for. (I do not know C++, so here I can be very wrong)

    Java's memory model allocates a chunk of memory when it needs it, and for each new it uses of this pre-allocated area. This means that a new in java is just setting a pointer to a memory segment and moving the free pointer while a new in C++ (granted it uses malloc in the background) will result in a system call.

    This makes objects cheaper to create in Java than languages using malloc; at least when there is no initialization ocuring.

    In short - creating objects in Java is cheap - don't worry about it unless you create loads of them.

提交回复
热议问题