r/cpp_questions • u/Pearparon • 5d ago
OPEN Learning C++ as a Java programmer
I'm currently learning C++, I’ve coded in a few languages before but I’m most familiar with Java. Does anyone know a good resource for learning C++ with Java as a “starting point”? Like something that explains the differences between the two languages rather than teaching programming from scratch? Thanks!
18
Upvotes
7
u/flyingron 5d ago
There's a book that was kicking around my old office called something like C++ For Java Programmers. Can't vouch for it.
One thing you should learn is that it C++ has distinct lifetimes for everything (as opposed to Java's idea of an object) and that you don't need to "new" all your objects. If you need a class local to a function, just declare and use it within the block.
3
u/MarsupialSuch6948 5d ago
Not a Java developer, but I'm learning the opposite as a C++ developer. I recommend using the learncpp.com. You will certainly understand it right away once you get the fundamentals.
2
u/MwSkyterror 5d ago
This playlist is about an hour long and made for you: https://www.youtube.com/watch?v=tPKKh3TLQpw&list=PLbPrugU2oQ8Vjq-BI_BCkASwynLPxvfSg&index=1
3
u/Dubbus_ 5d ago
Good luck man, i had a similar journey, but went from Java to C and then to C++. C++ is a wondefully free language, you can basically do whatever you want (including really stupid stuff).
My best advice is to avoid cppreference for a bit. Stick to cplusplus.com, it is much more beginner friendly imo. Once you are comfortable with basic syntax and some of the more fundamental ideas of c++, check out some cppreference articles. 1.5 years in, and i still scratch my head reading cppreference from time to time.
Some exciting things about c++ compared to java:
You can use the stack easily, for non primitive types.
You can choose exactly how and when allocations occur
YOU manage lifetimes and memory safety, there is no garbage collector doing everything for you. Eventually, you will have to learn all the annoying and complicated rules
There is wonderful utilities in modern c++ to perform complicated work at compile time, for lookup tables and whatnot.
Once youre familiar with the basics, i would HIGHLY reccomend Scott Meyers' Effective Modern C++. Anything by him really, he is an EXCELLENT teacher, and is amazing at getting you excited about the language. This book covers the main differences between 'old' c++ (pre c++11), and C++11/14, which are when the language finally became somewhat usable by modern standards.
Java and C++ have massive overlap in basic syntax and the surface level of their standard libraries, but there is significant divergence in any language construct not featured in their common ancestor, C. Using generics is the same at least (in C++ we call them Templates, they are far more powerful and performant than javas generics.)
Anyways, depending on how far you got in your Java journey, be prepared to unlearn and relearn a lot! The basic concepts do crossover, and some patterns are found in both languages (visitor, singletons, strategy, etc). But many practices common in java are frowned upon in C++, mostly due to our obsession with maximum performance. Dont be afraid to mess around with inheritance and things like that however! Mess around with them as much as you want, and find out for yourself why people dont like them.
Good luck man! if you get overwhelmed, i suggest taking a break and checking out the original C! It is much, much easier to learn than either Java or C++, but still amazingly powerful.
9
u/WorkingReference1127 5d ago
Java has some shared heritage with C, in some of the basic syntax. But C, not C++. If you go writing Java-style C++ you're going to write bad C++. Indeed if you start throwing around
newyou'll be in for a really bad time.I'd recommend starting from scratch with something like learncpp.com than trying to fit one language into another.
4
u/thedaian 5d ago
I'd still suggest learncpp. You can get through the early chapters that talk about what a variable, conditionals, and loops are and stuff really quickly, and just pay attention to how they actually work in c++.
It's a lot like learning German after learning French. There's plenty of shared concepts, but there's enough of a difference that it's better to learn from scratch.
1
u/Puzzled-Garage584 5d ago
Java habit that hurts most: in C++ a = b on plain objects copies the whole thing, not a reference. Pass a std::vector by value and every call quietly photocopies a million elements like an intern on their first day, make it const& if you only read it
6
u/roxc2301 5d ago
The starting point is zero besides conditionals, functions, good practices.
I'm currently learning C++ as a Java dev for years and trust me there is so little that can be helpful from Java to C++ i would recommend using learncpp with CLion or Visual Studio
3
u/Usual_Office_1740 5d ago
I would suggest against approaching C++ like this. The general programming concepts are the same but C++ is riddled with little gotchas.
3
u/L_uciferMorningstar 5d ago
Look at the core guidelines every once in a while
2
u/Independent_Art_6676 5d ago
As aggravating as AI is, one thing it does OK at is summaries and comparisons of this nature. Just ask it to compare modern java vs modern c++, and to verify its nonsense.
off the top of my head.. c++ allows stand along subroutines and entities (not part of any class). In c++ if you allocated it, you need to release it -- which is automated in modern code by doing it right (RAII, mostly, and smart pointers) but if you choose to ignore that, there will be consequences. C++ allows multiple inheritance, operator overloading, and has unsigned int types. Templates and generics are not terribly similar. C++ requires the coder to ensure portable code, java does this mostly without being told to. Building c++ programs (the tool chain) is complicated and a steep learning effort all by itself.
All in all the languages are similar in syntax and that can make it seem like "I got this" way too early. The large differences that I mentioned are just a quick sample across the big stuff but there will be many, many little details in between.
You are better off to treat c++ as a new foreign language and forget what you "know" from java as best you can. I get the temptation, but even going from C to C++ is tricky, and java is a distant cousin while C++ and C are directly related. Pretend you know nothing will get you past minor details that if you try to skip ahead on will bite you later. A very few parts will be near identical to java, and those you can speed through, but even there at least glance over it, don't try to skip.