0% found this document useful (0 votes)
4 views2 pages

C++ to Java Syntax Comparison Guide

Uploaded by

SONU KUMAR.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

C++ to Java Syntax Comparison Guide

Uploaded by

SONU KUMAR.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C++ to Java Syntax Mapping (Detailed Notes)

1. Input / Output
C++:
#include
using namespace std;
int main(){
int x;
cin >> x;
cout << x;
}

Java:
import [Link].*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner([Link]);
int x = [Link]();
[Link](x);
}
}

2. Arrays
C++:
int arr[5];

Java:
int[] arr = new int[5];

3. Vectors vs ArrayList
C++:
vector v;
v.push_back(10);
[Link]();

Java:
ArrayList v = new ArrayList<>();
[Link](10);
[Link]();

4. Strings
C++:
string s;
cin >> s;
[Link]();

Java:
String s = [Link]();
[Link]();

5. Functions
C++:
int add(int a,int b){
return a+b;
}
Java:
static int add(int a,int b){
return a+b;
}

6. Pointers in C++ for Arrays


In C++, arrays are stored in contiguous memory. The array name acts as a pointer to the first
element.

Example:
int arr[5] = {1,2,3,4,5};
int *ptr = arr;

ptr points to arr[0].


*(ptr + 1) gives arr[1].

Java does not have pointers. Java references handle memory automatically.

You might also like