“Is well-written Java code as fast as well-written C++ code? I’m a beginner deciding between the two for my future plans. Concerned about Java’s performance on weak hardware due to the JVM. Any advice on choosing the best language for me?” #JavaVsC++ #ProgrammingBeginner #JavaPerformance
On modern JVMs yes. But the Java has to be very well constructed with the absolute minimum thread synchronization the program can tolerate.
Look, there’s fast and there’s fast enough. You don’t need a car that can drive 220 mph/400 kph. It’s illegal to go that fast. Yes, if you’re working in embedded systems, you might prefer something like C++.
But arguably, finely tuned assembly would be even faster, but the time it would take to write that would be prohibitively crazy compared to writing good C++ code.
Worry about getting a language to do something before freaking out about how fast it is. People learn Python which is “slow”, but in many cases, fast enough. If that means learning to program quicker than learning Java, some say it’s a good trade off.
If you’re just starting out you shouldn’t worry about performance. Java is widely used in all sorts of applications and if you are hitting a performance bottleneck you can always use JNI and write interop C++. But for most of what you’re making, the performance differences between Java and C++ aren’t going to make a big difference, especially compared to the efficiency of the code itself that you’re writing. A lot of the concerns about the speed of Java are from decades ago, things have improved since then.
It also really depends on what you’re making. If you’re interfacing over a network, network latency is probably gonna be a much bigger bottleneck than execution time. If you’re just writing an application, half a second doesn’t really make a difference. It becomes an issue when you’re doing very large calculations, or when you’re trying to do a lot of calculations in a predefined time limit (like, say, 24 times per second for something synced with video, 60 times per second for a 60FPS game engine, etc.).
Also keep in mind that most of the skills you’re learning as a beginner will be applicable to nearly all languages, and most developers are proficient in multiple languages. You aren’t locking yourself into anything, you’re just building a base to expand on. Overthinking what language to start with is like if you wanted to learn the guitar and agonized over what genre. Just start working and building your skills and you can specialize later.
Java is insanely performant! Because the JIT compiler analyzes code while it is running, it identifies bottlenecks, and optimizes them away, while C++ will always be as slow or as fast as you, the programmer, and the compiler you selected at the time, decided it should be.
Unless you need extreme performance – like in that a few machine cycles more actually matters – you are often better of with Java than most other programming languages 🙁
Unfortunately – because I would love to argue for small compact hand-optimized assembly code any day, but unless you are working on very old hardware, and I’m thinking 15-20 years old or more, you probably won’t ever need it, or even be able to actually get that performance-boost.
lots of phones used to run on java, back when phones were slow.
Frankly, you’ll code in the language the company tells you to use. You won’t be the one making the decisions. And hardware is cheap now.
Java’s modern JIT compilation makes it VERY fast. Is it *as* fast? Nope, not close still I’d say. Is it *fast enough for 99.9%* of usecases? Yeah, pretty much
Thanks for everyone who answered!!
Very informative comments here!!
I think I will go with Java!!
The more important point is that you won’t be writing business logic in C++ for an enterprise. So the speed comparison is largely irrelevant. You could say the same for C#.
I mean, yes and no. It depends on what the code is doing, but honestly, it won’t matter most of the time. Even in something as performance-intensive as gaming, Java was used to make one of the most popular videogames of all time (minecraft).
If Java is more inline with your goals, then learn Java. And hell, learn C++ as well, then C#, then maybe Rust, then Python, LEARN LEARN LEARN. It’s never gonna make you less valuable to learn another language, and a lot of stuff you learn from any given lang can transfer over to others.
As a beginner, you shouldn’t worry about which lang you start with; you’re inevitably gonna learn more. Just focus on learning, and keep on the path.
Start with java its more easier and less confusing then c++ and at same time you can learn mostly every usefull concept that you can reuse in any other language
Specialy if you want to dive into backend i can recomend it, really stable and well developed env with nice tools to work with in long term
Performance should not be your main concern now. C++ is faster, but both languages have some use cases where they are better or more convenient. Just learn what you want to learn and don’t worry about it now.
Everyone gave good advice, Java is fine for pretty much anything. There are certain cases where C languages will be practically beneficial in terms of performance. For example, if you’re writing something that needs to do a lot of repetitive calculations as fast as possible, or if you’re running code on a very restrictive device where having the JVM may not be feasible, the C may make more sense.
I once worked with someone who over engineered a list of a max of 10 items that would be loaded once in an Android app (with Java). The sort optimization wouldn’t have been measurable, but trying to work with that code was a pain in the ass. So just use the language and style that fits your needs, and don’t worry about optimization until it’s actually a problem.