Does a Java to C++ converter/tool exist?


I always asked myself if it would be possible to make a Java to C++ converter.

Maybe a tool that converts the Java syntax to the C++ syntax?

I am aware that the languages differ, but simple things like loops where the semantics match 1 to 1.

Is there such a tool? Or is it possible to make one?

1/11/2017 1:47:58 PM

Accepted Answer

It's possible to do anything given enough time, money and resources. Is it practical? Beyond trivial examples not really. Or rather it depends on what constitutes an acceptable error rate.

The real problem is that the idioms are different in Java to C++. Java to C# for example would actually be far easier (because the idioms are much more similar). The biggest of course is that C++ has destructors and manually managed memory. Java uses finally blocks for this kind of behaviour and has garbage collection.

Also Java has a common Object supertype. C++ doesn't.

The generics to templates would be nigh on impossible I would imagine.

6/22/2010 6:43:01 PM

The Firefox HTML5 parser is written in Java and converted to C++. But I think the converter used there is quite specific for this project. Interestingly, it turned out the resulting C++ parser was faster than the old parser written in C++.

I'm also writing a converter as part of the H2 database, under src/tools/org/h2/java. The idea is to allow converting a subset of the H2 database to C++, so this is also not a general purpose translater.

And there is the open source project J2C.

So there are ways to convert Java to C++. But don't expect the translator support all features, and don't expect the resulting code to be any faster than a good Java JVM.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow