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?

27 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. no API by baylorhawk · · Score: 3, Informative

    Well, I can't recommend a book, since I learned C++ first, but I have a warning...there is no definitive API. I imagine that would be a stumbling block for Java-to-C++ converts.
    However, might I recommend this for the STL?
    I think that in searching for a book, you should probably look for one that highlights the differences between the languages. That helped me when I was learning Java, and it's probably the quickest way you can pick it up. Have a look at Amazon and just search under books for "java c++". The first few entries are aimed at this. I can't vouch for these books, but hey, that's what I found.

  3. 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!
  4. Re:My advice, take it or leave it... by the+eric+conspiracy · · Score: 2, Informative

    I did not have C++ in mind

    LOL. You know he won the Turning Prize for SmallTalk.

  5. 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.

  6. 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.

    2. 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....
  7. 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.

  8. Re:Ask Slashdot: where Google Morons ask questions by Webmonger · · Score: 2, Informative

    Not actually that useful. They don't mention smart pointers in the memory management section. They claim C++ has no string type. Not good.

  9. Re:Ask Slashdot: where Google Morons ask questions by InterventionOne · · Score: 2, Informative

    The link in the above post (see parent) seems to be broken for me. I don't know if this happens to be the same book as the one above...

    This is the book that my Survey of Programming Languages professor suggested for those of us who came in knowing Java but not C++.

    It's nice in that it compares and contrasts many features of both languages (including quite a bit on the STL and writing templates in C++), but I found myself wishing it would have gone into more detail several times - my one gripe is that it doesn't do much more than scratch the surface in several instances.

    (apparently, there's at least a couple of "C++ for Java Programmers" books on amazon alone...)

    A couple of reference sites I've used in the past:

    C/C++ Reference

    C++ Library Reference

    (Personally, I still shy away from C++ whenever I can...I certainly haven't gotten a comfortably firm grip on it yet.)

  10. Re:Ask Slashdot: where Google Morons ask questions by boneshintai · · Score: 3, Informative

    Ok, now having actually read the link, here is a list of things that are either actively wrong or totally unhelpful:

    • No mention of std::vector in the discussion of arrays
    • Deprecated pre-standard headers such as
      #include <pair.h>
      instead of
      #include <pair>
    • Introduces C-style IO (printf et al) before stream IO, even though Java provides workable streams and C-style IO is completely nonestensible in ways a Java programmer may be accustomed to
    • To declare a constant in C++, you use #define (not final) as in:

      #define MAX_SIZE 10

      No. Please use const instances. They're typechecked but the compiler is free to translate them into inline constants similar to what the preprocessor produces:

      const int MAX_SIZE = 10;
    • Macros (#define min(x,y) (((x)
    • void main (...) is totally wrong. Main is not permitted to be void in C++. Some compilers will allow it, but it is not a feature of the langugae.
    • C-style typedef struct {...} typename; is unnecessary in C++; structs essentially are handled as classes that default to public and as such the struct name is a type.
    • Unions.
    • "In C++, all parameters are passed by value." Unless they're passed by reference, anyways.
    • You do not need to use std::malloc to allocate a structure or a builtin type. The 'new' operator works just fine.

    Christ. This is from 1998. No wonder it's got so many issues.

  11. 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!
  12. 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.

    1. Re:You don't need a Java to C++ book by hummassa · · Score: 2, Informative

      Bruce Eckel's Thinking in C++ will help, also...

      --
      It's better to be the foot on the boot than the face on the pavement. ~~ tkx Kadin2048
  13. You'll Miss the API by pitfiend · · Score: 2, Informative
    First off, you're going to miss the Java API. I just spent the last semester in your predicament. I have a strong Java background, but I was forced to TA an intro to C++ class. I found that our text "C++ Effective Object Oriented Software Construction" by Kayshav Dattatri was a very good intro to C++ if you've got a Java background. He tends to make comparisions to Eiffel or Smalltalk, but Java comes into the picture often (and its pretty close to Smalltalk in a number of ways).

    Also check out www.cppreference.com. I have found it to be significantly more useful than the STL reference at SGI.

    To paraprhase Mark Twain, the rumors of the performance of C++ have been greatly exagerated. The advantage you get from cross platform compatibility and a fantastic API far outweigh a minor performance hit.

    1. Re:You'll Miss the API by E_elven · · Score: 2, Informative

      C++ is cross-platform. You just have to compile it separately for each target.

      Anyway, a few posters seemed to mention the Java API -or the SE part in J2SE, which to me seemed to be an alright compilation when I toyed around with Java. One should remember that C++ has all the libraries Java does, usually more and in various forms for one to pick and choose from. Google is one's friend for library-hunting aside from the nigh-obligatory boost.org libraries.

      --
      Marxist evolution is just N generations away!
  14. zerg by Lord+Omlette · · Score: 3, Informative

    My favorite book is Accelerated C++ by Koenig n Moo. It's C++ taught as a language w/ a library. It's not taught as C w/ classes tacked on as an afterthought. Check it out.

    You will spend the rest of your life learning C++, anyone who says they "know" the language is either lying or deluding yourself. After you're done w/ Accelerated C++, pick up Modern C++ Design by Alexandrescu. Also get a subscription to C/C++ Users Journal.

    --
    [o]_O
  15. C++ for Java Programmers, by Razzy · · Score: 2, Informative

    by Timothy Budd, may be what you're looking for. My uni taught intro cs in Java but in our OO development class, C++ was the language of choice. They threw this book our way to ease the transition. It didn't hurt, although Stroustrup's book, Meyers' Effective C++ and STL, and Lippman and Lajoie's C++ Primer are more useful in the long run. Also pick up a nice STL reference like Josuttis.

  16. 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.

  17. 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++.

  18. Re:C++ THEN Java by Nicolay77 · · Score: 2, Informative

    I learned Java first, and then C++ with wxWindows (now wxWigdets).

    The change was so welcome that now I use C++ with wxWin almost exclusively, even if somethimes I have to use Java (because someone pays me to use it).

    It has not only GUI classes but several data structures and system functions that make very easy to leverage the operating system API without actually going down and using it. Try to make a nice screensaver in Java.

    There is something I miss from Java, because I have to implement similar things in C++ and is serialization. That's where the difference between C++ and Java lies to me. ODBC is also better in Java, but there are ODBC libraries for C++.

    I find almost everything else better in C++. But remember, do not use features of the language that you do not fully understand. Just because C++ has templates, it doesn't mean you have to use them just to feel like you're using C++. The same with multiple inheritance, in fact, never use it.

    And compared to Java, good C++ is fast as hell. The right tool for the right job, bla, bla, bla, etc...

    --
    We are Turing O-Machines. The Oracle is out there.
  19. 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.
  20. 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.

  21. Why hasn't anyone recommended Thinking in C++? by techstar25 · · Score: 2, Informative

    You can't go wrong with Eckel's Thinking In C++. You can download it for free. Or buy it. It seems to have been designed for C programmers moving to C++, but for free it makes a great reference (picked up a hardcopy of an old edition on half.com for $3, just because I like hardcopies, although I keep the current softcopy nearby).