You are viewing our Forum Archives. To view or take place in current topics click here.
The Difference From C++ and Java
Posted:

The Difference From C++ and JavaPosted:

Slippery
  • Ladder Climber
Status: Offline
Joined: Feb 19, 201212Year Member
Posts: 348
Reputation Power: 14
Status: Offline
Joined: Feb 19, 201212Year Member
Posts: 348
Reputation Power: 14
Please Note That much Of this information is From other websites and Sources.

The differences between the C++ and Java programming languages can be traced to their heritage, as they have different design goals.
C++ was designed for systems and applications programming, extending the C programming language. To this procedural programming language designed for efficient execution, C++ has added support for statically-typed object-oriented programming, exception handling, scoped resource management, and generic programming, in particular. It also added a standard library which includes generic containers and algorithms.

Java was created initially as an interpreter for printing systems but grew to support network computing. Sun Microsystems used it for the basis of their "HotJava" thin client system. It relies on a virtual machine to be secure and highly portable. It is bundled with an extensive library designed to provide a complete abstraction of the underlying platform. Java is a statically-typed object-oriented language that uses a syntax similar to C++, but is not compatible with it. It was designed from scratch with the goal of being easy to use and accessible to a wider audience.

The different goals in the development of C++ and Java resulted in different principles and design trade-offs between the languages.

Syntax
Java syntax has a context-free grammar that can be parsed by a simple LALR parser. Parsing C++ is more complicated; for example, Foo<1>(3); is a sequence of comparisons if Foo is a variable, but it creates an object if Foo is the name of a class template.
C++ allows namespace-level constants, variables, and functions. In Java, such entities must belong to some given type, and therefore must be defined inside a type definition - either a class, or an interface.
In C++, objects are values, while in Java they are not. C++ uses value semantics by default, while Java always uses reference semantics. To achieve reference semantics in C++, either a pointer or a reference can be used.

C++:
class Foo {          // Declares class Foo
public:
    int x;           // Member variable
 
    Foo(): x(0) {}   //  Constructor for Foo; initializes
                     //  x to 0. If the initializer were
                     //  omitted, the variable would not
                     //  be initialized to a specific
                     //  value.
 
    int bar(int i) { // Member function bar()
        return 3*i + x;
    }
};


Java:
class Foo {               // Defines class Foo
    public int x;         // Member variable,
                          //initialized to 0 by default
 
    public Foo() {        // Constructor for Foo
    }
 
    public int bar(int i) {// Member method bar()
        return 3*i + x;
    }
}


Runtime
C++ is normally compiled directly to machine code which is then executed directly by the operating system. Java is normally compiled to byte-code which the Java virtual machine (JVM) then either interprets or JIT compiles to machine code and then executes.
Due to its unconstrained expressiveness, low level C++ language features (e.g. unchecked array access, raw pointers, type punning) cannot be reliably checked at compile-time or without overhead at run-time. Related programming errors can lead to low-level buffer overflows and segmentation faults. The Standard Template Library provides higher-level abstractions (like vector, list and map) to help avoid such errors. In Java, low level errors either cannot occur or are detected by the JVM and reported to the application in the form of an exception.
The Java language requires specific behavior in the case of an out-of-bounds array access, which generally requires bounds checking of array accesses. This eliminates a possible source of instability but usually at the cost of slowing down execution. In some cases, compiler analysis can prove a bounds check unnecessary and eliminate it. C++ has no required behavior for out-of-bounds access of native arrays, thus requiring no bounds checking for native arrays. C++ standard library collections like std::vector, however, offer optional bounds checking. In summary, Java arrays are "always safe; severely constrained; always have overhead" while C++ native arrays "have optional overhead; are completely unconstrained; are potentially unsafe."

Preformance
In addition to running a compiled Java program, computers running Java applications generally must also run the Java virtual machine (JVM), while compiled C++ programs can be run without external applications. Early versions of Java were significantly outperformed by statically compiled languages such as C++. This is because the program statements of these two closely related languages may compile to a few machine instructions with C++, while compiling into several byte codes involving several machine instructions each when interpreted by a JVM. For example:




