preface

C++ is an extension of C, while Java is designed to be the simplified successor of C++. They have many similarities, such as the same basic types: int, char, float, long, double. There are also some differences, such as Java char is 2 bytes, a byte 8 bits, such as: 0000 1111, 2 bytes is 16 bits, while C++ char is only 8 bits, 1 byte. They also have data types representing true, false, and if… Else, while, for and switch are all the same. In order to make the transition from Java to C++ faster, I prepared this article. I hope it will also be helpful to you.

print

Java

System.out.print("Hello World!"); 
System.out.println("Hello World!");
Copy the code

C++

// The first implementation
std::cout << "Hello World!";
std::cout << "Hello World!" << std::endl;
// If you use namespaces, you don't need STD ::
#include <iostream>
using namespace std;
int main(a) {
    cout << "Hello World!";
    cout << "Hello World!" << endl;
    return 0;
}
Copy the code

constant

Java

final int x = 2;
final int y = 1;
Copy the code

C++

// Use the #define preprocessor.
#define X 2
#define Y 1
// Use the const keyword.
const int  X = 2;
const int  Y = 1;
Copy the code

variable

Java

   int w;
   int z = 2;
    z = 3;
    w = 1;
Copy the code

C++

   int w;
   int z = 2;
    z = 3;
    w = 1;
Copy the code

The assignment is empty

Java

String lastName;
lastName = null
Copy the code

C++

string lastName;
lastName = NULL; // The compiler reported an error, unable to assign a value, want to free lastname.clear (); function
Copy the code

Sentenced to empty

Java

if(text ! =null) {int length = text.length();
}
Copy the code

C++

    string text;
	//C++ strings do not need to be nulled, return length 0
    cout << text.length() << endl;
Copy the code

Characters joining together

Java

String name = "John";
String lastName = "Smith";
String text = "My name is: " + name + "" + lastName;
Copy the code

C++

 string name = "John";
 string lastName = "Smith";
 string text = "My name is: " + name + "" + lastName;
Copy the code

Newline characters

Java

String text = "First Line\n" +
              "Second Line\n" +
              "Third Line";
Copy the code

C++

string  text = "First Line\nSecond Line\nThird Line";
Copy the code

The ternary operation

Java

String text = x > 5 ? "x > 5" : "x <= 5";
Copy the code

C++

string  text = x > 5 ? "x > 5" : "x <= 5";
Copy the code

Bit operation

Java

int andResult  = a & b;
int orResult   = a | b;
int xorResult  = a ^ b;
int rightShift = a >> 2;
int leftShift  = a << 2;
Copy the code

C++

int andResult  = a & b;
int orResult   = a | b;
int xorResult  = a ^ b;
int rightShift = a >> 2;
int leftShift  = a << 2;
Copy the code

Father and son

Java

if(x instanceof Integer){ }
Copy the code

C++

(is_base_of<Integer, x>::value ? "true" : "false")
Copy the code

Type conversion

Java

double a = 1.9;
int b = (int) a;
int d = a; / / an error
String c = String.valueOf(b);
Copy the code

C++

double a = 1.9;
int b = (int)a;
int d = a; // Hermit switch
string c = to_string(b); // String conversion
Copy the code

Switch

Java

		int a = 1;
        String b;
        switch (a) {
            case 0:
            case 1:
                b = "1";
                break;
            case 2:
                b = "2";
                break;
            default:
                b = "default";
                break;
        }
Copy the code

C++

		int a = 1;
        String b;
        switch (a) {
            case 0:
            case 1:
                b = "1";
                break;
            case 2:
                b = "2";
                break;
            default:
                b = "default";
                break;
        }
Copy the code

The For loop

Java

for (int i = 1; i < 11 ; i++) { }

for (int i = 1; i < 11 ; i+=2) {}for (String item : collection) { }
Copy the code

C++

for (int i = 1; i < 11 ; i++) { }

for (int i = 1; i < 11 ; i+=2) {}for (string &item : collection) { }
// or auto is also the type used to automatically fetch variables in the new C++11 standard
for (auto &item : collection) { }
Copy the code

A collection of

Java

final List<Integer> numbers = Arrays.asList(1.2.3);
Copy the code

C++

int numbers[5] = {1.2.3};


Copy the code

Map

Java

final Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1."One");
map.put(2."Two");
map.put(3."Three");
Copy the code

C++

    map < int , string > map;
    map.insert(pair < int,string > (1."One"));
    map.insert(pair < int,string > (1."Two"));
    map.insert(pair < int,string > (1."Three"));
Copy the code

So just to summarize

We review, the basic types of c + +, the types of operation, judgment, operators, collection, etc to do a simple comparison, it is not hard to find, c + + and Java really like, overall feeling to write c + + is a bit complicated, there are also than Java to write a simple part, etc., have more novel and waiting for you to discover, next and then study, thanks.