2

I'm looking for a program or web service that can convert Java code into C++ code (preferably some human-readable C++ code). Any license, OS or price is fine.

The same question was posted by n00ki3 on SO but it was closed as "We don’t allow questions seeking recommendations for books, tools, software libraries, and more".

3
  • The question was closed, yes, but it still has a lot of answers, have you looked into them?
    – kutschkem
    Jun 21, 2021 at 7:43
  • 1
    Answers to this question are being discussed on Meta Dec 30, 2021 at 12:49
  • 1
    Like already mentioned something like tis is not 100% posible, even developer of such converters say it. But for some specific code such converter could work. Here 1 such converter: github.com/arnetheduck/j2c
    – convert
    Jan 12, 2022 at 22:22

3 Answers 3

1
  • Firstly, when it comes to conversion from language 'X' to language 'Y', then no program can beat human intentions/efforts.

  • For the above case Java to C++ conversion can be done using programs such as TanglibleSoftware. But for some language constructs which are only supported in Java but not in C++, some work around is surely needed.

  • Consider case: User 'A' hands over his source code to program 'ABCD' for conversion from language 'X' to 'Y', and he assumes to get the result(exact converted code with same behavior) in bare minimal efforts, then user 'A' is calling for a failure.

  • I will not go into syntactic differences between Java & C++, but if one has a good grasp on programming then for unsupported constructs of language 'X', code can always be written to perform those operations in language 'Y' successfully.

Machines can only understand code, but meaning has to be construed by Humans.

But this may not be constrained if we consider artificial intelligence into this context or some 'xyz' technology in near future.

My answer is based on my experience, I am not affiliated to TanglibleSoftware by any means, the above software worked for me, with some efforts made. It might not have worked for some other users.

0
0

Something like this is not really posible, since there is concepts and keywords in Java without equivalents in C++. For example synchronized, package or instanceof can´t be translated from Java to C++. Even final has not always the same meaning as const.

4
  • Synchronized = use a synchronization object such as a Mutex? Dec 30, 2021 at 12:00
  • No, monitor is used.
    – convert
    Dec 30, 2021 at 15:42
  • You didn't get the point: whatever synchronized does, it could be emulated using a Mutex, Semaphore, Critical Section or whatever is available in C++ Dec 30, 2021 at 16:24
  • Yes emulating that behavior should be posible, but still not 100% the same.
    – convert
    Dec 30, 2021 at 17:52
-1

Java and C++ are too different for that to be possible:

  • Java (and the JVM) requires a garbage collector. Which deals with circular references.

  • C++ requires manual memory management. Even smart pointers (with reference counters) won't usually deal easily with circular references.

  • C++ wants the rule of five to be respected. That rule has no simple equivalent in Java (or the JVM).

  • Java object model requires a single class for every object, and the class is a Java object. A C++ class is not a C++ object. C++ allows multiple-inheritance.

  • Java is multi-threaded. C++ knows thread only since C++11.

  • Java requires a dynamic class loader (so the code can be extended at runtime). In standard C++ the code is defined at compile time. POSIX has dlopen and dlsym to load more code. See also the C++ dlopen minihowto on Linux.

  • Java is often garbage collecting unreachable code. This is not easily feasible in standard C++.

Some experimental JVM implementations may be improved to generate (compile to) unreadable C code.

On many OSes (including Linux) you could use inter-process communication techniques between your JVM process and some process running C++ code.

Many JVM implementations are coded in C++.

3
  • 1
    I don't see the point with the Garbage collector. Java could run the Garbage collector at the end of each scope as well. For threads: so, let's use C++ 11, OP did not make restrictions on the version, so why not use C++ 17? Windows and Linux have had threads for much longer, also in C++. Also, in C++ you can load DLLs. No need to do stuff at compile time. Dec 30, 2021 at 12:05
  • Loading DLL is outside of the C++ standard. Dec 30, 2021 at 12:46
  • That's true, but why shouldn't a Java to C++ converter provide such a solution. Doesn't need to be a DLL, could be something they made up themselves which works on Windows and Linux. Dec 30, 2021 at 12:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.