In: Computer Science
Your task is to write an article describing the differences you have found or noticed so far between C++ and Java.
First you should go through the W3schools C++ tutorials as described in the week’s description, taking some notes about the differences between C++ and Java as you do so. Then, you should write your document.
Your document does not need to be long, but it should be well-written. Imagine that you are writing this for Java programmers who want to know about C++. You will be telling them about some of the differences between the two languages that you noticed as you began to learn about C++.
It would best to submit your article as a Word. Be sure to include your name and a title at the top of the article, much like we do with identifying documentation when programming, no specific format is required, as long as the information is there and your document doesn’t look too bad.
Practice writing about topics in Computer Science is important for students studying the discipline.
Java : Java programming language was developed by Sun Microsystems by James Gosling and released in 1995 as core component of Sun Microsystems Java platform. With the advancement of Java and its widespread popularity, multiple configurations were built to suit various types of platforms. Java is guaranteed to be Write Once, Run Anywhere.
C++ : C++ is a powerful general-purpose programming language. It can be used to develop operating systems, browsers, games, and so on. C++ supports different ways of programming like procedural, object-oriented, functional, and so on. All this makes C++ powerful as well as flexible. A general purpose programming language created as an extension of C language and can be said as C language with classes is called C++. The language offers high level control for system and memory and as a cross platform language develops high performance applications.
I will list down some basic differences first before going into the other smaller differences:
Now some more in-depth differences:
Class vs. Object
From the name, it seems like object-oriented programming is all about objects. But when we write an object-oriented program, our code specifies something called a class and both C++ and Java have class as a keyword but object is not a keyword in either of them. The concepts of class and object are connected at a very fundamental level but the two concepts are distinct.
The most common example used to show the relationship between an object and a class is of a cookie and a cookie shaper. A class is like a cookie shaper which describes the shape and the size of a cookie but it is inedible and does not consist of any cookie ingredients. Cookies are like objects. Once the cookie dough is made and rolled out, we can shape them using the cookie shaper. The shape and size of every cookie will be identical.
Similarly, a class is a description. At a high level a class describes three features of the objects that are created or instantiated from the class:
General Method and Function Calls:
Let's suppose that the model class has a method or function called display. C++ has two ways of creating an object, which requires two ways to use the object, which in turn requires an operator not found in Java. C++ has the arrow operator which is used whenever the asterisk appears during object instantiation. Both languages use the dot operator to make the call. Concept of code using asterisk and the arrow operator is a feature called pointers.
mymodel1.display(); // Java
mymodel2->display(); // C++
mymodel3.display(); // C++, looks like Java
Arrays:
Java implements arrays as objects whereas C++ treats them as a primitive data type.
Functionally, an array is a collection of variables which are accessed through a common name. Individual variables or elements of the array are selected by an index or a subscript. Arrays have a size, which is determined when they are created.
Arrays in Java have a length attribute, which is possible because they are considered as objects. However, C++ arrays are not objects and do not have a length attribute.
Generally Java is more robust than C++ due to the following:
Some more differences:
Due to the fact that as Java program runs on a virtual environment (Java Virtual Machine), programmers cannot access deeply.
Generally, Java can be just as fast or faster because the JIT compiler that can make optimizations that a C++ compiled program cannot because it can query the machine.
A C++ program has to be compiled beforehand usually with mixed optimizations so that it runs decently well on all machines, but is not optimized as much as it could be for a single configuration.
Java does heap allocations more efficiently than C++ because of the layer of abstraction between the garbage collector and the code which allows it to do all heap compression at once