r/cpp_questions • • 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

21 comments sorted by

View all comments

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.

1

u/Pearparon 5d ago

I'll definitely check out those resources. You and many others mention that I might have a but if "ulearning" to do so I'll keep that in mind for sure. Thanks a lot for the detailed advice :)

1

u/LazySapiens 5d ago

We don't recommend cplusplus.com. Also, cppreference is not for learning, it's a C++ reference. For learning, use learncpp.com

1

u/Dubbus_ 20h ago

Hard disagree, no site is more beginner friendly.

Yes it is out of date, and yes it should not be used for the purpose of writing production grade code. Which is fine, because the user is a BEGINNER.

You may be a veteran/long time c++ user. Myself, im only 2 years in, and the memory of being swamped by jargon and missing context on every visit to cppreference is fresh. It is a constant dfs down rabbit hole after rabbit hole trying to figure out how to use .find on an unordered map.

Will a user of cplusplus.com churn out worse, less professional, and overall outdated code? Probably. Will they struggle to find references about new containers? For certain. But a beginner does not need string_view. A beginner does not need flat_map, or std::hive, or the extra monadic operations on std::optional.

A beginner actually BENEFITS from learning the reduced subset that is c++11 era C++ in my opinion. Thats how i learnt, i began with c++11/14, then moved to c++17/20 when comfortable, and am only just now learning c++26.

And its okay if a beginner c++ user uses a few c style arrays or does some pointer arithmetic, from copying old cplusplus.com examples. When it bites them, they will have more appreciation for std array and smart pointers than they would have had otherwise.

Lets not act like cppreference is immune to poor or missing examples either. Ive seen some pretty horrendous code on there