Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I know one difference:
Java is write once run anywhere.
C++ is write once compile anywhere.

But are there other differences?
Is there something i can make in cross platform Java, but not in cross platform C++?
Posted
Updated 23-Mar-15 9:13am
v2
Comments
Sergey Alexandrovich Kryukov 23-Mar-15 15:21pm    
The question makes no sense at all. You cannot define what is "difference".
Apple and cat, what's the difference? This is a talk about nothing, idling.
"Something I can make..." What is that, "cross-platform C++"? It is not cross-platform in the same sense as Java, you have to recompile code, and it all depends on the code.
—SA
Ziya1995 23-Mar-15 15:24pm    
Now i wanna make a choice, compiling anywhere is no problem for me.
Should i choose Java or C++?
It makes sense, i hope you will help.
I mean only cross platform C++.

We write once code in Java and run it on any platform.
We write once code in C++ and compile it on any platform.
Is it all the difference?
ZurdoDev 23-Mar-15 15:26pm    
Depends on what you want to do and how much you already know about each one.
Ziya1995 23-Mar-15 15:28pm    
Explain me, i feel good at compiling an app separately.
Can i make the same stuff on both of them?
I wanna make cross platform games on C++ once and compile anywhere.
I wanna have an easy language, not hard.

Help me, i am really confused, why cross platform Java is the most popular, when you can write once and run anywhere while being native, i can't understand.

What is easier?
One user said if i wanna make apps i need to use managed languages like Java or C#.
But in actually i can write a cross platform app in C++ and compile anywhere just by click and that is what confuses me!

Cross platform Java vs cross platform C++:
Are they of the same power?
Sergey Alexandrovich Kryukov 23-Mar-15 15:42pm    
Again, makes no sense.
"Same power"? The concept of "language power" does not make the fully ordered set of languages.
Are you looking something "easier"? Then I have a good advice for you: give up this profession. There too many those easy workers already, they just spoil work of others and each bread they don't earn...
—SA

Please see my last comment to the question, the one saying "I think I understand the confusion".

To understand things, you need to understand all the operation from CPU to the application. You don't, so it's hard to explain, needs time and patience.

How do you think the applications work with OS? C++ and all languages addressing native platforms, compile to native code, writing native CPI instructions. This code is not exactly the same as the code when it is executed by the process when an application is started. The code is created by a compiler and uses relative addressing and referenced to other parts of code, which are prescribed in the form of names (strings) in the binary obj or lib file. Those files are put together in one executable code (DLL or EXE, it does not matter, this is just the PE file) by the linker. The linker takes the symbolic names and links them by addresses. Those addresses are still relative, only the symbols are eliminated. Then the third player comes up, the loader, the least known and most sophisticated part. It actually creates code in memory. Each process gets its separate virtual address space using virtual memory. The code blocks, all calls, objects are shifted in sync with addresses referencing them, to fit in the memory the OS provides, code and data descriptors are set up. The problem here: where are the OS calls? Get back to the stage between compilation and linkage: they are the symbolic names of OS DLLs (shared objects, or something else) and function names. This is all resolved in the PE file.

Now, all OS are different, so the compiled code is different. Not just in names: OS calls do not have one-to-one correspondence. Nothing is cross-platform, except common standard library things, such as console and file I/O. It's not "cross-platform", it's portable. It means that there are libraries that bind OS with those standard portable calls. The source code is the same, but the libraries are different on implementation side. Other aspects of code, notably, UI, is not portable. It's a lot more than UI: serial and parallel I/O, synchronization primitives, system-specific facilities, a lot of things. Note that the same library has separate version of every platform and every CPU instruction set architecture. Say, Windows for x86 is one thing, Windows for x86-64 is another one, and Windows for IE-64 is different, so same goes for the C++ libraries for these platforms — they are all different version. And there are a lot more platforms, non-Microsoft once.

