Java VS C/C++

Isuru Jayalath
LinkIT
Published in
4 min readSep 8, 2023

In this article, I’m going to discuss some key differences between Java and C/C++.

Photo by Juanjo Jaramillo on Unsplash
  1. C/C++ has a static memory allocation but Java has a dynamic memory allocation.

So what is the meaning of static and dynamic here? Static memory allocation means memory allocation happens at compilation time (c/c++). Dynamic means memory allocation happens during the runtime of the program (Java).

Artwork by Author

2.C/C++ are platform-dependent. Java is platform-independent.

Platform independent means we can write the source code on one operating system and execute it on another operating system (write once, run anywhere). As an example, if you write a Java program in the Windows operating system, you can run it in another OS (Linux or Mac OS) without changing the source code. Platform-dependent means compilation and execution should be done in the same operating system.

So why is Java platform independent while C/C++ is platform dependent? Because the Java program is not directly running on the computer. It is running on top of a machine called the JVM (Java virtual machine). Java source code is compiled into Java byte code. This byte code is platform-independent. (It is not dependent on the OS.) The JVM executes this byte code into an .exe file. Here, the JVM is platform-dependent. Since this byte code is platform-independent, we say Java is platform-independent.

But in c/c++ Source code is directly compiled into an.exe file. There is no intermediate file, like in Java. The compiler directly converts it into an OS-specific.exe file.

3. C++ supports multiple inheritance, but Java does not support multiple inheritance.

Inheritance is one of the main concepts in object-oriented programming. C follows the procedure-oriented programming paradigm, and C++ and Java follow the object-oriented programming paradigm. Since C follows a procedure-oriented programming paradigm, we cannot talk about inheritance in C. So simply, inheritance means passing the properties( variables and methods) of a superclass to its subclass.

So why doesn't Java support multiple inheritance? Let’s see this example.

public class A {
// variables
int number1 = 10;
int number2 = 20;

// Method
public void displayNumber() {
System.out.println("Summation is: " + number1 + number2);
}
}
public class B {
// variable
int number1 = 10;
int number2 = 20;

// Method
public void displayNumber() {
System.out.println("The division is: " + number1/number2);
}
}
public class C extends A,B {
}

If you try to inherit a single subclass from multiple superclasses, that is called multiple inheritance. This is not possible in Java because, as in this example, if super classes have the same method as displayNumber(), there will be an ambiguity about which method should be inherited by the subclass. Java language developers needed to reduce the complexity and simplify the language. That’s why Java does not support multiple inheritance.

4. C/C++ follow both call-by-value and call-by-reference parameter passing mechanisms. Java follows a call-by-value parameter-passing mechanism only.

a) call by value in c

#include <stdio.h>

// Function that uses call by value
void increment(int num) {
num = num + 1;
}

int main() {
int x = 5;
printf("Before increment: x = %d\n", x);
increment(x);
printf("After increment: x = %d\n", x);
return 0;
}

Here output is

Before increment: x = 5
After increment: x = 5

b) call by reference in c

#include <stdio.h>

// Function that uses call by reference
void incrementByReference(int *num) {
*num = *num + 1;
}

int main() {
int x = 5;
printf("Before increment: x = %d\n", x);
incrementByReference(&x);
printf("After increment: x = %d\n", x);
return 0;
}

Here output is

Before increment: x = 5
After increment: x = 6

Here we pass a pointer variable to the incrementByReference() Function. That means we are passing a pointer variable to the function, and it is not just a normal variable. Then, inside the function, an increment by one happens to the variable x inside the main method. But in example a), incrementing is happening to a local variable called num in the increment() function.

c) call by value in Java

public class CallByValueExample {

// Method that uses call by value
public static void increment(int num) {
num = num + 1;
System.out.println("Inside method: num = " + num);
}

public static void main(String[] args) {
int x = 5;
System.out.println("Before increment: x = " + x);
increment(x);
System.out.println("After increment: x = " + x);
}
}

Output is

Before increment: x = 5
Inside method: num = 6
After increment: x = 5

When we pass a parameter to a method in Java, we are passing a copy of the value of the actual argument. Any modifications made to the parameter within the method do not affect the original argument.

Then why doesn’t Java use the call-by-reference parameter passing mechanism? Because in Java, they do not use the concept of pointers. Even though there are reference variables in java, it stores the reference of the object but java restrict direct access memory address. So there are no pointer variables. Because of that, there is no way to store the actual memory location of a variable in Java.

I hope you have some ideas about Java and C/C++. Thank you for reading.

--

--

Isuru Jayalath
LinkIT
Writer for

Undergraduate of University of Moratuwa, Faculty of Information Technology.