<< Chapter < Page Chapter >> Page >

All function or method definitions in Java are contained within a class definition. To a C++ programmer, they may look like inline function definitions, but they aren't. Java doesn't allow the programmer to request that a function be made inline, at least not directly.

Both C++ and Java support class (static) methods or functions that can be called without the requirement to instantiate an object of the class.

The interface keyword in Java is used to create the equivalence of an abstract base class containing only method declarations and constants. No variable data members or method definitions are allowed in a Java interface definition. (True abstract base classes can also be created in Java.) The interface concept is not supported by C++ but can probably be emulated.

Java does not support multiple class inheritance. To some extent, the interface feature provides the desirable features of multiple class inheritance to a Java program without some of the underlying problems.

While Java does not support multiple class inheritance, single inheritance in Java is similar to C++, but the manner in which you implement inheritance differs significantly, especially with respect to the use of constructors in the inheritance chain.

In addition to the access modifiers applied to individual members of a class, C++ allows you to provide an additional access modifier when inheriting from a class. This latter concept is not supported by Java.

Java does not support the goto statement (but goto is a reserved word) . However, it does support labeled break and continue statements, a feature not supported by C++. In certain restricted situations, labeled break and continue statements can be used where a goto statement might otherwise be used.

Java does not support operator overloading .

Java does not support automatic type conversions (except where guaranteed safe) .

Unlike C++, Java has a String type, and objects of this type are immutable (cannot be modified) . (Note, although I'm not certain, I believe that the equivalent of a Java String type was introduced into C++ sometime afterthe original publication of this document.)

Quoted strings are automatically converted into String objects in Java. Java also has a StringBuffer type. Objects of this type can be modified, and a variety of string manipulation methods are provided.

Unlike C++, Java provides true arrays as first-class objects. There is a length member, which tells you how big the array is. An exception is thrown if you attempt to access an array out of bounds. All arrays are instantiated in dynamic memory and assignment of one array to another is allowed. However, when you make such an assignment, you simply have two references to the same array. Changing the value of an element in the array using one of the references changes the value insofar as both references are concerned.

Unlike C++, having two "pointers" or references to the same object in dynamic memory is not necessarily a problem (but it can result in somewhat confusing results) . In Java, dynamic memory is reclaimed automatically, but is not reclaimed until all references to that memory become NULL or cease to exist. Therefore, unlike in C++, the allocated dynamic memory cannot become invalid for as long as it is being referenced by any reference variable.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask