Not sure where you got that from... the notion of a class being a "reference type", which is the foundation of Java, doesn't even exist in C++. Every class type is a value type in C++.
The only difference is that Java's subset also does not include pointer arithmetic, so this is actually a safe coding practice, because it allows for garbage collection to be performed.
Yes, C++ can emulate what Java is doing, but no, that's not what I'm saying. I'm saying C++ classes are not reference types, i.e., every class you define is always a value type, which is the exact opposite of Java. Obviously C++ also has pointers/references that could help you emulate Java, but that's beside the point. The point is that classes work differently in C++ and Java, and neither functionality is a subset of the other.
class MyClass_ { ... }
typedef MyClass_ *MyClass;
and you have identical "reference" semantics in C++. Of course you have to use `->` instead of `.`, but does it matter?Today, these decisions would have been different, as C++ no longer reigns supreme. I find that Kotlin has completely adopted all of Java's core values, but modernized the specific syntax and semantics. Kotlin is what Java would have been if it had been designed today. The fact that Kotlin has made interoperation so seamless (you can switch any single Java class with a Kotlin file in an existing project -- IntelliJ would even automatically translate it for you) makes transitioning from Java to Kotlin (if you want) so smooth and gradual that it's a tru pleasure (even working on a mixed codebase).
[1]: http://www.win.tue.nl/~evink/education/avp/pdf/feel-of-java....
OTOH - where are the named parameters in Java method calls?
The creators had Objective C in mind, but the result is so stripped down, it becomes an ink blot test for the viewer to see what they want :-)
I sure as hell don't see "Eiffel", though. JavaBeans are evil.
I learned Java and C++ at the same time. And I had a hard time doing it because I believed Sun's marketing department that Java was a better C++. When I finally realized that was a classic bit of puffery, and that the marketing was simply trying to make Java look powerful by association with C++, I was able to make some progress.
> Java is basically a subset of C++ with some sugar around.
Here are a few reasons why:
* As you said, all Java classes inherit from a common type; java.lang.Object (Smalltalk).
* Inner classes automatically have a reference to the containing class (not in C++, though Java "nested classes"[1] behave similar to C++ structs/classes).
* java.lang.ClassLoader has no corresponding abstraction in C++.
* C++ has pass-by-reference semantics which is completely missing in Java.
* C++ supports destructors intrinsically.
* All definitions must exist within a type in Java. There is no concept of a "free standing" definition.
* Java provides meta-objects by way of java.lang.Class for all classes (not present in C++).
* Java does not support stack-based allocation except for built-in POD's.
> Java interface is full-abstract class without fields (in Java 8 even not so full-abstract). Multiple inheritance only with interfaces.
A Java interface has a distinct type within the type system, yes, and there exist meta-objects describing them generated as well. But until Java 8, interfaces had no behaviour and could only declare static fields.
> So Java "reference" is C++ pointer without asterisks and ->.
Pretty much.
As an aside, it has always cracked me up that a language touted as having no pointers has a "NullPointerException" class. (I am easily amused)
1 - http://docs.oracle.com/javase/tutorial/java/javaOO/nested.ht...
You can limit yourself in C++ to inherit your objects from a common type. I believe that Qt did just that, for example. So this is a subset of C++.
> * Inner classes automatically have a reference to the containing class (not in C++, though Java "nested classes"[1] behave similar to C++ structs/classes).
That's a good example of simple sugar. Inner class just have pointer to the outer class and constructor with this parameter. Java compiler emits this code automatically. Yes, there's no such thing in C++, but it's very simple to emulate.
> * C++ has pass-by-reference semantics which is completely missing in Java. > * C++ supports destructors intrinsically. > * All definitions must exist within a type in Java. There is no concept of a "free standing" definition. > * Java does not support stack-based allocation except for built-in POD's.
Yes, Java is subset of C++ in that regard. C++ has more features which were stripped out.
> * java.lang.ClassLoader has no corresponding abstraction in C++. > * Java provides meta-objects by way of java.lang.Class for all classes (not present in C++).
Actually those systems could be emulated more or less in C++. Windows does have COM (and there are a lot of less known, but similar systems) which allows to dynamically load classes with virtual methods, etc. Qt implements (with some preprocessor magic) reflection for C++. But generally yes, C++ does not have any kind of runtime reflection (except dynamic_cast) and dynamic code loading, I agree here. What I want to point out is, that those features are provided by JVM. Java as a language does not have anything related to reflection or class loading. So probably it's not fair to compare Java + JVM to C++.
> As an aside, it has always cracked me up that a language touted as having no pointers has a "NullPointerException" class. (I am easily amused)
That's very unfortunate mess of terms. Pointers should be pointers, even if there's no pointer arithmetic in the language. Now we have pointers, references, pass-by-reference and beginners having hard time to understand what that even mean.
One of the wonderful things about C++[1] is that it can express just about anything which can be encoded in a program. GC, common base types, functional programming, imperative programming, object oriented programming, meta-programming, logic-based systems, dynamic module systems (a la Apache modules[2]), and a bunch more.
But that does not make a language such as Java a subset, unless your premise is that any language which can in some way be expressed in or embedded int C++ qualifies as a subset. If that's the case, then pretty much every language "is a subset of C++" (and yes, even Lisp[3]).
1 - Yes, I mean "wonderful things" as I have worked in C++ for many years. Still do when the problem calls for it.
More than Microsoft, I think Java killed Borland. Java had no IDE back in 95/96, but the compiler was free and it also ran on Linux, which Borland was late to adopt.
... then Anders went to work for Microsoft, and the rest is tragedy.
float a = 1.0; // compilation error in java, no problem in C++
For another: int a[5] = { 0, -1, 2, 3, 4 };
(1+a) = 1; // :)
Also: assert( !(1/2) );
Also: enum Foo {A, B};
enum Bar {C, D};
Foo e1 = A;
Bar e2 = C;
int i = 0;
assert(e1 == e2); // warning
assert(e1 == i); //no problem whatsoeverI will concede that the others are problematic...
*(1+a) = 1;
Still bad but not as bad. 1[a] = 1;Before then java wasn't so bad.
List doSth(Object a, Object b) { ... }Scala pretends to solve this problem by "layering" code. Complex types are for libraries and users should use libraries. But IMO it never worked. Problems arising, "users" have to debug into library code and they have to deal with full-featured types anyway.
My only real wish in regard to Java 1.4 was that I would be able to implicitly cast Object to anything (like I can in Objective C). That would help to reduce type clutter a lot.
From my perspective that's the large difference between Java and Scala. In Java, complexity leaks into the use-site (especially with Generics), in Scala it's possible to shield the user from that complexity.
So Java "reference" is C++ pointer without asterisks and ->. All java methods are virtual. Java interface is full-abstract class without fields (in Java 8 even not so full-abstract). Multiple inheritance only with interfaces. No generics (I learned Java 1.4), one common ancestor Object, friendly GC releasing you from the pain of memory management.
Basically the only new concept is package. Otherwise it's stripped C++ with different runtime library.