Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java vs C++

 
Mike Isano
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to decide whether to learn java or C++.

java cons:
---------------
I've heard C++ is way faster than java. Java is supposed to be 20 times slower. Is this performace acceptable?

I've also heard that java has NO pointers. How are you supposed to know where something is located in memory?


java pros:
----------------
Java is portable and will run everywhere.




C++ cons:
---------------
I can't find any cons.


C++ pros:
--------------
Very fast. It has pointers. Supports multiple inheritance which allows much more powerful designs than java. True object oriented programming and not limited watered down version.



I'm thinking about learning C++ since there aren't any cons. The only thing i'm worries about is the portablility? How hard is it to make a C++ program run everywhere?
 
Jesus Angeles
Ranch Hand
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this is not an 'advanced' topic; this should probably be on another topic group.

How much time do you have to research on their difference? If you have like 80 hours, get a beginner's book on each - like 'c++ in 21 days', and 'java 2 version 1.5 in 21 days'.

There sure are more cons in c++ than you mentioned. E.g. if someone forgets to properly handle his memory variables in c++, it can crash the whole system; such wont ever happen in java.
 
Aaron Roberts
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, here's my two cents.

You assertions are flawed, because they are too general. If you don't know how to program right now, then you won't understand a deep technical reply on the merits of one language over the other. I can provide some insight which I have found to be true for myself.

I prefer Java to C++ because of the following -
- I hate having to look in two source files to get an idea of whats going on. This is a personal peeve, so you should take it as such. Not all source code is well written in C++, which means I end up poking in two files, rather than one in java

