There are lots of features missing in java that is available in C++.
- Java has no Pointer.
//In C++, a pointer refers to a variable that holds the address of another variable. //Syntax: datatype *variable_name; #include <iostream> using namespace std; int main() { int x = 27; int *ip; ip = &x; cout << "Value of x is : "; cout << x << endl; cout << "Value of ip is : "; cout << ip<< endl; cout << "Value of *ip is : "; cout << *ip << endl; return 0; } //So we can't write this code on java.
So we can’t write this above code on java.
- Java has no sizeof() operator.
//Syntax: sizeof (data type) #include <iostream> using namespace std; int main() { cout << "Size of char : " << sizeof(char) << endl; cout << "Size of int : " << sizeof(int) << endl; return 0; } //So this code didn't write in Java.
- Java has no Scope Resolution Operator (: 🙂
// C++ program to show that we can access a global variable // using scope resolution operator :: when there is a local // variable with same name #include<iostream> using namespace std; int x; // Global x int main() { int x = 10; // Local x cout << "Value of global x is " << ::x; cout << "\nValue of local x is " << x; return 0; } //So, Java didn't support this code.
- Local variables in functions can’t be static.
//In C++ local variables are static but Java Not. See below example #include <iostream> using namespace std; void test() { // var is a static variable static int var = 0; ++var; cout << var << endl; } int main() { test(); test(); return 0; }
- In Java, there are no multiple inheritances.
//In C++ multiple inheritance supports through "virtual" keyword but Java didn't. lets' see #include<iostream> using namespace std; class Person { public: Person(int x) { cout << "Person::Person(int ) called" << endl; } Person() { cout << "Person::Person() called" << endl; } }; class Faculty : virtual public Person { public: Faculty(int x):Person(x) { cout<<"Faculty::Faculty(int ) called"<< endl; } }; class Student : virtual public Person { public: Student(int x):Person(x) { cout<<"Student::Student(int ) called"<< endl; } }; class TA : public Faculty, public Student { public: TA(int x):Student(x), Faculty(x) { cout<<"TA::TA(int ) called"<< endl; } }; int main() { TA ta1(30); } /* In the above program, constructor of 'Person' is called once. One important thing to note in the above output is, the default constructor of 'Person' is called. When we use 'virtual' keyword, the default constructor of grandparent class is called by default even if the parent classes explicitly call parameterized constructor. */
//Output
Person::Person() called
Faculty::Faculty(int ) called
Student::Student(int ) called
TA::TA(int ) called
- In Java no operator overloading.
//In C++ Operator Overloading is possible #include<iostream> using namespace std; class Complex { private: int real, imag; public: Complex(int r = 0, int i =0) {real = r; imag = i;} // This is automatically called when '+' is used with // between two Complex objects Complex operator + (Complex const &obj) { Complex res; res.real = real + obj.real; res.imag = imag + obj.imag; return res; } void print() { cout << real << " + i" << imag << endl; } }; int main() { Complex c1(10, 5), c2(2, 4); Complex c3 = c1 + c2; // An example call to "operator+" c3.print(); } //Output: 12 + i9 //But In Java Not.
- In Java no Preprocessor and Micros.
//In java didn't support preprocessors. #define PI 3.141 #include<iostream> int main() { int r; float area; area = PI*r*r; cout<<"Area of circle is= "<<area; return 0; } //Here #define didn't support in Java.
- In Java, no user suggested Inline functions.
//Inline function in C++ #include <iostream> using namespace std; inline int cube(int s) { return s*s*s; } int main() { cout << "The cube of 3 is: " << cube(3) << "\n"; return 0; } //Output: The cube of 3 is: 27 //Java didn't support inline function.
- Java has no goto statements
// C++ program to check if a number is // even or not using goto statement #include <iostream> using namespace std; // function to check even or not void checkEvenOrNot(int num) { if (num % 2 == 0) // jump to even goto even; else // jump to odd goto odd; even: cout << num << " is even"; // return if even return; odd: cout << num << " is odd"; } // Driver program to test above function int main() { int num = 26; checkEvenOrNot(num); return 0; } //Java didn't support goto statement
- Java has no default arguments.
//In java didn't support default argument #include<iostream> using namespace std; // A function with default arguments, it can be called with // 2 arguments or 3 arguments or 4 arguments. int sum(int x, int y, int z=0, int w=0) { return (x + y + z + w); } /* Driver program to test above function*/ int main() { cout << sum(10, 15) << endl; cout << sum(10, 15, 25) << endl; cout << sum(10, 15, 25, 30) << endl; return 0; }
- Java has no unsigned int.
//C++ supports unsigned int but java didn't unsigned short us; unsigned int ui; unsigned long ul; unsigned long long ull;
- No -> operator in Java.
//C++ supports dot and arrow operator but java didn't //Syntax: (pointer_name)->(variable_name) #include <iostream> #include <stdlib.h> // Creating the structure struct student { char name[80]; int age; float percentage; }; // Creating the structure object struct student* emp = NULL; // Driver code int main() { // Assigning memory to struct variable emp emp = (struct student*) malloc(sizeof(struct student)); // Assigning value to age variable // of emp using arrow operator emp->age = 18; // Printing the assigned value to the variable cou<<emp->age; return 0; }
- Java has no stack-allocated objects.
//std::allocator() in C++ with Examples // C++ program for illustration // of std::allocator() function #include <iostream> #include <memory> using namespace std; int main() { // allocator for integer values allocator<int> myAllocator; // allocate space for five ints int* arr = myAllocator.allocate(5); // construct arr[0] and arr[3] myAllocator.construct(arr, 100); arr[3] = 10; cout << arr[3] << endl; cout << arr[0] << endl; // deallocate space for five ints myAllocator.deallocate(arr, 5); return 0; }
- Java has no delete operator due to garbage collector.
//delete operator in java didn't support // Program to illustrate deletion of array #include <bits/stdc++.h> using namespace std; int main() { // Allocate Heap memory int* array = new int[10]; // Deallocate Heap memory delete[] array; return 0; }
- Java has no destructor.
//In Java didn't support destructor while C++ supports //Syntax: ~constructor-name(); class String { private: char* s; int size; public: String(char*); // constructor ~String(); // destructor }; String::String(char* c) { size = strlen(c); s = new char[size + 1]; strcpy(s, c); } String::~String() { delete[] s; }
- Java has no type-def.
//Java didn't support typedef #include <iostream> int main(){ typedef unsigned int ui; ui i = 5, j = 8; std::cout << "i = " << i << std::endl; std::cout << "j = " << j << std::endl; return 0; }
- Java has restricted global variables and global functions due to pure OOPs.
//Java didn't access global variable and function // CPP program to illustrate // usage of global variables #include<iostream> using namespace std; // global variable int global = 5; // global variable accessed from // within a function void display() { cout<<global<<endl; } // main function int main() { display(); // changing value of global // variable from main function global = 10; display(); }
- Java has no friend function.
//Java didn't support friend function class Node { private: int key; Node* next; /* Other members of Node Class */ friend int LinkedList::search(); // Only search() of linkedList // can access internal members };
- Java has no friend classes.
//java didn't support friend class but c++ class Node { private: int key; Node* next; /* Other members of Node Class */ // Now class LinkedList can // access private members of Node friend class LinkedList; };
- Java has no templates.
//C++ Supports but Java Not #include <iostream> using namespace std; // One function works for all data types. This would work // even for user defined types if operator '>' is overloaded template <typename T> T myMax(T x, T y) { return (x > y)? x: y; } int main() { cout << myMax<int>(3, 7) << endl; // Call myMax for int cout << myMax<double>(3.0, 7.0) << endl; // call myMax for double cout << myMax<char>('g', 'e') << endl; // call myMax for char return 0; }
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. Or WhatsApp Me.