Slashdot Mirror


Learning C++ for Java Programmers?

The Real Joe Faith asks: "The O'Reilly book 'Java in a Nutshell' used to include a really handy introduction to Java specifically aimed at C++ programmers. It meant an experienced programmer could re-use their knowledge and get up to speed quickly. But what about going the other way? I know a fair amount of Java but, for my sins, have always avoided C++. Now I need to learn it. Fast. Not just the syntax, but also about the various standard libraries out there. Now that Java is the standard language on most computer science courses I guess there will be a few people in the same boat. Can anyone recommend a good book (or any other information source)?" For those Java programmers among us who have gone this route, what books did you use to assist you in the transition to C++? How well did these books work for you?

9 of 99 comments (clear)

  1. STL is important by magefile · · Score: 4, Informative

    This is probably the best tutorial I've come across for the Standard Template Library.

    Good luck!

    1. Re:STL is important by Profane+MuthaFucka · · Score: 4, Insightful

      STL is MORE than important. Learning all about the STL is an activity that should occupy the time period from 1 to 1.5 years after a person starts learning C++.

      --
      Fascism trolls keeping me up every night. When I starts a preachin', he HITS ME WITH HIS REICH!
  2. book we use for 15-113 by viperstyx · · Score: 4, Informative

    heres a book that was suggested by my professor here at CMU for our intro to C class: "C for Java Programmers" by Thomasz Muldner" ISBN: 0-201-70279-7 not exactly c++ but may be you can find something like it. in anycase, should help others.

  3. Do us all a favor by Henry+V+.009 · · Score: 4, Insightful

    Please don't code anything in C++ after learning it "fast." It's not that kind of language.

    1. Re:Do us all a favor by baylorhawk · · Score: 4, Informative

      The parent post has a point. C++ is a difficult language to pick up quickly. My college has 3 C++ courses that are prereqs to just about all the other cs courses, and I still didn't learn it well until my senior year when I had to use it again.
      Learning C++ for any practical use will take time and practice to learn all of the nuances, especially the copy constructors, operator overloading, and all of that junk.

  4. Trollish? True! by Webmonger · · Score: 4, Funny

    C++ is an excellent language, but it gives you enough rope to hang yourself, your immediate family, and maybe a few pets. Learn it fast, and you'll learn it wrong.

  5. You don't need a Java to C++ book by LoveMe2Times · · Score: 4, Insightful

    Just be confident knowing that most of your basic skills will transfer, and get a normal C++ book. Personally, I recommend Stroustroup's book, and Dietel and Dietel is an oft used text. Most of those "Learn C++ in 24 Hours!" types of books are a waste of trees. Like others have said, it's crucial that you get something that covers STL (both the ones I mentioned will). It's C++'s version of a class library. You'll find it a little thin after working in Java, and that's where platform specific stuff enters the picture. You'll find most of your time is learning a new widget set (presuming you need GUI programming), or socket APIs (if you need 'em), or platform specific threading/synchronization etc.

    Overall, coming from Java, you won't expect or know how to use fancy language features. That's Ok. Write C++ like you would Java (mmm, careful about that memory leak though). This assumes you're doing app development where you can afford to make every function virtual, every object a reference, and have accessor functions for every member variable. If that's not what you're up to and you need write tight C++, well, forget Java while you learn C++. Make sure you get your memory models straight--stack vs heap, by value and by reference, pointers vs reference, iterators vs pointers, reference counted, and on and on. You have to get the now classic "Effective C++" books by Scott Meyers. That's the best way to avoid classic stupid C++ mistakes. You'll find it a bit heavy going at first, but until it makes sense to you, you're probably writing bad C++. Good luck.

  6. The three needs of Java2C++ programmers by SilentJ_PDX · · Score: 4, Informative

    I was in the same boat a year ago. I think (good) Java developers needs are unique when approaching C++ because we already know about 80% of the syntax and concepts, but we need a quick way to get into the STL, tips on how NOT to hang yourself (far too easy in C++), and some sense of how C++ programmers organize code.

    For getting into the STL, I chose Accelerated C++ by Koenig and Moo. It is very basic at the beginning. However, it's not a 700-page behemoth (a Good Thing), it approaches C++ as OOP from the outset, and it starts using the STL from chapter 1. I'm sure you'll need a full STL reference after this book, but it serves as a decent starter. (I also have "The C++ Programming language" by Stroustrup and it's a bit too close to a language definition... good reference, bad primer)

    For the tips, Scott Myers books can't be beat. Enough people have heaped praise on them that I won't bother with it here.

    That leaves the last part of my education: "how do C++ programmers organize code". Unfortunately, the C++ world doesn't seem nearly as unified as the Java world. I started out doing things very Java-like, but decided that probably wasn't going to work if I eventually start coding with other people. Accelerated C++ has some tips. The C++ Programming Language also has a lengthy discussion on how to organize your code. Being the completely anal guy I am, I wanted to get it right the first time. Unfortunately, that's not possible. My style is still changing frequently as I see elements I like. I'm sure it will calm down if I ever get a job in a C++ shop.

  7. Things to consider when coming to C++ from Java by linuxhansl · · Score: 4, Insightful
    I started out with C++ a loonngg time ago. For all of my current projects I suggest Java (although I actually don't code that much anymore), unless I need to do system programming.

    Here's (on the top of head) what comes to mind when I think ab out moving from Java to C++:
    1. Learn the STL (as has been suggested here before). It's a great way to understand the philosophy behind C++.
    2. Look into templates (which you need to understand the STL anyway). You can write very generic code if you can employ "type-variables".
    3. Consider using stack variables even for non-primitive types.
      In C++ you can place objects on the stack with no need to allocate an object on the heap.
    4. Think about an ownership model from the beginning. C++ does not have a Garbage Collector, so you have to make sure that from the beginning you always know who owns which objects when and who is responsible to allocating/deleting it. (That also relates to using stack variables if you can).
    5. For the reasons above, don't forget about destructors.
    6. Learn about when C++ performs automatic conversions, when copy-constructors and default-constructors are called, etc. These conversion are sometime unexpected.
    7. Learn about pass by reference vs. pass by value. In Java Objects are always passed by reference, primitive types by value. In C++ you have much more control. Specifically objects are passed by value by default leading to sometimes unnecessary copies.
    8. Learn about const. Const "Constants", const parameters, const methods. That can make your code safer.
    9. Think about "virtual". In Java every object is automatically polymorphic. In C++ you have to declare method as virtual to use late-binding (which is used to achieve polymorphism).
    10. Look at the different forms of class derivations. In Java there's only one form (equivalent to the C++ public form). In C++ you have public, private, protected derivation (no, that's not the scope identifier for members). C++ also supports multiple inheritence, in that case in addition you have to think about virtual derivation.
    11. There's no "finally" in C++. Typically you code guaranteed actions at the end of a method call in a destructor of a stack-allocated object.
    12. Take a look at threading. C++ does not included any standardized thread library. There are abstraction libraries out there that wrap the Windows API and PThreads into one nice library.