File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Java vs C++ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java vs C++" Watch "Java vs C++" New topic
Author

Java vs C++

Mike Isano
Ranch Hand

Joined: Jan 19, 2007
Posts: 144
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

Joined: Feb 26, 2005
Posts: 2061
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

Joined: Sep 10, 2002
Posts: 174
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

Joined: Jul 11, 2001
Posts: 15300
    
    6

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.


GenRocket - Experts at Building Test Data
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

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

Joined: Sep 10, 2002
Posts: 174
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

Joined: Jan 10, 2002
Posts: 61772
    
  67

Please take the Ant vs Make discussion to the Ant forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40

Sorry. Sometimes I can't seem to control my anti-XML sentiments. Please ignore my last diatribe.

Henry
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Steve Fahlbusch
Bartender

Joined: Sep 18, 2000
Posts: 582
    
    7

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

Joined: Jan 29, 2003
Posts: 8791
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!


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Guy Allard
Ranch Hand

Joined: Nov 24, 2000
Posts: 776
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

Joined: Aug 31, 2004
Posts: 11343

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


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Jan 02, 2007
Posts: 27
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.


~"Great is the art of beginning, but greater is the art of ending."~
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14435
    
  23

Here's some interesting info about the diamond problem which you can get into with multiple inheritance: Diamond problem (Wikipedia)


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java vs C++