Want to read Slashdot from your mobile device? Point it at m.slashdot.org and keep reading!

 



Forgot your password?
typodupeerror
×
Books Media Programming IT Technology

Learning C++ for Java Programmers? 99

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?
This discussion has been archived. No new comments can be posted.

Learning C++ for Java Programmers?

Comments Filter:
  • My advice, take it or leave it...

    Switch jobs fast. You do not want to get into C++. It's a freaking mess. Remember Alan Kay, father of OO? Here's a quote:

    I invented the term 'object-oriented' and I can tell you I did not have C++ in mind.
    -- Alan Kay


    • 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.
    • I did not have C++ in mind

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

      • by Anonymous Coward
        LOL. You know he won the Turning Prize for SmallTalk.

        Really? In what direction was it turning?
      • i'm not sure if this is supposed to be a joke or what. smalltalk is a well done OOP language. objective-c is what c++ SHOULD have been. (which for those who don't know is a smalltalk/c derivative).
        • by Anonymous Coward
          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 j
  • STL is important (Score:4, Informative)

    by magefile ( 776388 ) on Friday May 07, 2004 @09:35PM (#9090811)
    This is probably the best tutorial I've come across for the Standard Template Library [yrl.co.uk].

    Good luck!
  • no API (Score:3, Informative)

    by baylorhawk ( 650925 ) on Friday May 07, 2004 @09:50PM (#9090891)
    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 [sgi.com] 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 [amazon.com] 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.
  • I like C++ a lot more for learning theory. Data structures and algorithms and what not. I'm working on a project now with Java though and I love it for actual development. Easy to read API plus everything is already done, no lower level networking or serial interfacing.
    I learned C++ first and I found it easy to learn. Then I learned java, and i find it similar. The only difference that might be hard to grasp is the difference pointers and references have between the two. Just look up a website listi
  • by viperstyx ( 578360 ) on Friday May 07, 2004 @10:21PM (#9091042) Homepage Journal
    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.
  • Do us all a favor (Score:5, Insightful)

    by Henry V .009 ( 518000 ) on Friday May 07, 2004 @10:21PM (#9091044) Journal
    Please don't code anything in C++ after learning it "fast." It's not that kind of language.
    • by Webmonger ( 24302 ) on Friday May 07, 2004 @10:44PM (#9091170) Homepage
      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.
    • Re:Do us all a favor (Score:4, Informative)

      by baylorhawk ( 650925 ) on Friday May 07, 2004 @10:45PM (#9091175)
      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.
    • 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.
  • Man, you don't need to learn C++, you need to learn to be more truthful during job interviews. End of sermon.

    Now about c++. Boot linux, bring up kdevelop, create a console project and start writing.

    What's that? You need to program windows apps! OMG!! You are a big liar!
  • What I did was simply buy and read a C++ programming book, the same way many C++ programmers learned C++. The only difference is that with my knowledge of Java, I was able to skip (or quickly review) the parts of C++ that are similar to Java (and there are quite a few). The rest I learned like any other person. Knowing Java just helps you to pick up the same stuff faster.
  • I started learning C++ about 7 years ago (I say started because it seems like no matter how long one has been programming C++ there are always new things to learn), and I started with Java about a year and a half ago.
    One thing that seems apparent to me is that it is much more inherently difficult to go from a higher level language like Java to a lower level language like C++, because starting with Java the programmer becoms accustomed to not having to think about things which become major problems in a C++
    • Re:C++ THEN Java (Score:2, Informative)

      by Nicolay77 ( 258497 )
      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 thing
  • My Opinion (Score:4, Insightful)

    by The Slashdolt ( 518657 ) on Saturday May 08, 2004 @01:16AM (#9091720) Homepage
    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.

    • Re:My Opinion (Score:3, Insightful)

      by E_elven ( 600520 )
      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++.
      • Let me get this straight. It's your opinion that learning things such as pointers, references, pass-by-value, method pointers, macro's, STL, virtual, pure virtual, operator overloading, destructors, no-GC, etc, etc is perfectly fine to learn all in one shot without any prior knowledge. Remeber, that Java has no analogous to A LOT of these things. I would hate to have to see the code of some experienced Java coder having to see all of these things at once. But apparently you have different opinion.
        • It's your opinion that learning things such as pointers, references, pass-by-value, method pointers, macro's, STL, virtual, pure virtual, operator overloading, destructors, no-GC, etc, etc is perfectly fine to learn all in one shot without any prior knowledge

          What does any of that have to do with learning C before C++? C++ includes pretty much everything that's in C, but renders some of those things obsolete because it has better features. All one does by learning C first is learn the bad / old way of do
  • by LoveMe2Times ( 416048 ) on Saturday May 08, 2004 @01:29AM (#9091760) Homepage Journal
    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.
  • You'll Miss the API (Score:2, Informative)

    by pitfiend ( 681709 )
    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.cpprefe [cppreference.com]

    • by E_elven ( 600520 )
      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.
  • zerg (Score:4, Informative)

    by Lord Omlette ( 124579 ) on Saturday May 08, 2004 @02:09AM (#9091862) Homepage
    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.
  • You have my sympathy.

    After ten years of C++ programming, I'm underwhelmed with C++. It's broken, and Strostrup is in denial about its flaws. If C++ wasn't broken, we wouldn't have needed Java or C# or Objective-C, which, after all, look a lot like C++.

    The basic problem with C++ is that, alone amongst major programming languages, it has hiding without safety, which is a terrible combination. C has neither hiding nor safety. Java, Perl, Python, Ada, and LISP have hiding with safety. This has nothing

    • Witness Microsoft's endless buffer overflow problems. Switching to C++ has not helped at all.

      Microsoft never used MFC for their own development of code. It's all C. Nor was C++ ever really a priority at Microsoft until Visual Studio .NET; the Visual C++ 6.0 standard library was considered a sick and cruel joke, almost gratuitously incompatible with the real C++ standard library.

      The C++ language development community is dominated by people who like to do really l33t things with templates.

      You obviously

    • If C++ wasn't broken, we wouldn't have needed Java or C# or Objective-C, which, after all, look a lot like C++.

      A lot of people don't seem to realize this, but ObjC was created around the same time as C++. It wasn't made as a response to C++, but rather they were both responses to the idea of gluing OO onto C. Just a minor nitpick to a very good post.
  • 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.
  • Use one of the first two books for syntax, and use the rest to further your knowledge quickly.

    For syntax:
    C++ in a Nutshell
    by Ray Lischner

    OR

    Sams Teach Yourself C++ in 10 Minutes
    by Jesse Liberty, Mark Cashman

    For examples:
    The C/C++ Users Journal (from the publishers of Dr. Dobbs)
    http://www.cuj.com/

    Effective C++ Cd: 85 Specific Ways to Improve Your Programs and Designs
    By Scott Meyers

    Frankly, there are a ton of similar books out there...

    At some undisclosed site (Amazon.com) their data miners|web age
  • by SilentJ_PDX ( 559136 ) on Saturday May 08, 2004 @10:02AM (#9093169) Homepage
    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.
    • I want to second the Meyers and the "Accelerated C++" recommendations. As for "how do C++ programmers organize code", I would recommend another book by Koenig and Moo, "Ruminations on C++". Chapter 24, "An Object Lesson in Library-Interface Design" has the best example of how to organize a C++ program that I have seen. Even if you don't get the book, read this one chapter.
  • by linuxhansl ( 764171 ) on Saturday May 08, 2004 @02:21PM (#9094624)
    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.
    • 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.

      You are incorrect about Java.
      In Java, everything is passed by value. You never pass objects by reference, you pass references to objects by value.

      • Before you say somebody is wrong, it's always a good idea to ask google first. :-)

        http://www.javaworld.com/javaworld/javaqa/2000-0 5/ 03-qa-0526-pass.html
      • Re: Re: 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.

        Re: You are incorrect about Java.
        In Java, everything is passed by value. You never pass objects by reference, you pass references to objects by value.

        You are incorrect, well, not really as you just said the same thing as the original poster did but in
        • You are incorrect, well, not really as you just said the same thing as the original poster did but in a less clear way.

          I'm not incorrect. The OP said

          In Java Objects are always passed by reference, primitive types by value.

          while I said

          In Java, everything is passed by value. You never pass objects by reference, you pass references to objects by value.

          That's not the same and there's quite a semantic difference between those two.

          The way I told it is correct. And even you yourself say:

          Yes, you ar

          • There is only a difference between passing a reference to an object and passing the value of the reference to an object when there is a way to access an object without using a reference which there isn't in Java. So, in Java there is NO difference.

            In C++ where you can access an object directly, though a pointer, or by a reference there is a difference.

            Therefore, you are incorrect to state that the original poster was incorrect, thougth as you say... you just rehashed the view of the original.

            You might w
  • Accel C++ (Score:3, Insightful)

    by thebroken ( 761356 ) on Saturday May 08, 2004 @02:26PM (#9094654) Journal
    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.
  • After learning c++, I would recommend learning c#. I t has many of the nice features of Java, like complete OO, strong typing, automatic polymorphism, etc,, and many of the powerful features of c++ like operator overloading and the use of pointers in unsafe code blocks (only used when speed is very important).
  • You can't go wrong with Eckel's Thinking In C++. You can download it for free. [64.78.49.204] Or buy it. [amazon.com] 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).
  • (disclaimer: one of my livejournal interests is "informed dislike of c++". ;))

    Bruce Eckel has written some fairly good (IMHO) books on c++ and Java. You can download them for free from here [mindview.net]. The thing I like best about his approach is that it tries to teach you more than just the syntax, also (as the titles suggest) how to think in that language. The only con to his writting style is that his examples are sometimes a little contrived...

    As other posters have said, do try to learn the STL as soon as po

  • .. and I second the recommendation for Timothy Budd's "C++ for Java Programmers" (ISBN 0201612462) ... great book!

    Also, this [brown.edu] tutorial is full of useful information. However, you have to ignore the occasional bitter remarks about Java from the author, such as these gems:

    Unlike Java, C++ is a fast, powerful, and flexible programming language.

    Java was built as a simple-to-learn subset of C++ for set-top boxes and drooling AOL users. Now, it's time to play in the big leagues.
  • For me, Essential C++ [amazon.com] was a good jumpstart book, and then
    The C++ Programming Language [amazon.com] is of course the gold standard if you need to go deeper.
    I have also heard good things about Accelerated C++, but haven't looked at it myself.
  • My uni basically only taught Java as a programming language, which proved (not very) useful for my first real development job in c++.

    Things I wished I'd realised sooner:
    1) Learn how to use (or write, if you're brave) STL iterators with containers.
    2) Learn the difference between the stack and the heap.
    3) Use memory leak checkers (eg. valgrind), as you WILL get memory management incorrect.
    4) Realise that destructors in c++ are actually useful, in that you know (more or less) when they'll be run.

    I've read so

"Protozoa are small, and bacteria are small, but viruses are smaller than the both put together."

Working...