Photomk. Подвал

всякое о всяком

Archive for the ‘Java’ Category

Авг
17

Java difference from C/C++

Filed Under Java

  • No preprocessor

Java does not include a
preprocessor and does not define any analogs of the #define, #include,
and #ifdef directives. Constant definitions are replaced with static
final fields in Java. Macro definitions are not available in Java, but
advanced compiler technology and inlining has made them less useful.
Java does not require an #include directive because Java has no header
files. Java class files contain both the class API and the class
implementation, and the compiler reads API information from class files
as necessary. Java lacks any form of conditional compilation, but its
cross-platform portability means that this feature is rarely needed.

  • No global variables

Java defines a very clean namespace. Packages contain classes, classes
contain fileds and methods, and methods contain local variables. But
there are no global variables in Java, and, thus, there is no
possibility of namespace collisions among those variables.

  • Well-defined primitive type sizes

All the primitive types in Java have well-defined sizes. In C, the
size of short, int, and long types is platform-dependent, which hampers
portability.

  • No pointers

Java classes and
arrays are reference types, and references to objects and arrays are
akin to pointers in C. Unlike C pointers, however, references in Java
are entirely opaque. There is no way to convert a reference to a
primitive  type, and a reference cannot be incremented or decremented.
There is no address-of operator like &, dereference operator like *
or ->, or sizeof operator. Pointers are a notorious source of bugs.
Eliminating them simplifies the language and makes Java programs more
robust and secure.

  • Garbage collection

The
Java Virtual Machine performs garbage collection so that Java
programmers do not have to explicitly manage the memory used by all
objects and arrays. This feature eliminates another entire category of
common bugs and all but eliminates memory leaks from Java programs.

  • Variable declarations anywhere

C requires local variable declarations to be made at the beginning of a
method or block, while Java allows them anywhere in a method or block.
Many  programmers prefer to keep  all their variable declarations
grouped together at the top of a method, however.

  • Java does not include structures or unions because the class encompasses these other forms. It is redundant to include them.
  • Java does not support operator overloading.
  • Java does not include a preprocessor or support the preprocessor directives.
  • Java does not perform any automatic type conversions that result in a loss of precision.
  • All the code in a Java program is encapsulated within one or more
    classes. Therefore, Java does not have global variables or global
    functions.
  • Java does not support multiple inheritance.
  • Java does not support destructors, but rather, add the finalize() function.
  • Java does not have the delete operator.
  • The << and >> are not overloaded for I/O operations.
  • Java does not support templates.