Java for C++ Developers - Other Miscellaneous Differences

[Start] Start No typedef

Java doesn't have a typedef keyword for aliasing types. Then again, it doesn't really have much need for it.

In C++, typedef No pre-processor

It's debatable whether this is a good or a bad thing. The debate has raged in earnest in the Java USENET news groups, with people feeling strongly for both points of view.

In C++, the main use of #include is to import class definitions for compiling, and Java has other ways of doing that. The use of #define for simple named constants is also handled in Java by final variables (Similar to const Classes can be "final" (no sub-classing)

In Java, if a class is declared final No variable-length parameter lists

Variable-length parameter lists put the burden of checking parameters and their types back on the developer. This is error-prone and could be a potential security problem. So Java doesn't allow them.

Some uses of variable-length parameter lists can be replaced by function overloading, but there's no simple way of handling printf No initialisers

C++ allows an initialiser list in a constructor for assigning the initial values of member variables and calling base class constructors.

Java doesn't have initialiser lists. You can call the base class constructor as the first line of code in the constructor, but member variables first take on their default values (which can, of course, be changed in the constructor body).

This would be a major problem in C++, because it would require all objects which are member variables to have parameterless constructors (And it would also call those constructors, when it may be preferable to call a different constructor).

But this isn't a problem in Java. Firstly, Java guarantees that member variables are initialised to known values. Secondly, the initial value for member variables which are objects (i.e. not primitive types) is null Guaranteed actions at function exit

Like C++, Java functions don't have to "fall off the end" to return. In addition to falling off the end, they can exit via an exception throw, an exception catch, or an explicit return statement. Unlike C++, the Java finally keyword can be used to specify a block of code which is always All data and functions belong in objects

Unlike C++, Java doesn't allow functions to stand by themselves - they must be member functions of objects. Similarly, there is no "global data" - all data must be member variables of objects.

This is nice and neat and consistent and "more object-oriented" (whatever that means), but doesn't have any earth-shattering consequences for development (as far as I can see).

[Start] Start