Jump to content

A Java Developer's Adventure of Learning C++

Well, I know that a lot of people debate about how one language is better than the other, but I am going to do something different. For the past few weeks, i've been learning and using C++ instead of Java, and so far my experience has been very positive. Java to me feels like i'm being "babied" if you will, however that's a good and a bad thing at the same time. C++ however, has shoved me off a cliff and expects me to find my own way out (again, a good and a bad thing).

When i'm reading through C++ tutorials, I get all of the technical details of how things really work. I now understand how memory allocation works, and how to optimize and build habits accordingly. When I learned Java, I never really felt the need to learn how stuff like this worked, and kind of just went with things. I didn't really care how things worked because Java took care of it for me. This could partly be because of the book I read to learn Java, because they're all different. Never-the-less, even if I decide to write something in Java, the knowledge of how things work that C++ has forced me to learn are always going to be useful to have. If you're a Java programmer, I strongly suggest learning at least a little C++. It's made me a way better programmer in just under two weeks.

For small projects, I think i'll still use Java. Java's advantage over C++ is that it is much easier and much faster to write. You don't have to worry about memory leaks or segmentation faults (they'll annoy the crap out of you), and you can focus on just writing code. Sometimes you don't need all the control that C++ provides and just want to get something done as quickly and simply as possible. This is, of course, at the cost of optimization and control, which isn't very important in very small projects.

Overall, they're both pretty awesome. Java keeps getting closer and closer in speed to C++, and i'm excited to see how far Oracle can push it. Unfortunately, since applications are getting more and more resource intensive, the market for Java on the desktop is almost dead. I see Java's future on the server-side of things, which is the honest reason why I decided to learn C++.

A lot of this most of you are going to find obvious, but I just want to see if I can provide any useful information for beginners on where they should be going. I don't have a bias for either language, and I thought i'd offer a true comparison of the two.

Thanks for reading!

EDIT: Sorry for the formatting, I tried to to paragraphs but vBulletin's editor sucks!

Link to comment
https://linustechtips.com/topic/9237-a-java-developers-adventure-of-learning-c/
Share on other sites

Link to post
Share on other sites

If you are learning C++ as a Java developer, probably the most important thing to notice is RAII. In Java, you have the garbage collector cleaning up memory for you, which is convenient. But every other resource you have to clean up yourself (or now, with Java 7, use the more than ugly auto-closeable-hack). In C++, all resources get freed by a simple system: Destructors. If you follow these rules, you cannot, by design, get any leaks at all:

- Free stuff in destructors.

- If you have a destructor, a user defined copy-constructor or a user defined copy-assignment-operator, you probably need the other two things as well. (Rule of 3.)

- One object per resource. This is important. Never allocate another resource before all other resources are assigned to an object. If a class needs multiple resources, make them objects with destructors, don't hold raw handles! Keep that in mind for function calls as well:


void foo(unique_ptr a, unique_ptr b);

foo(unique_ptr(new int(3)), unique_ptr(new int(5))); // Think this is safe? Think again http://herbsutter.com/gotw/_102/


class my_class
{
int* a_;
int* b_;
public:
my_class()
: a_(new int)
, b_(new int) // What happens if this throws? Use unique_ptr!
{}
};

And really try to avoid shared_ptr. You think it makes things easier? It doesn't. Think about which object owns a resource, and which objects only know the resource.

Link to post
Share on other sites

If you are learning C++ as a Java developer' date=' probably the most important thing to notice is RAII. In Java, you have the garbage collector cleaning up memory for you, which is convenient. But every other resource you have to clean up yourself (or now, with Java 7, use the more than ugly auto-closeable-hack). In C++, all resources get freed by a simple system: Destructors. If you follow these rules, you cannot, by design, get any leaks at all: - Free stuff in destructors. - If you have a destructor, a user defined copy-constructor or a user defined copy-assignment-operator, you probably need the other two things as well. (Rule of 3.) - One object per resource. This is important. Never allocate another resource before all other resources are assigned to an object. If a class needs multiple resources, make them objects with destructors, don't hold raw handles! Keep that in mind for function calls as well:
 void foo(unique_ptr a, unique_ptr b); foo(unique_ptr(new int(3)), unique_ptr(new int(5))); // Think this is safe? Think again http://herbsutter.com/gotw/_102/ 
 class my_class { int* a_; int* b_; public: my_class() : a_(new int) , b_(new int) // What happens if this throws? Use unique_ptr! {} }; 

And really try to avoid shared_ptr. You think it makes things easier? It doesn't. Think about which object owns a resource, and which objects only know the resource. [/quote']

Thanks for the advice!

Link to post
Share on other sites

Ah, the language that caused me to realize that I didn't want to be sitting behind a desk writing code all day...

Two notable quotes regarding these two languages:

"Fine, Java MIGHT be a good example of what a programming language should be like. But Java applications are good examples of what applications SHOULDN’T be like."

- pixadel

"Pascal keeps your hands tied, C gives you enough rope to hang yourself, C++ gives you the rope and provides the gallows."

- unknown

Current Rig
AMD Ryzen 5900X - Asus ROG Strix X570-E Gaming WiFi 2 - 32 GB GSkill TridentZ RGB
GeForce RTX 3080 - WD Black SN850 1TB  - Lian Li O11 Dynamic XL

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×