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.

C/C++ FeatureJava equivalentsMore information

No exact equivalent; the sometimes are functionally similar:

new byte[]

Java does not have the direct equivalent of allocating blocks of memory for everyday uses such as storing structs/arrays. Instead, objects and arrays are created using the new operator. See: Java memory management operators.

For certain specific uses, ByteBuffer.allocate() and ByteBuffer.allocateDirect() can be used to reserve areas of memory on and off the heap. But these are not used for general object/array creation.

new() new

The Java new operator is used to create objects and arrays on the heap. See: Java memory management operators.


Garbage collection.

finalize(), in some specific cases.

Java does not really have an equivalent of the C++ delete operator, since it has automatic garbage collection. However, it does have "hooks" to be notified of when objects are garbage collected, for the rare circumstances where this is required. See: Java memory management operators.

const final
Methods such as asReadyOnlyBuffer(), Collections.unmodifiableList()

There are a few const equivalents in Java, depending on the circumstances. In particular, the final keyword sometimes performs the equivalent function.

Pointers Object references

Strictly speaking, Java does not have pointers as far as the programmer is concerned: i.e. Java never exposes an actual memory address to the progammer(*). But Java does allow you to access fields of objects, allocate and access memory buffers, refer to blocks of code in the form of lambdas etc. The Java equivalent of pointers therefore depends on what you would have actually used a pointer for in the C/C++ equivalent.

(*) Java supports native methods, in which it is possible to access the memory address of a DirectByteBuffer.


The Java equivalent to printf and related methods is to use System.out.printf(), String.format() or a Formatter object. However, in some cases where you would have used printf etc in C, you can simply concatenate strings and variables in Java.

unsigned char is essentially an unsigned short; otherwise, no direct equivalent

Unsigned arithmetic in Java can be tricky, because Java primitive data types are generally signed (including byte!). With the exception of the two-byte char, we need to do extra work to achieve the equivalent of unsigned in Java.

What to read next

If you are relatively new to Java and coming from a language such as C/C++, then a key difference to understand is how Java memory management works compared to these languages.

If you enjoy this Java programming article, please share with friends and colleagues. Follow the author on Twitter for the latest news and rants.

Editorial page content written by Neil Coffey. Copyright © Javamex UK 2021. All rights reserved.