views:

296

answers:

12

Possible Duplicates:
Why should i learn C++.
Which one: C++ or Java?

Hello. I only know Java and Assembly currently but was thinking of learning C++. However, I would like to hear from someone more experienced in the programming field which language should I dive into the most for general programming purposes (as in, the most efficient and versatile)? And in what instances would the other programming language be more usefull? Thanks.

+4  A: 

Java. Portable, object oriented and memory managed. Probably the best language for general programming. Although it depends on whether or not you're talking about application or script programming. If you just need short scripts, then learn a scripting language like perl or python.

You want to learn C++ if you need to do 3d graphics programming or more low level operating systems type programming. But if you're just doing general application programming, then Java can be nearly as efficient as C++ and the time saved by its portability and memory management is extensive.

Daniel Bingham
Java is nice, except for the language itself being *bloody awful*. Seriously, there are several reasons to like the Java platform (it's almost ubiquitous, it's mature and robust and so on), but the Java language is *not* one of them. (And I can't be the only one groaning when "object oriented" is used as a selling point. This isn't 1985. OOP is a description, like "purple", not in itself a positive thing. (it can be negative, for example when it means "not functional")
jalf
Comment not helpful. Why do you think the language is bloody awful?
Daniel Bingham
because I have tried other languages. Because it takes a page of code to fulfill the simplest tasks.Because it misunderstood what OOP *means*, and has stuck rigidly to that misunderstanding. Because it doesn't allow any of the features that in the last decade have become commonplace *everywhere else*. Functional programming, closures, lambda expressions are flat out impossible to express in Java. Generic programming, as in C++ is fundamentally unachievable too. The language has no way to express something as simple as a general "max" function. Not without throwing away all hopes of type safety
jalf
If you're doing any GUI work, importing Swing really slows things down. Is there some alternative for Java that I'm not aware of?
krdluzni
"because I have tried other languages." This is non-sequitor, I too have tried other languages. I actually grew up on C++ and still consider it my favorite. "Functional programming" It isn't a functionally oriented language, it isn't SUPPOSED to allow for functional programming. Generic programming is not fundamentally unachievable, it's practically essential to the language. "Because it misunderstood what OOP means, and has stuck rigidly to that misunderstanding" It seems to me it simply had a different approach to OOP than many other languages take, but one that has great value.
Daniel Bingham
Hmm... hard to carry on this sort of debate in the comments section of StackOverFlow. I know SO isn't supposed to be a site to debate this sort of thing, but somewhere to do that would be really nice.
Daniel Bingham
It's not a non-sequitoir. It's an answer to your question. I think Java is awful because I have seen what other languages are capable of.About functional, exactly my point. Java clings to an outmoded and flawed paradigm, and entirely prohibits anything new even if it would have been useful. Generic programming is *impossible* in Java. Anyone claiming otherwise has no clue what the term means. As for OOP, perhaps you've heard of Smalltalk? That's what OOP was supposed to be. Java's (and C++'s) implementation of it is flawed in every way. But C++ has realized this and moved on. Java hasn't.
jalf
Code size is really not much of an indication of a language's power either. Code size is virtually meaningless if you can code very close to fully factored which in java I can (Although I admit it takes a little more effort, skill and commitment than with Ruby). For me the critical important factors are can a single unit (class) be read and understood easily and does the language help keep surprises down to a minimum. I find Java far better than any of the other languages when looked at this way.
Bill K
A: 

C++ can be more efficient (IF done well), and is also very versatile . . . the problem there is primarily that it takes a lot of tiny steps to make a big leap. You won't be getting things done particularly fast. But, depending on what you want to program (You mention knowing assembly at the moment; C++ sits much closer to that than Java) it can be worthwhile to learn.

C++ would be useful for optimization and underlying systems programming.

Java is more useful for portable applications programming.

To throw this out there, C# is very useful for fast GUI design.

Walt W
Your first sentence is a urban legend since the rise of efficient jit compilers.
EricSchaefer
Yep. There are some important cases where C++ can be more efficient, but it is certainly not "substantially more efficent" *in general*.
jalf
This post is community wiki you know, you could change it..
Walt W
A: 

