How Java Differs From C And C++

  • 1.8K
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1.8K
    Shares

Although Java was modeled after C and C++ languages, it differs from C and C++ in many ways. Java does not i a number of features available in C and C++. For the benefit  of C and C++ programmers, we point out here a few major differences between C/C++ and Java language

How Java Differs From C:

Java and C

Java is not lot like C but the major difference between Java and C is that Java is and object-oriented language and has mechanism to define classes and objects. In an effort to build a simple and safe language, the Java team did not include some of the C features in Java.

  • Java does not include the C unique statement keywords sizeof, and typedef.
  • Java does not contain the data type struct and union.
  • Java does not define the type modifiers keywords auto,extern,register,signed, and unsigned.
  • Java does not support an explicit pointer type.
  • Java does not have a preprocessor and therefore we cannot use # define, # include, and # ifdef statements.
  • Java requires that the functions with no arguments must be declared with empty parenthesis and not with the void keyword as done in C.
  • Java adds new operators such as instanceof and >>>.
  • Java adds labelled break and continue statements.
  • Java adds many features required for object-oriented programming.

How Java Differs From C++ :

Java and C++

Java is a true object-oriented language while C++ is basically C with object-oriented extension. That is what exactly the increment operator ++ indicates. C++ has maintained backward compatibility with C. Is is therefore possible to write an old style C program and run it successfully under C++. Java appears to be similar to C++ when we consider only the “extensions” part of C++. However, some object -oriented features of C++ make the C++ code extremely difficult to follow and maintain.

Listed below are some major C++ features that were intentionally omitted from java or significantly modified.

  • Java does not support operator overloading.
  • Java does not have template classes as in C++.
  • Java does not support multiple inheritance of classes. This is accomplished using a new feature called “Interface”.
  • Java does not support global variables. Every variable and method is declared within classes and forms part of that class.
  • Java does not use pointers.
  • Java has replaced the destructor function with a finalize() function.
  • There are no header  files in Java.

Java also adds some new features. While C++ is a superset of C, Java is neither a superset nor a subset of C or C++. Java may be considered as a first cousin of C++ and a second cousin of C

The following two tabs change content below.

William Fieldhouse

I currently work full time as a penetration tester and have been involved within the IT security industry for over a decade. I also love to pioneer any forms of new technology and ideologies for future advancements. Feel free to contact me at [email protected]

William Fieldhouse

I currently work full time as a penetration tester and have been involved within the IT security industry for over a decade. I also love to pioneer any forms of new technology and ideologies for future advancements. Feel free to contact me at [email protected]

17 thoughts on “How Java Differs From C And C++

  • August 17, 2015 at 8:32 pm
    Permalink

    to AAKASH

    Java and PHP are kinda hard to compare mainly because PHP is used for websites and Java can be used for just about anything, but from what i understand a lot of big companies prefer Java to PHP for their backend stuff. To make a small website or a blog or something I’d use PHP, i guess that’s why wordpress and pivot-x are both written in PHP

    Reply
  • May 25, 2015 at 8:57 am
    Permalink

    The reason why Java is slower than C/C++ is because it runs on java virtual machine hence portability of apps whilst the latter run on machine directly hence not portable. However, running on some virtual machine means more processing time because the machine has now to communicate with the hardware unlike a c/c++ program which deals with h/w directly.. 🙂

    Reply
    • August 17, 2015 at 5:40 pm
      Permalink

      That’s true to some extend, but because of java “Just-In-Time” compilation feature it’s even faster than c/c++.

      Reply
      • August 18, 2015 at 5:07 am
        Permalink

        Please stop deceiving yourself that Java can be faster than C++ under any circumstances. JVM is not the real performance killer (although it used to be when Java was x20 slower than C++ back in the day). There are so many tricks about inlining and memory that an expertly tuned C++ program can only be rivaled by (much uglier) C code, the impossibility of performing those tricks in Java is the real performance (memory AND CPU) killer. I know a lot of Java and I know its weaknesses very well, but if you think JIT makes up for the difference between Java and C++ then you know nothing about C++.

        Reply
  • February 23, 2015 at 12:07 am
    Permalink

    This article should have more typos

    Reply
  • February 21, 2015 at 8:18 am
    Permalink

    Java is only suitable for application development; it’s far too abstract for low-level programming. While that abstraction makes it easier to write cross-platform applications it comes at a cost in both performance and efficiency. Java compiles to byte code not native machine code, and that byte code needs to be interpreted by a virtual machine every time it is executed. Java’s garbage collection also means you waste far more memory than you would with well-designed C++ resource handles that automatically release resources as soon as the handle falls from scope. Java encourages sloppy programming whereas C++ encourages good programming style.

    Reply
    • August 18, 2015 at 5:13 am
      Permalink

      Exactly! People are deceived that there’s no memory management in Java, but in a sufficiently complicated project there’s plenty. Say you have a component in some GUI, and you decide to get rid of it. How do you make sure that it does indeed get garbage collected? How do you know it’s not hanging around due to some listener holding a reference to it? The language provides you with no tools whatsoever to formalize ownership semantics. Beyond “if I’m pointing to it, it’s still alive” there’s no support and the resulting solution is no prettier than plain old C. Resource management can not be handled automatically by the language! It’s a non-trivial problem beyond the most trivial programs, and you need a language that helps you with it, not one that pretends to solve it while hiding it under the rug.

      Reply
  • February 20, 2015 at 6:24 pm
    Permalink

    Over a decade programming with all the three but this article sheds more light to what am already aware of. Need some more 🙂

    Voila!

    Reply
  • February 20, 2015 at 4:35 pm
    Permalink

    I think most important thing is that C++ differs in performance from java. C++ or C is way faster then java and more creative too.

    Reply
  • December 6, 2014 at 2:18 am
    Permalink

    Thanks alot, so generous of you quite informative as well.
    Need more guys like u.

    Reply
  • November 15, 2014 at 5:46 pm
    Permalink

    Thank for info…plz.. Sent info to crate user logine data base in websites

    Reply
  • November 7, 2014 at 6:10 am
    Permalink

    Thanks it helped me a lot. Finally got to know the real difference between JAVA and C. I will appreciate if you compare Java and PHP as well

    Reply
  • November 6, 2014 at 12:51 pm
    Permalink

    Thanks for the interesting differences!

    Reply
  • November 6, 2014 at 8:29 am
    Permalink

    Great updates given by ur portal, thats helps us lot. Thanks

    Reply

Leave a Reply