- I enjoy not having to muck with pointers. (Don't believe the line that everything in java is a pointer, because its not. See this Java Pass By Value for a nice explanation with lots of sarcasm and thought.

- The language syntax is great. I could not care less if it compiles down to bytecode vs assembly vs basic, as long as it does what I want. Such is the case for this language. This is also the reason I begrudgingly like C# as well. The syntax is so much 'cleaner'.

- In general, with the exception of using JNI and some other detailed Swing nits, it does work on the top three OSes - Mac, Lin, Win

Why I like C++

- Its better than C.

- Its object oriented and provides great flexibility to do whatever well thought out or really stupid implementation scheme I feel like.

- I can use it on any OS I happen to be forced to use.

So here's what I don't like about Java / C++ -

Java
- I've lost touch with the newest cool features, since I currently work at a place that uses C++ only.

- It doesn't run everywhere perfectly, in fact performance can vary widely depending upon OS, JVM version, graphics card installed, memory, etc. Normally this is never an issue for the projects I've worked on, but I am familiar with what things could put a person in the 'bad lands.'

- Sun took too long in making an IDE with a strong GUI development component. There are still tons of ways to make guis by hand, using frameworks, etc. I would have preferred a nice GUI IDE earlier on. There are tons of opinions on this alone, so feel free to disagree.

C++
- Most programs I've come across are C programs that tried to pretend to be C++. The results are silly and its obvious the programmer wasn't an OOP developer.

- A newbie will spend more time fighting tools when learning than programming. You can take someone who is excellent at C++ using MSStudio Vxxx and plop them in front of a Linux box, and they will be helpless to produce a hello world. Not always, but many times. This is the one thing about learning to program any language on any OS that is most annoying. Fighting the tools, just to get the application to compile, link, and execute.

- Makefiles are of the devil. I don't care what anyone says, they are evil. C++ is usually accompanied by these nasty things, regardless the OS.

Ok, so here's my advice. Do youself a favor and take the other posters advice and learn them both. Being a good programmer is like being a linguist. All languages express ideas, its just the words which change. If you understand programming concepts like factory methods, singletons, threads, looping, conditional logic, etc then the language is just a way to make your designs concrete. Some programming languages express some concepts more easily than others.

Cheers!
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I couldn't agree more with Aaron on nearly every point he makes. My advice in this day in age would be to start with Java because IMHO it will be easier for you to learn.

How are you supposed to know where something is located in memory?

In the wonderful world of Java, you really don't care about this. That's one aspect that makes Java an easier language to learn. Easier being relative obviously. But as Aaron stated, once you learn the concepts, picking up another language is typically just syntax and semantics. The concepts stay the same. An object is an object is an object.
 
Henry Wong
author
Marshal
Pie
Posts: 21114
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I too, agree with Aaron on nearly every point... except...

- Makefiles are of the devil. I don't care what anyone says, they are evil. C++ is usually accompanied by these nasty things, regardless the OS.


I guess Java's equivalent to Makefiles would be Ant files. Who the heck thought that using XML for building projects is a good idea? I guess it is tolerable for providing some structure of dependences, but by the time you add all the features that Makefiles supports, you get a huge mess.

The first time that I saw an Ant file, my thought was.... Wow. Writing compilers is definitely a lost art. It is easier to just use XML parser instead.

Henry
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Who the heck thought that using XML for building projects is a good idea?

Me for one. But seeing as how you can technically compile and build C++/Java projects without using Make or Ant, I'd say that is a bit off topic.
[ January 20, 2007: Message edited by: Gregg Bolinger ]
 
Aaron Roberts
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd have to agree about Ant. My feeling is that -

Makefile = evil
evil = 666

Build.xml = 333
333 = Half evil

I like ant's build files better than make. Its not easy to just jump into making your own ant file, once the process goes beyond just compiling. The files seem to get a bit unwieldy for me at least.

Cheers,
Aaron
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64827
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please take the Ant vs Make discussion to the Ant forum.
 
Henry Wong
author
Marshal
Pie
Posts: 21114
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry. Sometimes I can't seem to control my anti-XML sentiments. Please ignore my last diatribe.

Henry
 
fred rosenberger
lowercase baba
Bartender
Posts: 12122
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've heard...
I have heard that some people can speak with the dead, that Mikey died when he drank coke and ate pop-rocks, and that aliens are stealing our women.

I don't believe any of it without evidence. And, evidence for determining which is faster/slower is VERY hard to come by. It's VERY hard to conduct a fair test that PROVES one is faster than the other JUST because of the language.

How are you supposed to know where something is located in memory
I have heard that these newer cars don't have carburetor. How are you supposed to mix fuel and oxygen without a carburetors?

My point is, Java has OTHER things that don't require you to know where things are in memory. Plus, many people would tell you it's a BAD idea to mess directly with memory. Take a quick poll here and see how many people have lost HOURS of time debugging a memory clobber when somebody tries to write too much to an allocated buffer, or forgets their null-terminator on the string.

Java is portable and will run everywhere.
Not exactly 100% true. There are some platforms (usually OLD ones) where there is no JVM, so no java. It is also possible to write java code that IS platform specific, and thus not portable.

Very fast. It has pointers.
Here you are counting the same thing as both a negative for Java AND a positive for C/C++. You're kind of giving it double the weight it should have.

Also, there is a camp of people who would say that pointers in C should go in the CONS for the language.

Supports multiple inheritance which allows much more powerful designs than java
Java has a mechanism that works very similarly to multiple inheritance. And again, some people would say that multiple inheritance should go in the negative column for C/C++.

True object oriented programming
Search around here to see many discussions on whether Java is a "truly OO language". You'll see that different people disagree on what that term even means. But, the resons generally cited for why Java is NOT could pretty much apply to C/C++. In fact, I would say that C++ is LESS OO, simply because it has to be 100% backwards compatible with C, a purely procedural language.

How hard is it to make a C++ program run everywhere?
It's easy to WRITE the program. The problem is that you have to compile it for every platform. Want it to run on Windows? you need a windows compiler. Unix? another compiler. Sun? another. Linux? Mac?

Now, with java, you write it once. you compile it once. you send the same .class files everywhere, to any platform, and they work.
 
Steve Fahlbusch
Bartender
Posts: 605
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you ask 'Is a screwdriver better than a wrench (or spanner)?'

Bottom Line: they are both tools and with any tool you will find that in one situation one tool will be more approprate than another.

Any pro can be a con - depending on the point of view.

I would suggest that before you learn either one you spend some time researching the whats and the whys for each language (in fact i would suggest that you research c, c++ and java) then get down to learning a language.

That said, after you have some understanding of why, then start by learning Java (it is far easier on your brain, first). But this way you can evaluate when a pro or a con comes by, by evaluating the point of view from whence it comes.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are some program functions you can do in C++ that you cannot do in Java, such as call a 3rd party DLL in Windows. I have no problem with people who use C++ for low level stuff, close to the OS APIs. Thank you, C++ gurus, for many fine tools!

When I write applications one way I evaluate a language is how much of the code sings about my business. On my current project, any line of code that doesn't say something about customer service or insurance contracts is noise. Java has a much better business to noise ratio than C++. But I gotta tell you, CICS COBOL was far better yet.

That's also a ratio I look at with frameworks. What does EJB do for our business to housekeeping ratio? Ack!
 
Guy Allard
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May of the posters have made many very valid points.

I'll add my two cents on a couple of the ideas.

Where things are in memory: you are not going to write device drivers, OS memory allocators, or interrupt handlers in Java. Right tool for the right job.

Many C++ programs are really just C: that is a fact. Most 'C++' programmers I have met could not find an object if they had to. And if they can, do not know what to do with it.

Make versus ant: With Java I am a big ant fan. I cannot bring myself to obfuscating build processes with something like maven. With C/C++ I always disliked make. It goes in the other direction: too much detail required. For the little bit of C++ work I still do, I am actually using rake. Go figure.

Guy
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mike Isano:
...java cons:
---------------
I've heard C++ is way faster than java. Java is supposed to be 20 times slower...

In Core Java 2, Volume I - Fundamentals, Cay S. Hortmann and Gary Cornell cite this is a "Common Misconception" about Java.
In the early days of Java, the language was interpreted. Nowadays ... the Java virtual machine uses a just-in-time compiler. The "hot spots" of your code will run just as fast in Java as they would in C++.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mike Isano:
I've heard C++ is way faster than java. Java is supposed to be 20 times slower. Is this performace acceptable?


That's a myth. Today Java's performance is mostly comparable to C++. There are even circumstances where it can be faster.


I've also heard that java has NO pointers. How are you supposed to know where something is located in memory?


As others already said, you really don't care.


C++ cons:
---------------
I can't find any cons.


C++ is a much more complicated language. It has many more features, and mastering them all - and especially being able to decide which to use when - will take much more time.

As far as I know, weak typing still is the default in C++.

No garbage collector per default.

Much inferior IDE support. No powerful refactoring browser etc.


Supports multiple inheritance which allows much more powerful designs than java.


Multiple inheritance also come with a lot of conceptual complexity. And I know very experienced developers coming from C++ to Java who state that they miss is roughly once every couple of years.

True object oriented programming and not limited watered down version.


To paraphrase Alan Kay: "I've invented the term 'object oriented programming', and I can assure you that I didn't have C++ in mind!"

In fact, C++ was never meant to be an OO language - it's a multi-paradigm language. Actually, most C++ programs I've seen in my life were very procedural in nature. (That's also true for many Java programs, though.)

If you want to learn "true OO programming" - which I think is a great idea - you should rather take a look at Smalltalk (for example Squeak) or Ruby (which is very much influenced by Smalltalk).

I'm thinking about learning C++ since there aren't any cons.


I hope you've learned that there isn't a language without cons by now.

Of course that doesn't mean you shouldn't learn C++ - it's an interesting language and working in it will tell you a lot about the low-level details of programming languages.

One of the most important things for a serious programmer, in my opinion, is to not stick to one language, anyway. Learn many as different languages as possible - one as close to hardware as C or C++, a mainstream one like Java or C#, an OO language like Smalltalk or Ruby, a declarative language like Prolog, something functional like LISP etc. pp.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java is 20 times slower? Direct control of pointers is a good thing? C++ is "true" OOP, while Java is a "limited watered down version"? Multiple inheritance allows "more powerful" design? Java's only advantage is its portability? There are no cons to C++?

Mike, where exactly did you find these lists of supposed pros and cons?
 
Jacob Rich
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Supports multiple inheritance

Uhm...that should a sure con...multiple inheritance is just confusing and also allows risky things like the DDOD (Deadly Diamond Of Death), Java has it's way of inheritance, its called superclasses and trust me...its way less confusing, and OO is a pro, procedural guys have to re-code things and remove whole portions where as OO guys only need to edit there mistakes

I hope this helps you with your decision.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's some interesting info about the diamond problem which you can get into with multiple inheritance: Diamond problem (Wikipedia)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic