Java vs C++, your thoughts/opinions?

Shadowssong

[H]ard|Gawd
Joined
Sep 17, 2009
Messages
1,969
I've always heard arguments for either side, I am partial to c++ because that is really the only language that I know and I feel it is very powerful and I know some of my professors have claimed it is a much more powerful language than java. I've tried googling java vs c++ but results are very mixed so I was wondering what you guys in field feel about the debate? I know the two are good for different things but like.. what things are those?

I know the design and algorithms are really what programming is about but on a completely language based comparison, what do you think?

Thanks for participating! :D
 
They're both tools for different tasks. C#/Java are good for rapid development, but in the rare situation where you need every last ounce of performance, you can't beat C/C++ - you'll pay an extra cost in terms of not tolerating bad developers and having to do manual garbage collection, but it does lack a lot of overhead.
 
If you're still in school you don't want to be tied to one language. Learn Java, Python and something else so that you are well aware of their advantages and limitations.

Then you'll be prepared to program in whatever language you are getting paid for, instead of being a genius c++ programmer who is now incompetent at a Java or .net dev house.

(And i don't think most things you wind up programming need to wring the last ounce of performance out of the hardware).
 
Last edited:
It's really about knowing the fundamentals of programming, software design, and such, agnostic of any language. You never know when your project, or employer, or whatever, will call on you to use something you're not familiar with, or if you encounter a problem that could more easily solved using a different language (for example, you want to prototype an application without investing too much time because you don't know if it is a feasible idea, you can use a very high level language like Python to make it easier).

In one job, I was hired on as a generic sounding software developer, and my first project was C++, and then VB6, TCL, both of which I didn't use in any capacity, though easy to pick up. Later on I had projects in C# and Java, and I used a lot of my perl, bash, and python knowledge to write a lot of helper code that would have just been painful to write in Java. You have to be able to adapt and learn. Perhaps you won't be a super pro in a variety of languages, but as long as you are reasonably proficient in a few, that is good enough for a start.

Right now, my current job is exclusively C++, and the projects we work on actually do need to be high performing (simulation software), plus the people in my industry use C++, so my company does.

Though much of my supposed worth has been my ability to work in a mix of Unix and Linux environments.
 
In my travels as an intern I've gone from C (video production switchers) to Java (application server), and finally to .NET using C# (developer tools). My hobbyist projects right now are being done using PHP (Twitter app using self-written PHP wrapper... just because) and Objective-C (kicking around w/ iPod/Phone app development). Within every job I've had, the need has arisen to use some kind of interpreted language or O/S scripting language (Python, Perl, TCL, Batch scripting, shell scripting)

Learning language syntax, features, and limitations isn't terribly difficult once you've gotten some experience under your belt.

In answer to your question...

I find that Java's syntax, especially with regards to object oriented programming, is a little cleaner and easier to read than C++. From a very simple POV...

Code:
//C++
class Person {
public:
   Person();    
   ~Person();      //destructor method
  
   int eatFood();  //returns updated weight
   void doSleep();  
private:
   int height;
   int weight;
};
Person::Person()
{
   height=17;
   weight=5;
}

...doesn't seem as clean and readable as...

Code:
//Java
public class Person
{
   private int height;
   private int weight;

   public Person()
   {
      height=17;
      weight=5;
   }
   public int eatFood() { return 0; }
   public void doSleep() { //code }
}

...although I can certainly see the benefit of having the declaration and implementation of class methods being separated. This may just be a matter of preference.

Another key blocking point for me using C++ is interfaces, which are *very* beneficial where standardization is required. Suppose I had this Person class, and another class called Animal. I want both of them to use an interface called communicate, by which they can Speak() or Gesture().

Code:
//C++
class ICommunicate
{
   public:
      virtual ~ICommunicate() {  }
      virtual void Speak() = 0;
      virtual void Gesture() = 0;
};
class Animal: public ICommunicate 
{ 
   public:
      virtual void Speak() { } 
      virtual void Gesture() { }
};

versus

Code:
//Java
public interface Communicate
{
   void Speak();
   void Gesture();
}
public class Animal extends Communicate 
{
   public void Speak() { }
   public void Gesture() { }
}

I'm at a point where I understand both, but I must say that I prefer the verbosity of Java... that and the fact that I don't have to think about destructor methods. I still scratch my head a little bit when trying to comprehend why virtual destructors are needed for base classes and interfaces) but I think I have a fairly sound understanding.

OP, I hope none of that was over your head (it's fairly elementary... novice to intermediate stuff

I will now patiently await mikeblas (or another one of our resident gurus) to point out all the flaws in my POV :D
 
Please define 'powerful' in the context of a language.

C++ gives you a lot more flexibility & raw performance at the cost of complexity. Java gives you portability, the safety of garbage collection, a massive standard library and the whole J2EE ecosystem.

Since you're in college, you should probably focus on learning the language your curriculum is based on - however if it's Java based & you're an overachiever, you might want to learn C so you can learn pointers (it really makes the difference between call-by-value and call-by-reference make sense)

Like ambientZ said - if you really want to expand your horizons, learn something completely different. From a certain perspective, Java & C++ are pretty much the same. Take a look a different paradigms - true object oriented languages, scripting languages, functional programming, stack-based languages, Lisps & logical programming. Knowing these sorts of things will expand your horizons far more than worrying about how low-level you want your kinda-object-oriented incremental-improvement-on-C language to be.
 
I know what you mean Shadow. This has been something I have also pondered. Even as an IT major at FSU, I still have to take courses on both C++ and Java and I've wondered what the real advantages/disadvantages are to both sides.

This thread has been very helpful so far. :)
 
The STL is beautiful. J2SE is comprehensive.

Personally I prefer C++, and with a powerful library like Qt or .NET, it's really quite similar to Java in practice but you can do something about the over-engineered standard library.
 
Thanks for the replies everyone. I don't really have any real world experience but so far my class programming experience was:
Intro to programming - Python
Intro to C - C (durp!)
Intro to OO - Java
OO Programming - C++
Data Structures - C++ (What I am in now).
Computer Organization 1 - Assembly Programming (MIPS) (Also in right now)

I've aced all of my classes pretty much, and luckily I've had good professors thus far so that helps.
Also my school forces us to work from the unix standpoint (although I do my editing in gedit and compile via terminal g++ on my desktop (ubuntu)), which I'm sure will be helpful in the future. My next programming class is Databases and from what I hear they use C#, and I don't know shit about that so I should probably look into it.

Thanks for all the comments guys, looks like the consensus is to just learn it all, which I have no problem doing :)
 
I do my editing in gedit and compile via terminal g++ on my desktop (ubuntu))

