This is a comparison between Java and C, two prominent object-oriented programming languages.
Video Comparison of Java and C++
Design goal
The difference between C and Java programming languages ââcan be traced to their inheritance, as they have different design goals.
The C language is designed for system and programming applications (a.k.a., infrastructure programming), extending the C procedural programming language, designed for efficient execution. For C, C adds support for object-oriented programming, exception handling, life-based resource management (RAII), generic programming, metaprogramming templates, and C Standard Library that includes generic containers and algorithms (STL), and many other general-purpose facilities.
Java is a common object-oriented programming language, concurrent, class-based, designed to minimize implementation dependencies. It relies on a Java virtual machine to be secure and highly portable. It's bundled with an extensive library designed to provide a full abstraction of the underlying platform. Java is a statically typed object-oriented language that uses a similar (but incompatible) syntax with C. This includes a documentation system called Javadoc.
Both C and Java provide facilities for generic, template and generic programming, respectively. Although they were created to solve similar problems, and have similar syntax, they are very different.
Miscellaneous
- Java and C use different ways to split the code into multiple source files. Java uses a package system that specifies file names and paths for all program definitions. The compiler imports executable class files. C uses the source file system inclusion header file to share declarations between the source files.
- The compiled Java code file is generally smaller than the code file in C because Java bytecode is usually more compact than the original machine code and the Java program is never statically connected.
- C compiles additional preprocessing phase text features, while Java does not. So some users add preprocessing phase to their build process to support better conditional compilation.
- The division operator and Java modulus are well defined to cut to zero. C (pre-C 11) does not specify whether this operator cuts to zero or "cuts to -infinity". -3/2 will always -1 in Java and C 11, but C 03 compiler can return -1 or -2, depending on the platform. C99 defines the division in the same way as Java and C 11. Both languages ââguarantee (where a and b are integer types) that
(a/b) * b (a% b) == a
for all a and b (bÃ,! = 0). Version C 03 will sometimes be faster, as it is allowed to choose which of the original cutting mode of the processor. - The size of the integer type is defined in Java (int is 32-bit, 64-bit length), whereas in C integer and pointer sizes the compiler and binary interface (ABI) applications depend on the given constraints. So Java programs will have consistent behavior across platforms, while C programs may require customization for multiple platforms, but can run faster with more natural integer sizes for local platforms.
An example of comparing C and Java is in Wikibooks.
Maps Comparison of Java and C++
Performance
In addition to running compiled Java programs, computers running Java applications generally have to run Java virtual machines (JVMs), while compiled C programs can be run without external applications. Early versions of Java were significantly outperformed by statically compiled languages ââlike C. This is because the program statements of these two closely related languages ââcan be compiled into several machine instructions with C, while compiling into some byte code that involves several machine instructions each when interpreted by the JVM. As an example:
Because performance optimization is a very complex issue, it is very difficult to measure performance differences between C and Java in general, and most benchmarks are unreliable and biased. Given the very different nature of language, definitive qualitative differences are also difficult to describe. In short, there is an inherent inefficiency and a hard limit on optimization in Java, given that it relies heavily on flexible high level abstractions, however, the use of powerful JIT compilers (as in modern JVM implementations) can reduce some problems. In any case, if the Java inefficiency is too large, compiled C or C code can be called from Java via JNI.
Some inefficiencies inherent in the Java language include, especially:
- All objects are allocated on the heap. For functions that use small objects, this can cause performance degradation and fragmentation of the stack, while pile allocation, on the contrary, costs are essentially zero. However, modern JIT compilers reduce this problem to some extent by runaway analysis or escape detection to allocate objects on the stack, since Oracle JDK 6.
- Key performance projects such as efficient database systems and messaging libraries must use unofficial internal APIs such as
sun.misc.Unsafe
to gain access to manual resource management and can perform pile allocations; manipulating pseudo-pointers effectively. - The method is virtual by default (though it can be made final), usually leading to abuse of virtual methods, adding a deceit level on every call. It also slightly increases memory usage by adding one pointer per object to a virtual table. It also induces a performance start-up penalty, because the JIT compiler must perform additional optimizations to demystify the small functionality.
- Many run-time casting required even using standard containers induces performance penalties. However, most of these casts are statically eliminated by JIT compilers.
- The security guarantee comes with a running time fee. For example, the compiler is required to place an appropriate range check within the code. Keeps any array access with inefficient range checks, so most JIT compilers will try to eliminate it statically or by moving it out of the inner loop (though most of the original compilers for C will do the same when optional distance checks are used).
- Lack of access to low-level details prevents developers from increasing programs where the compiler can not do it.
- The use of semantic-mandatory reference for all user-defined types in Java can introduce a large number of excessive memory changes (or jumps) (unless preceded by the JIT compiler) which can cause frequent cache errors (aka cache thrashing ). Additionally, cache optimization, typically through the structure or algorithm of the cache-aware or unknown data, can often result in great performance improvements and also avoid the degeneration of time complexity that is characteristic of many cache sweep algorithms, and therefore one form of optimization the most important; semantic references, as mandated in Java, make such optimizations impossible to manifest in practice (either by JIT programmers or compilers).
- Garbage collection, because this form of automatic memory management introduces memory costs.
However, there are a number of benefits to Java design, some realize, some just theorize:
- Java garbage collection may have better cache coherence than the traditional malloc / new use for memory allocation. However, there is an argument that the two allocators also fragment the stack and do not show better cache locality. However, in C, the allocation of a single object on a heap is rare, and a large number of single objects are typically allocated in blocks via a STL container and/or by a small object allocator.
- Run-time compilation potentially uses information about the platform on which the code is run to improve the code more effectively. However, most of the original state-of-the-art (C, C, etc.) compilers generate multiple code paths to use the full computing capabilities of a given system. In addition, the inverse argument can be made that native compilers can exploit optimizations and specific set of architectural instructions that are more specific than JVM multi-platform distributions.
- Run-time compilation allows for inlining more aggressive virtual functions than is possible for static compilers, because JIT compilers have more information about all possible virtual call targets, even if they are in different dynamically loaded modules. Currently available JVM implementations have no problem in connecting most monomorphics, mostly morphorphic and dimorphic calls, and ongoing research for inline and megamorphic calls, thanks to the added dynamic enhancements recently added in Java 7. The insertion may allow for further optimization such as loop vectorisation or unrolling loop, resulting in a large overall performance improvement.
- In Java, thread synchronization is built into the language, so a JIT compiler can potentially, through escape analysis, opening lock, significantly improves the performance of naive multi-threaded code. This method was introduced in Sun JDK 6 update 10 and was given bias locking.
Also, some performance issues occur in C:
- Allowing pointers to point to any address can make optimal difficulty because of possible interference between pointers with each other alias. However, the introduction of strict-aliasing rules mostly solves this problem.
- Since code generated from multiple instantiations of the same class template in C is not shared (as with generic deleted types in Java), excessive use of templates may cause a significant increase in executable code size (bloat code). However, because the template functions inlined aggressively, they can sometimes reduce code size, but more importantly allow for more aggressive static analysis and optimize code by the compiler, more often making it more efficient than non-templated code. In contrast, generic Java is certainly less efficient than non-generic code.
- Because in traditional C compilers, dynamic linking is done after code generation and optimization in C, function calls that include various dynamic modules can not be underlined. However, modern C compilers like MSVC and Clang LLVM offer a code-time-code-making option that allows the module to be compiled into an intermediate format that allows inlining at the final link stage.
- Since thread support is generally provided by libraries in C, C compiler can not perform thread-related optimizations. However, since the multi-threading memory model was introduced in C 11, modern compilers have the language features necessary to implement the optimization. In addition, many compiler optimizers, such as Intel compilers, provide multiple language extensions and advanced threading facilities for professional multi-threading development.
Official language standard and reference
Language specifications
The C language is defined by ISO/IEC 14882 , ISO standards, issued by the committee ISO/IEC JTC1/SC22/WG21 . The latest post-standardization draft of C 11 is also available.
Language C evolves through an open steering committee called the C Standard Committee. The committee consists of creator C Bjarne Stroustrup, organizer Herb Sutter, and other prominent figures, including many industry representatives and user groups (ie shareholders). Being an open committee, anyone is free to join, participate, and contribute proposals for future releases of standard and technical specifications. The Committee now aims to issue new standards every few years, although in the past the process and rigorous review discussions meant longer delays between the publication of new standards (1998, 2003, and 2011).
The Java language is defined by Java Language Specification , a book published by Oracle.
Java language continues to evolve through a process called Java Community Process, and the world's programming community is represented by a group of people and organizations - members of the Java Community - who are actively involved in language upgrading, by sending a public request - a Java Specification Request - which must pass a formal review and public before they are integrated into the language.
The lack of a clear standard for Java and the more volatile nature of its specifications has been a constant source of criticism by stakeholders who want more stability and conservatism in the addition of new language and library features. On the contrary, Committee C also receives continuous criticism, for the opposite reason, that is, too tight and conservative, and too long to release a new version.
Trademark
"C" is not a trademark of any company or organization and is not owned by an individual. "Java" is a trademark of Oracle Corporation.
References
External links
- Object Oriented Memory Management: Java vs. C
- Chapter 2: How Different from Java C, chapter of Java in Brief by David Flanagan
- Java vs. C Comparative resource management - Comprehensive paper with examples
- Java vs C performance... again... - In-depth discussion of performance differences between Java and C/C
- Hyperpoly - Java and C Comparison
Source of the article : Wikipedia