Certain inefficiencies are inherent to the Java language itself, primarily:
All objects are allocated on the heap. For functions using small objects this can result in performance degradation as stack allocation, in contrast, costs essentially zero. However, this advantage is obsoleted by modern JIT compilers utilising escape analysis or escape detection to allocate objects on the stack. Escape analysis was introduced in Oracle JDK 6.
Methods are virtual by default. This slightly increases memory usage by adding a single pointer to a virtual table per each object. It also induces a startup performance penalty, since a JIT compiler must perform additional optimization passes even for de-virtualization of small functions.
A lot of casting required even using standard containers induces a performance penalty. However, most of these casts are statically eliminated by the JIT compiler, and the casts that remain in the code usually do not cost more than a single CPU cycle on modern processors, thanks to branch prediction.
Array access must be safe. The compiler is required to put appropriate range checks in the code. The naive approach of guarding each array access with a range check is not efficient, so most JIT compilers generate range check instructions only if they cannot statically prove the array access is safe. Even if all runtime range checks cannot be statically elided, JIT compilers try to move them out of inner loops to make the performance degradation as low as possible.
Lack of access to low-level details prevents the developer from improving the program where the compiler is unable to do so.[9] Programmers can interface with the OS directly by providing code in C or C++ and calling that code from Java by means of JNI.

In contrast, various optimizations in C++ are either too difficult or impractical to implement:
Pointers make optimization difficult since they may point to arbitrary data. However, in some cases this is obsoleted as new compilers introduced a strict-aliasing rule[10] and because of support of the C99 keyword restrict.[11]
Java garbage collection may have better cache coherence than the usual usage of malloc/new for memory allocation, as its allocations are generally made sequentially. Nevertheless, arguments exist[weasel words] that both allocators equally fragment the heap and neither exhibits better cache locality.
Due to the lack of garbage collection in C++, programmers must supply their own memory management code, often in the form of reference-counted smart pointers.
Since the code generated from various concretisations of the same templated class in C++ is not shared, excessive use of templates may lead to significant increase of the executable code size.
Run-time compilation can potentially use additional information available at run-time to improve code more effectively, such as the processor on which the code will be executed. However, this claim is effectively made obsolete as most state-of-the-art C++ compilers generate multiple code paths to employ the full computational abilities of the given system[12]
Run-time compilation allows for more aggressive virtual function inlining than is possible for a static compiler, because the JIT compiler has complete information about all possible targets of the virtual call, even if they are in different dynamically loaded modules. Currently available JVM implementations have no problem in inlining most of the monomorphic, mostly monomorphic and dimorphic calls, and research is in progress to inline also megamorphic calls, thanks to the recent invoke dynamic enhancements added in Java 7.[13] Inlining can allow for further optimisations like loop vectorisation or loop unrolling, resulting in a huge overall performance increase.
Because dynamic linking is performed after code generation and optimisation in C++, function calls spanning different dynamic modules cannot be inlined.
Because thread support is provided by libraries in C++, C++ compilers have no chance to perform thread-related optimisations. In Java, thread synchronisation is built into the language, so the JIT compiler can, with the help of escape analysis, easily elide or coarse locks,[clarification needed] significantly improving performance of multithreaded code. This technique was introduced in Sun JDK 6 update 10 and is named biased locking.[
#2. Posted:
CRACKbomber
  • Resident Elite
Status: Offline
Joined: Oct 23, 201013Year Member
Posts: 243
Reputation Power: 15
Status: Offline
Joined: Oct 23, 201013Year Member
Posts: 243
Reputation Power: 15
Now write a thread about the difference between ctrl+c and ctrl+v vs actually writting a thread.
#3. Posted:
Slippery
  • Ladder Climber
Status: Offline
Joined: Feb 19, 201212Year Member
Posts: 348
Reputation Power: 14
Status: Offline
Joined: Feb 19, 201212Year Member
Posts: 348
Reputation Power: 14
I explained that these werent from me. This was to help TTg users understand the difference from c++ and java
#4. Posted:
MLP
  • TTG Contender
Status: Offline
Joined: Oct 26, 201013Year Member
Posts: 3,869
Reputation Power: 177
Status: Offline
Joined: Oct 26, 201013Year Member
Posts: 3,869
Reputation Power: 177
Slippery wrote I explained that these werent from me. This was to help TTg users understand the difference from c++ and java


I thought they were different though, seeing as they are two different languages.
#5. Posted:
99rock99
  • Resident Elite
Status: Offline
Joined: May 22, 201113Year Member
Posts: 235
Reputation Power: 9
Status: Offline
Joined: May 22, 201113Year Member
Posts: 235
Reputation Power: 9
you forgot to mention since Java compiles to a binary tode wich the JVM can read, it can be run on all platforms that have java installed while c++, which compiles to code which the OS can read can only be run on that OS.
#6. Posted:
CoNdEmR
  • Shoutbox Hero
Status: Offline
Joined: Apr 29, 200915Year Member
Posts: 4,420
Reputation Power: 1211
Status: Offline
Joined: Apr 29, 200915Year Member
Posts: 4,420
Reputation Power: 1211
When it is clearly evident that you are copying and pasting someone else's written work, do them the courtesy of referencing the original source and don't be an all round dickhead.
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.