Learn a Real Editor. I won't make this into a vim/emacs/GUI IDE flamewar by suggesting one, but if you plan on spending the rest of your life doing this, you owe it to yourself to learn a proper editor.

Just to illustrate this, the project I'm working on right has me reviewing a bunch of HTTP logs (headers + URLS + stuff), chopping it down to just the lines that start "http://..." and then splitting the URLs on their arguments into a readable format. With Vim, i can record a macro that does this..

Code:
qa  ## record macro "a"
g!/^http/d  #delete all lines that don't start with 'http'
%s/^http/^Mhttp/  # put a new-line before each line starting with http
%s/[&?]/^M^I/g  # replace all occurances of & or ? with a newline & a tab
gg  # go to top of file
dd  # ...and delete the empty line at the top
q  # stop recording

...which I can then fire off repeatedly by using '@a'.

edit: in that example it's not completely unambiguous which of those ^s are literal ^s and which mean control+character. It probably doesn't make much of a difference to anyone but me.
 
I have programmed in everything from QB to Java, C#, C/C++, PHP, Perl to Ada and I agree with those who said that the best thing you can do is learn or at least mess around with as many languages as possible.

Currently I only use PHP and C++ for a number of projects (webdev, simulation software and game dev), and find myself preferring a distinctly non-OOP approach in my code. I remember Java driving me often nuts with its hand-holding, while C/C++ give you all the rope you need and more, and Ada is like a teacher who is always looking over your shoulder and slapping you on the wrist if you even considered doing something the wrong way :)
 
No, it isn't.

It's a good start for most projects, though. I also appreciate not having to browse through 15 levels of documentation to figure out how to use a particular function call in a particular library function in C#. Now that was heaps of fun.
 
Thanks for the replies everyone. I don't really have any real world experience but so far my class programming experience was:
Intro to programming - Python
Intro to C - C (durp!)
Intro to OO - Java
OO Programming - C++
Data Structures - C++ (What I am in now).
Computer Organization 1 - Assembly Programming (MIPS) (Also in right now)

I've aced all of my classes pretty much, and luckily I've had good professors thus far so that helps.
Also my school forces us to work from the unix standpoint (although I do my editing in gedit and compile via terminal g++ on my desktop (ubuntu)), which I'm sure will be helpful in the future. My next programming class is Databases and from what I hear they use C#, and I don't know shit about that so I should probably look into it.

Thanks for all the comments guys, looks like the consensus is to just learn it all, which I have no problem doing :)

If you know Java you can learn C# in about 2 minutes
 
If you know Java you can learn C# in about 2 minutes

I wouldn't say I know java though, I just took a simple class in it about two years ago. I pick up languages quickly though so I don't think itll be a problem.


Learn a Real Editor. I won't make this into a vim/emacs/GUI IDE flamewar by suggesting one, but if you plan on spending the rest of your life doing this, you owe it to yourself to learn a proper editor.

Just to illustrate this, the project I'm working on right has me reviewing a bunch of HTTP logs (headers + URLS + stuff), chopping it down to just the lines that start "http://..." and then splitting the URLs on their arguments into a readable format. With Vim, i can record a macro that does this..

Code:
qa  ## record macro "a"
g!/^http/d  #delete all lines that don't start with 'http'
%s/^http/^Mhttp/  # put a new-line before each line starting with http
%s/[&?]/^M^I/g  # replace all occurances of & or ? with a newline & a tab
gg  # go to top of file
dd  # ...and delete the empty line at the top
q  # stop recording

...which I can then fire off repeatedly by using '@a'.

edit: in that example it's not completely unambiguous which of those ^s are literal ^s and which mean control+character. It probably doesn't make much of a difference to anyone but me.

Yeah I know I should use vim or emacs :( I know (or maybe knew, id have to refresh) most of the commands for emacs and vim but I like using my mouse for editing I guess. I can see how VIM has its advantages with that macro above, maybe I will start working in it :) Thanks for your suggestions everyone
 
...although I can certainly see the benefit of having the declaration and implementation of class methods being separated. This may just be a matter of preference.

If you have seperate declaration and implementation files you can release the header files with the declarations along with compiled library files and keep the details of your implementation to yourself.
 
No, it isn't.

mikeblas, why don't you like STL? I can guess it's due to performance.

If you don't like the STL, what do you like that does not involve reinventing the wheel? Some options I know of are the boost data structures and particularly the boost intrusive data structures that require no mallocs per item insertion.
 
Back
Top