How more complex C++ application can be made portable? With UI and other features? There is only one way: only if they come with the libraries playing the role of OS and hardware abstraction layers. One example is QT: http://en.wikipedia.org/wiki/Qt_%28software%29[^], https://qt-project.org/[^]. On this example, how the same QT application can work on different platforms? First of all, it cannot: first, it needs to be re-build for different platform. Each platform supposedly has its one version of QT, which should be installed on development machine, only them you can build it. But when your solution is built, you need to deploy it with the application to the platform supported by one or another QT version. If such version does not exist, your application won't work. If the user, when asked for confirmation, says "What is that QT? Who knows? I don't want to install it", your application won't work. Is it portable? Yes, potentially? Is it "cross-platform"? Ha-ha, only in the sense I just explained.

With Java, things are different. The role of such OS and hardware abstraction is played by its virtual machine, JVM. This is not a library. This is a whole platform working on top of native OS. There are JVM implementations for different OS. They have to surfaces: the backs ends face native OS, so they are all different, in the same sense as the C++ libraries I mentioned above. On the top, front-end size, it faces Java application, and this interface is unified. Java does not compile in CPU instructions, it compiles to the "fictional" instructions of the virtual machine, byte-code. The byte code is compiled on the fly using Just-In-Time compilation (JIT). The virtual machine translates byte-code instruction into native code and executes it.

With .NET, the OS and hardware abstraction part is the .NET Framework implementing CLR. .NET applications are also compiled kind of byte-code, CIL code, JIT compilation is used, but the compiled CIL code is packed into regular PE files, but the files are special, then can only work if CLR is installed. There are CLR implementation for different OS, such as Mono. The same PE file can be used for different CPU architectures and different platforms, no recompilation is done, but there are exclusions: application can target concrete CPU instruction-set architectures, usually for compatibility with some native-code modules (in .NET it can be done with C++/CLI or P/Invoke, Java also can use native code via JNI). Such applications looses part of its portability, they can work only with the CPIs compatible with the target CPU.

Please see:
http://en.wikipedia.org/wiki/Java_virtual_machine[^],
http://en.wikipedia.org/wiki/Bytecode[^],
http://en.wikipedia.org/wiki/Virtual_machine[^],
http://en.wikipedia.org/wiki/Portable_Executable[^],
http://en.wikipedia.org/wiki/Just-in-time_compilation[^],
http://en.wikipedia.org/wiki/Compiler[^],
http://en.wikipedia.org/wiki/Linker_%28computing%29[^],
http://en.wikipedia.org/wiki/Loader_%28computing%29[^],
http://en.wikipedia.org/wiki/Instruction_set[^],
http://en.wikipedia.org/wiki/Common_Language_Runtime[^],
http://en.wikipedia.org/wiki/Platform_Invocation_Services[^],
http://en.wikipedia.org/wiki/C%2B%2B/CLI[^],
http://en.wikipedia.org/wiki/Cross-platform[^],
http://en.wikipedia.org/wiki/Operating_system_abstraction_layer[^],
http://en.wikipedia.org/wiki/Hardware_abstraction[^].

—SA
 
Share this answer
 
v2
Comments
Ziya1995 24-Mar-15 4:20am    
You need to understand that i know all of that literally!
I know ALL OF THAT!
I ask a specific question and you think i am not aware of all that, but i have read all that including those Wiki links until i came here and read everyday.

Now, let's stop at the point i need:
"Nothing is cross-platform, except common standard library things, such as console and file I/O. It's not "cross-platform", it's portable."
YES! It is portable and i can compile the same code for different OS.

Java can create a cross platform good stuff like games.
And now: can i create such the complex good stuff using C++ using "common standard library things, such as console and file I/O" to compile the same stuff for other platforms without nothing like QT?

I wanna create a cool game in C++ using C++ Standard Library to compile the same code for different OS, is it possible without nothing like QT?

Java runs the code on different platforms.
C++ compiles the same code (i mean its own C++ code, but as powerful as Java's) for other platforms or its portable capabilities are much less than Java's?

I asked the same question in different poses.
Sergey Alexandrovich Kryukov 24-Mar-15 8:53am    
If depends on what do you mean by "like QT".

Now you game, portable game:
It is possible if your game is based on pure console I/O based on standard C++ library.
It can be possible if you had some very widespread OS and hardware abstraction library with all your game needs. "like QT" :-).

In all other cases, no, it is not possible with C++.

