For the past month, I have been working on a server side web service in Java. Although I have used Java in numerous courses throughout college and grad school, this is the first time I have programmed in it professionally.
Compare to C++, Java is a much cleaner language. Here’s a few things I enjoy so far.
- Importing and exporting libraries is fairly trivial (no declspec and name mangling issues).
- A memory model that provides visibility and ordering guarantees (unlike C++ volatile)
- Safe casting without worrying about memory layout (no memory alignment or padding).
- Garbage collection allows for slightly sloppier exit sequence. (vs. memory corruption)
In general, difficult things I deal with in C++ are still difficult in Java (such as concurrency). But Java’s cleaner design provides an invisible guiding hands to prevent me from shooting my foot.
Deep copying in Java is an unpleasant experience. Java does not provide a default copy constructor, hence all copy constructor must be hand-crafted. The clone interface counter-intuitively provides a shallow copy, and is avoided by best practices.
Over the years with C++, I have formed a habit of cleanly separating business logic and data. Under such a setup, data is coded in form of POD, the default copy constructor (and = operator) provides deep copying for free. In general, deep copying of POD is a trivial operation, and generally has no-throw guarantee.
Unfortunately, Java does not have a well defined concept of POD. In fact, even the simplest object in Java, java.lang.Object, is non-trivial. Each Object is associated with a monitor object, and deep copying a monitor object under a multi-threaded environment is almost never desirable. Since the internal monitor object is not exposed in the Object interface, you can never truly perform a deep copy of any Java objects.
And without an automated POD copy constructors, the copy-and-swap idiom does not transfer as nicely in Java.
If the lack of deep copy is the only thing I can complain about Java, it is not a bad experience so far.