Home  Memory management  Java equivalent to...  const  new  delete  malloc

Threads Database Profiling Regular expressions Random numbers Compression Exceptions C Equivalents in Java

Java equivalents of C/C++ features

This section looks at how to achieve certain common C/C++ functions or keywords in Java. The articles below generally deal with subtle differences between the two languages, or features that C/C++ tend to "miss" when they migrate to Java.

memory management operators

This section effectively covers Java equivalents malloc() and free(), new() and delete(). It looks at how memory management works in Java vs C/C++ and answers questions such as the following:

  • how does the Java runtime allocate memory for objects?
  • how do you allocate and manipulate a "raw block of memory" in Java, such as you might with C's malloc() function?
  • how do you allocate memory on the stack in Java?
  • how do you deallocate an object in Java?
  • is finalization the same thing as deallocation?
  • how can I pass a pointer to a value in Java?

The discussion moves on to look at related issues such as the details of garbage collection and finalization in Java, and alternatives to the latter.

const

A detailed look at the C/C++ const operator, and its Java equivalents where they exist. We see that as a more "object oriented" language, direct equivalents are not always available, as Java requries field access to be controlled by the defining class.

unsigned

Generally, integer primitives are treated as signed values in Java, with the exception of the two-byte char. This article looks at how to achieve the equivalent in Java of the C/C++ unsigned modifier, which signals to the compiler that unsigned arithmetic should be used on a given variable. We see that with a little care, it is possible to achieve the equivalent of unsigned in Java. The tricky case of 64-bit unsigned in arithmetic in Java sometimes needs some "manual" treatment.

New suggestions are always welcome for this section? Are you a C/C++ programmer who misses a particular feature in Java? If so, why not leave a suggestion on the Javamex blog?


Written by Neil Coffey. Copyright © Javamex UK 2008. All rights reserved.