Home Java Java-tips Background Books For C++ programmers

Ask Questions?

View Latest Questions

Advertisement


 
 

For C++ programmers
Posted on: July 26, 2006 at 12:00 AM
Java inherited many features from C++, but some of the important differences are mentioned below.

Java Notes

For C++ programmers

Java inherited many features from C++, but some of the important differences are mentioned below.

  1. Case. Class names always start with an upper case character, Methods (called functions in C++) start with a lower case character.
  2. File names, directories. Source file names must be exactly the same name (including case) as the class, with the extension ".java". Generated object file names will have the class name followed by ".class". Many class files can be compressed into a "Java archive" with the file extension ".jar". A package is a way to group classes together and corresponds to a directory in the file system. There are no header (.h) files in Java.
  3. No header files. import is the rough equivalent of C++'s #include, but it doesn't include source code - it simply tells which .class/.jar files can be searched for symbol definitions.
  4. .class and JIT. The .class files represent Java programs in a machine independent way (called Java Byte Code for the Java Virtual Machine) that is mostly, but not quite, specific machine object code. When you run a Java program a Just In Time (JIT) compiler translates the Java Byte Code to machine instructions for your CPU. This accounts for the annoying delay when starting Java programs.
  5. No preprocessor. There are no #defines, typedefs, conditional compilation, etc.
  6. Everything in classes. All functions (called methods in Java) and variables must be defined in some class. There are no structs or unions. Java uses the superior idea of "interface" in place of multiple inheritance.
  7. References, not pointers. Java has references, which are pointers, but the name change is intended to show that you can't use them as C++ pointers. There are no address of(&), dereference(*), or arithmetic operators - these operations are basically automatic.
  8. Only primitives and references. Variables can contain only primitive values (long, int, short, byte, char, float, double, boolean) or object references. Variable do not contain object values. The biggest possible variable is therefore only 8-bytes (long). All arrays, strings, and other objects must be dynamically allocated on the heap.
  9. No bad references. It's impossible to reference uninitialized memory (altho references can be null).
  10. Garbage collection. There is no delete to deallocate objects. When there is no longer a reference to an object, it is eligible for automatic garbage collection when memory is needed. This is one of the most wonderfully profound improvements Java made over C++.
  11. Unicode. Type char is two bytes containing a Unicode character: first 128 characters same as ASCII, first 256 same as ISO 8859-1 (Latin-1), remainder encode basically all modern human scripts.
  12. boolean is the only type that can be used where conditional expressions are expected (eg, in an if statement). No more zero vs non-zero tests.
  13. String. You can't modify a String, only create a new one. Use StringBuilder or StringBuffer for something that changes.
  14. No operator overloading. All operations in classes are implemented as methods. This can make data structure operations look a little awkward.
  15. Data structures. Java's Collections libraries correspond approximately to what's in C++'s Standard Template Library (STL). Java 5 introduced generics, which are similar to C++ templates. The biggest shock is that Java's data structure libraries only deal with object types, not primitives. Java's ArrayList<T> is about the same as C++'s Vector<T>.
  16. Libraries. The Java libraries are astounding more extensive than the standard C++ libraries. They predefine over 3,000 classes you can use.

References

Copyleft 2006 Fred Swartz MIT License
Advertisement


DMCA.com