In terms of efficiency, C++ is much "closer to the metal" than Java, so similar code in both languages will run faster in C++.

fbrereto
No, it will not run faster since C++ is compiled to a static executable while Java consistently does dynamic analysis of its execution and optimizes the bytecode even between loop iterations if possible. See http://kano.net/javabench/
Esko
-1: Though this may have been true in the past, it's no longer a blanket truth such as you suggest. JVM optimization is VERY good these days and may well outperform a chunk of C++ code.
DarkSquid
Not necessarily -- JIT optimization in Java has reached amazing peaks; if you can stand (or rather "amortize") the large memory footprint and startup costs, you might end up in some cases with "steady state" performance that's equal or better in Java.
Alex Martelli
Not actually by much. C++ can be about twice as fast as java, but the interpreted languages (Ruby, Python) are 50x slower than C++. For some operations Java is quicker now because it can do runtime optimizations (and remember now that it does compile down to bytecode). Google "Code Shootout" for an awesome speed comparison between all popular languages.
Bill K
Apart from the quality of Java's JIT compiler, you're also forgetting that it is *extremely* easy to write inefficient C++ code. If you're not careful, you end up performing needless object copies everywhere, constructing temporaries all over the place, performing dynamic memory allocations and so on... All in all, yielding a program that is easily several times slower than the simple Java version.Determinging the "speed" of languages isn't trivial (and is, in general, impossible)
jalf
+5  A: 

Consider Python for "general" programming.

It's slightly easier to work with than C++ because it has pleasant memory management and a somewhat more complete OO programming model.

It's slightly easier to work with than Java because it's a "dynamic" language and doesn't require a lot of fairly complex type definitions to get something simple to compile.

S.Lott
+1 for Python. I just can't understand how Python has 'more complete OOP programming model'?
AraK
C++ doesn't have first-class "class" objects at run-time. A C++ function isn't a proper object. Also, C++ has "primitive" (non-object) types.
S.Lott
Python's an interpreted language. A scripting language. It's not a general programming language unless those general programs are scripts.
Daniel Bingham
You can write general programs in Python. Therefore it is a general programming language. Who *cares* how it is executed? Do I really need to remind you that Java was interpreted for many years?Or perhaps I should point out that Python compilers actually exist?
jalf
@Alcon, what an unbelievably ridiculous assertion, really. Is reddit.com "a script"? See http://code.reddit.com/browser. Is Youtube.com "a script"? It's not open source, but it's well known it's coded in Python. Ever heard of NLTK, numpy, scipy, MIT's CS intro course (now in Python, not Scheme any more), AstraZeneca, NASA, Industrial Light and Magic, Rackspace, Resolver One, ...? Cheez. Go read http://www.python.org/about/success/ , http://pythonology.org/success and links therefrom...! Python's "interpreted" EXACTLY like Java: it's compiled to bytecode.
Alex Martelli
Got a reference for the Youtube thing? That's new to me, and would really surprise me. But apart from that, I agree with you. Whether or not Youtube is written in Python, there are countless serious applications written with it.
jalf
@Alcon: Python's a dynamic, object-oriented language. I'm not sure what you mean by a "scripting language". Are you talking about languages that are used to script other apps: shell scripts or TCL?
S.Lott
@jalf: http://www.python.org/about/quotes/, "Python is fast enough for our site and allows us to produce maintainable features in record times, with a minimum of developers," said Cuong Do, Software Architect, YouTube.com.
S.Lott
+2  A: 

For general programming I prefer Python because you have the option of writing simple scripts or using OO depending on your needs.

If python is not an option, Java might be the better choice because it's more portable and you already know it.

Ryan Thames
A: 

I think learning any new language gives you more perspective, but since you know Assembly already you know how Object references are handled behind the scenes, so you don't really need to learn c++. Instead I'd suggest learning a functional programming language like Haskell, Scheme, or Erlang to learn a different way of thinking. Also, you could learn something more expressive like Python or Ruby.

But if you are looking to start in on a specific project, you might as well continue with Java as it is fairly efficient for most tasks.

