views:

250

answers:

3

I have a need to learn the stuff that James Gosling took out of C/C++ to make Java.

For example:

  • The way you can't overload operators in Java.
  • The way you can't have multiple-inheritance in Java.
  • The way that dependencies are handled quite a bit differently in C++ than in Java.
+4  A: 

This is the first thing that comes to my mind: Comparison of Java and C++ (article on Wikipedia)

Some snippets from the Wikipedia page (core language differences):

Java and not C++:

  • Primitive data always passed by value
  • Standard documentation mechanism
  • Garbage collection
  • Multi-threading

C++ and not Java:

  • Function pointers
  • Unsigned arithmetic
  • Operator overloading
  • goto statement
Malcolm
I was shocked by "C++ | Allows both procedural programming and object-oriented programming." C++ is a multiparadigm language. At least the one not to forget is *generic programming*, since it is how the standard library is coded.
Ugo
Hmm, what would stop you from writing procedural code in Java? Just create only one class that has functions. You could have other classes that only hold data, which would then function like a C struct. I would think the result would then be indistinguishable from a C-style program. Am I missing something?
Jay
Java interfaces act as function pointers.
Longpoke
"Primitive data always passed by value" -> In java, **everything** is [passed by value](http://javadude.com/articles/passbyvalue.htm). Java does not have pass by reference.
FredOverflow
@Longpoke: Function pointers are based on structural typing, whereas interfaces are based on nominal typing. Quite different beasts.
FredOverflow
Yeah well I don't think the name "function pointers" implies structural typing. Maybe someone will read the post and think there is no way to do callbacks in Java :)
Longpoke
+8  A: 

I think Malcolm's answer is very useful. But beyond that, I'd like to point out that it's probably not correct to think about the Java design process in the way you're wording it.

While Gosling was of course aware of C++, you make it sound like he took C++ as a starting point and chopped stuff off to make Java. Conceptually, what he did, as I think I remember reading in his articles, was to design a language from the bottom up, throwing in all those features of other languages he considered useful.

Much of the syntax comes from C, and this helped keep Java's learning curve shallow for hordes of (former) C programmers. Consistent sizes for data types were something he considered useful for writing cross-platform code, and garbage collection was a big step toward alleviating the headaches of memory management. Single or multiple inheritance? That's a valid decision, and the simpler answer is single. But then to allow some tricks with data types, interfaces had to be thrown in. There, in a nutshell, is a lot of Java's design, mostly independent of C++.

Carl Smotricz
A: 

Adding to Malcolm:

You left out one of the biggest ones: Java is designed to be cross-platform compatible. To achieve this it uses interpreted bytecode rather than compiling to machine code, and contains numerous minor features to maintain compatiblity.

Off the top of my head:

Java Strings are dynamically resized. C/C++ strings (i.e. char arrays) have fixed size with major headaches about buffer overruns.

Java has built-in screen painting, i.e. features for putting buttons and dropdown boxes and so forth on the screen and manipulating them. C/C++ must use features provided by the OS, and which are therefore different between OSs. (See #1.)

Java has a built-in collections library.

Because C/C++ are truly compiled, you can do low-level things that you could not do with Java. For example, as far as I know there is no way with pure Java to talk to a USB port.

Etc.

To agree with Carl: Java is not "C++ with some things I don't like left out and some other stuff I wanted tossed in". While there are many obvious similarities between the two languages, there are also huge differences. Gosling and company obviously took a lot of inspiration from C++ when writing Java, but Java is not C++ version 2.0 by any stretch of the imagination.

Jay
There is no such thing as "C/C++ strings". C strings are `(const) char[n]`, whereas C++ strings are `std::string`.
FredOverflow
Well, C strings are arrays of characters. You can use the same kind of strings in C++, which is what I was referring to. Sure, you can use MFC strings, which are more like Java strings, sort of.
Jay