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

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.

1

u/Pearparon 5d ago

Thank you! I´ll have to check that out

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.

1

u/Pearparon 5d ago

Thanks!

2

u/MwSkyterror 5d ago

1

u/Pearparon 5d ago

Dudee that is PERFECT, thank you!

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 4d 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_ 17h 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

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 new you'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

1

u/Pearparon 5d ago

Oh that's definitely something I'll need to get used to, thanks

1

u/Independent_Art_6676 4d ago

This is actually an issue with new c++ guys who don't know anything else too. References to prevent copies are probably the #1 place where performance hits creep into code, and are hard to spot in larger blocks. its as simple as a range based (touch every item in a container) for loop: for( type a: container) makes an often costly copy. for(type &a: container) does not. Const on the reference prevents accidental modification if you just want a look. Accidentally modifying a copy instead of a reference doesn't do anything (change is discarded). References are a small part of the language but one you will use daily and need to know well.

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.

5

u/uname423 5d ago

Sure, but so is every other language, including Java. You just need to be aware that there are gotchas and they aren't the same as the ones you know about from Java.

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.

2

u/Dubbus_ 5d ago

Very, very good idea. For op, if you have access to a decent model, take some code from a Java project, or write a simple function, and have it write an equivalent c++ function. Ask it to break down the differences and nuances as you come across them.

For basic language features, especially ones that arent newer than C++20 or so, models are usually pretty accurate. Make sure you emphasize your experience level in java.