Steve Tranby
A: 

If by 'general progrmming' you plan to include desktop GUI apps, then Qt alone is reason enough to go C++

Javier
A: 

Depends on what you are trying to do with it. If you are trying to do low level stuff then use C++, if you are trying to do web use Java. For desktop apps it's up to what you like to program in. If there are frameworks you like to use (e.g. .Net or Swing etc) then that could be a factor.

Personally I would say if you are considering learning C++, then learn it and expand your knowledge base that much more, especially if you already know Java.

I programmed device drivers for 3 years in C/C++, and later did websites in Java/C#. The knowledge I had from C++ has carried over to website development, and I feel it has made me a better all around developer.

The long and short of it is, figure out the right tool for the job and use it.

Scott Lance
+2  A: 

I've worked with both (Started in C and moved to C++ then Java with a spattering of other languages along the way).

I prefer to work with Java. Period. It's just about as fast, it doesn't use header files the syntax feels much cleaner, etc.

Normally I wouldn't post an opinion like this because it's guaranteed to piss off about 1/2 the people out there, but you did ask for an opinion "What should I use", so I'm just blatantly giving you mine. Not saying one is "Better" than the other, just an opinion.

Bill K
I don't think it's going to piss anyone off as long as it's not presented as fact. :) I personally prefer C++, but I can definitely see your point (especially about header files and syntax)
jalf
+3  A: 
Alex Martelli
+7  A: 

Both languages are turing-complete. And both languages allow you to call C code, so the two languages are equally versatile. There's nothing you can do in one of them, which can't, with enough effort, be done in the other as well.

As for efficiency, languages don't have one. A naive compiler will usually generate ridiculously slow code for both languages. What makes them efficient is the extent to which the compiler optimizes the code. And good compilers exist for both languages, meaning that both languages can potentially be very efficient. There are a few areas where Java is, and will always be, at a disadvantage (it has to load a large runtime, so startup costs are higher, for example), but they're the exception, not the rule. In general, both languages can be as efficient as the compiler (or runtime/JIT) allows.

as for which you should use for "general programming", it depends.

Grossly simplified, you might say that:

  • C++ is a big, messy and complicated language. It has a lot of useful features, but if you don't really understand what's going on, your program is going to blow up.
  • Java is so simplified and barebones that it is 1) impossible to really screw up, and 2) impossible to actually write expressive code.

Java is widely used in application development, where the main task is to present a nice GUI to the user. C++ is often used in high-performance computing, in games (where performance is important, and the developers are conservative and 10-15 behind the rest of the industry), and in big legacy apps.

Ideally, you should know both.

Java is probably going to be most useful if you just want results. If you already knew C++ really well, your productivity wouldn't be too bad. But as a beginning C++ programmer, you'll be spending a lot of time debugging and trying to figure out why your code doesn't do what you thought it'd do.

But most people are going to be more productive in Java.

However, I'd say that C++ is by far a more interesting language to learn. There are more interesting features and ideas in it, there are more things which, once you learn them, you just can't live without. And more concepts that are actually worth learning about, and which will make you a better programmer.

But of course, there are many other options.

If productivity in general apps is your goal, I'd go with Python, not Java.

And if expanding your horizons, and becoming a better programmer is your goal, I'd go with Haskell before C++. Or Scheme. Or Prolog.

Both C++ and Java are, in different ways, showing their age. Java is just embarassingly old-fashioned, clumsy and verbose. Modern languages allow far more concise and expressive code.

C++ is a mess of old and new ideas (but at least the new ideas are in there, unlike in Java), its compilation model is ridiculously inefficient, complicated and error-prone. It's hard to use and the compiler does almost nothing to ensure that your code is correct.

There are plenty of languages to learn. These two old dinosaurs don't have to be your first choices.

jalf
A: 

For general purpose I would actually use C#.

Between C++ and Java use Java because.

  1. Java IDE's are better than the ones for C++ (Visual and Eclipse treat C++ like an afterthought)

  2. Java is easier to use and has more features than C++

Use C++ if:

  1. Need speed
  2. Have too for some legacy reason
Nick