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?

12 of 99 comments (clear)

  1. Re:My advice, take it or leave it... by Profane+MuthaFucka · · Score: 3, Insightful

    That's probably because C++ isn't object-oriented. It's object capable, and probably better programmed in a generic style.

    Anyway, if you want to learn C++ fast, I suggest getting a couple good books, a lot of coffee, and a compiler. It'll take a short year or so, but it's completely worth it.

    This is not a flame - I'm a C++ fan. It's my favorite language. But if you think you can learn it well in less than a year, you're kidding yourself.

    --
    Fascism trolls keeping me up every night. When I starts a preachin', he HITS ME WITH HIS REICH!
  2. 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!
  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 1iar_parad0x · · Score: 2, Insightful

      Do us all a favor and cut the sarcasm! I'd be more sarcastic, but I'm trying not to sound like a troll.

      If the guy has been writing code for a while, he can make the switch. I'm guessing he's working in C++ shop, thus there will be some senior coders around to help him out.

      --
      What do you mean my sig is repetitive? What do you mean my sig is repetitive? What do you mean....
  4. My Opinion by The+Slashdolt · · Score: 3, Insightful

    My learning went from assembly ==> C ==> C++ ==> Java, and my advice to you is to not go from Java to C++. If you remember venn diagrams, imagine one circle as Java, Then draw circle next to it without intersection and call that one C. Now draw a circle that intersects Java and C, but also has its own distinct characteristics, and that circle would be C++. There is no way to learn C++ without learning C. If I were you, I'd learn C. Then once you learn C, your OO knowledge will make you question some things. Then C++ will make sense to bridge that gap. Learning C++, or C for that matter, is not going to be fast. And anyone who does learn it fast will be in trouble unless they're really talented. One thing is for sure, once you learn C/C++ you will REALLY appreciate Java. Good Luck.

    --
    mp3's are only for those with bad memories
    1. Re:My Opinion by E_elven · · Score: 2, Insightful

      There is absolutely no reason to learn C before going to C++. In fact, it will probably be harmful. All the C you will need will be provided by C++.

      --
      Marxist evolution is just N generations away!
  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. Re:My advice, take it or leave it... by OmniVector · · Score: 1, Insightful

    oop is a good solution. by no means the best, but good. the thing is certain problems are inherintly procedural, some are functional, others are oop, a few are logical, and even aspect oriented. the point is easy programming methodology has a use and as such should be applied where that use is best. if you told all AI programmers to stop coding and lisp and redo it all in C, they'd laugh at you.

    computer science 101: pick the right tool for the job.

    --
    - tristan
  7. Re:One word: Don't. by seanbry · · Score: 2, Insightful

    I am sorry, I am a bit confused by your argument. Move away from C++ to what exactly. The only other languages you mentioned where Perl, Python, Ruby, or Scheme. I have yet to use Ruby or Scheme, but I did look up some things on them.

    Most of those languages are scripting lanugages and aren't really what you'd program a major application in. I know I user perl for sys admin tasks and quick hacks to just 'get things done'

    If you're going to beat up on C++ tell them to move away give a decent language to move to. And you never explained what Java had that C++ doesn't.

    Please tell me why I want to move away from a language such a C++ or even its predecessor C. Those languages are solid, and fast. You can do most anything with them. And the cleanliness of the program is left to the programmer, so if you develope bad practices you'll write bad code. If done propertly I see nothing wrong with using C++.

  8. 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.
  9. Accel C++ by thebroken · · Score: 3, Insightful

    The book my Koeing and Barbara Moo, Accelerated C++, is an excellent starting point in my oppinion. If you have previous programming experience, i suggest you pick this up, rather than a 500 page primer. It goes through a lot of stuff and a suprising rate. It also teaches you some basic algo's and uses STL right from the start, not the sort of backward teaching some other books use. It's very good, and i suggest you pick it up. You might also try Bruce Ekels Thinking In C++, they can be found for free (both volumes) on his site. Simply google the title and it will take you there.

  10. Re:My advice, take it or leave it... by Anonymous Coward · · Score: 1, Insightful

    objective-c is definitely NOT what C++ should've been. Smalltalk isn't what it shouldn't have been either, as arkane, and provincial as most other languages born of the 70's. C++ is reviled by java zealots and "alternative" object-ish languages alike because of your fundamental idiosyncratic failure to grasp even the most fundamental concept of power.

    "With great power, comes greater responsibility".

    Didn't ANY of you ever read Spider-Man ?

    Part of why the current crop of developers has so much "faith" in java is because academia itself didn't see "fit" to teach principals of object-oriented programming in a language that they themselves didn't quite "get". Don't believe me, just ask how many CompSci majors are truly exposed to 'C', much less C++, by the time they're in their senior year in college.