It could be possible in case, say, Java applet.
It could be possible with C++/CLI, which I would not call "C++".

It is even possible in pure HTML+JavaScript where a Web browser could play the role of abstraction layer. For example: Tetris on Canvas.

Now, listen carefully. If you really knew "all that", you would easily draw this conclusion by yourself, moreover, it would be quite obvious to you. Actually, claiming "I know all that" is much worse that knowing next to nothing, because illusion of knowledge, or false knowledge is much less then lack of knowledge. With lack of knowledge, you can always learn.

Why are repeating the same idle words again and again? You don't understand. "...its portable capabilities are much less than Java's?" Wrong question. There is no comparison because 1) the portability is not defined by language, 2) the technology are of different type, there is no such comparison; you cannot compare apples with potatoes. And statements "as powerful as Java's" are just childish.

—SA
Ziya1995 24-Mar-15 9:25am    
Yes, i knew all of that and resulted the next conclusion:
I can write a code and compile it (the same code) for other operations systems.

In this case, as you say, i can create a game on pure console I/O based on standard C++ library and compile it anywhere depending on compilers.

I think i can create a pretty good stuff like "Jewels" game with some good graphics on pure C++ Standard Library and compile it anywhere depending on compilers.
Just like in Java!

If Java and C++ could support the same amount of operation systems, it could be the same quality of cross platform apps in Java and C++, first to run, second to compile and then run.

But! The next points confuse me!:
1. When i read about C++ Standard i see opinions like even simple GUI is not possible without frameworks like QT. C++ Standard has no GUI.
2. C++ standard library is so small i saw on a website, Java Standard library is so big.
3. You say console based games, but Java cross platform apps are not console based, they are based on nice GUI.

Do you understand now?
Why it is possible to create nice GUI on Java Standard, but not possible on C++ standard?
I have read and it writes C++ Standard Library has no GUI.

I think the right answer is the next:
It is possible to create the same language like Java where we write once and compile anywhere where JVM can work today and it will has all the abilities of Java Standard Library, briefly, it will be the same Java replacing running with compiling-to-run.

C++ Standard is just not specialized to have all the power of Java, for example, it has no GUI, Standard Library is so small in compare to Java, why?
Because C++ also would run on none screen device where GUI is not possible to run.

It is simple, C++ is specialized for apps and applets, but it could and we could have the same power of Java to compile anywhere.
Sergey Alexandrovich Kryukov 24-Mar-15 10:29am    
I already answered all your question. Will you accept if formally now?
You confusion the you think I still don't understand is imaginary. Just think a bit more if you are not getting it.
—SA
Ziya1995 24-Mar-15 11:21am    
I accepted, but i knew all of that, the real answer is:

C++ Standard Library is much more less powerful than Java Standard Library and it is fact, just look at lists of both of them.
You can not create a good app in C++ Standard, but you can do it in Java Standard. Why? Because C++ Standard is just not for apps and applets.
Yes, it is possible to create the same Java Standard to compile anywhere without JVM instead of running, but it is not made today.

I got everything.

""Much more less power" is about some relative measures, not the "real answer"."
C++ purpose and Java purpose are completely different, so, to measure them is not right, ok.
java is Platform independent and C++ is platform dependent

java is secure whereas C++ is not secure as there are pointers.

In Java you can have class without class,but in C++ one can write program without class.

In java,there is memory management and robust,C++ doesn't have these features.

java is multiprogramming and distributed,these can not be found of C++.

For more Differences between C++ and JAVA:

C++ vs JAVA

Video showing C++ vs JAVA
 
Share this answer
 
Comments
Stefan_Lang 23-Jun-15 6:23am    
Incorrect, misleading, and necroing an old QA that already had an accepted answer.

The first link you posted offers horribly outdated info: the very first point being made hints at the fact it's been written around the time Java was invented. A time when the huge number of issues, security holes and other problems hadn't been realized yet. It doesn't help either that the article is clearly pro-Java: it's trying to explain the advantages of Java, and doesn't even try to make a neutral comparison of features. Also a lot of C++ features not present in Java are hardly mentioned; one of the most powerful mechanisms of C++, templates, have been reduced to just STL containers. Really?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900