Summary of the differences between C++, Java, Python

1. Quotes

Java

The data quoted by single quotes is of char type, and the data quoted by double quotes is of type String; single quotes can only quote one character, and double quotes can quote 0 or more. Char is just a basic type, and String can be a class and can be directly referenced.

C++

Double quotes indicate character strings, and single quotes indicate characters.
In the string, even if you only put one letter, in fact, the system will automatically add a \0 to it. Indicates the end. In other words, the string occupies one more place than what you see.
For example,'a' means letter a, "a" means "a,\0"

Python

  1. No quotes
    Let the computer read the content inside the brackets and print the final result. such as:
输入:print(2*3)
结果:6
  1. With quotation marks,
    let the computer directly print the contents without understanding.

  2. With double quotation marks, the
    computer can directly print the contents without understanding. You can output single quotes in the content. For example: "I'm hungrg."

输入:print(''I'm hungrg.'')
结果:I'm hungrg.
  1. Use triple quotation marks to
    achieve line wrapping (automatic line wrapping based on whether there are punctuation marks), or you can do line wrapping during input, and the output will appear as a result of line wrapping, that is, what you get when you enter.

2. Output

Java

System.out.println();

C++

cout<< ;

Python

print()//无需分号Wello

Three output HelloWorld

//java
public class HelloWorld{
    
    
	public static void main(String[] args){
    
    
		System.out.println("HelloWorld");
	}
}

//C++
#include <iostream>
using namespace std;
int main(){
    
    
	cout << "HelloWorld" ;
	return 0;
}

//python
print('HelloWorld')

3. Logic operations

Java

&|^&&||

& : And, both sides are true to be true
| : Or, at least one side of both sides is true to be true
^ : XOR , only if the boolean on both sides are different, that is, one side is true and the other is false, then it is true
! : No, change true to false, change false to true
&& : Short-circuit and, have the same function as &, but have a short-circuit effect
|| : Short-circuit non, have the same function as ||, but have a short-circuit effect

Let's explain the difference between && and &: You
might as well think about the result of the following code first, and then I will give the correct result to check whether your thinking is correct.
Insert picture description here
Upper result:
Insert picture description here
Why is the result different? It is because && has a short-circuit effect. When it checks the first half and is wrong, it will not check the second half, and no more operations will be performed on the second half.
Similarly: ||, if the left is false, the right is executed, if the left is true, the right is not executed.

C++

&& , || , ! , & , |
Are the same as those corresponding to java, see the figure below.
Insert picture description here
ps: After learning C++ for a year, I only know today that the original logical operators of C++ also contain &, but I don’t feel that they are used too much, and && is generally used.

python

andornot

  1. In Python, and and or do not necessarily calculate the value of the expression on the right. Sometimes only the value of the expression on the left can get the final result.
  2. In addition, the and and or operators will use the value of one of the expressions as the final result instead of True or False as the final result.

The above two points are extremely important. Knowing these two points will not make you confused in the process of using logical operations.

For the and operator, the final result is true when the values ​​on both sides are true, but as long as one of them is false, the final result is false, so Python performs the and operation according to the following rules:
if the value of the left expression is False, then there is no need to calculate the value of the expression on the right, because no matter what the value of the expression on the right is, it will not affect the final result. The final result is false. At this time, and will take the value of the expression on the left as the final result.
If the value of the expression on the left is true, then the final value cannot be determined, and will continue to calculate the value of the expression on the right, and use the value of the expression on the right as the final result.

For the or operator, the situation is similar. The final result is false when the values ​​on both sides are false. As long as one of the values ​​is true, the final result is true, so Python performs the or operation according to the following rules:
if the left side is expressed The value of the formula is true, then there is no need to calculate the value of the expression on the right, because no matter what the value of the expression on the right is, it will not affect the final result. The final result is true. At this time, or will take the value of the expression on the left As the final result.
If the value of the expression on the left is false, then the final value cannot be determined, or will continue to calculate the value of the expression on the right, and use the value of the expression on the right as the final result.
Insert picture description here

4.String array related

C++

#include <iostream>
using namespace std;

int main()
{
    
    
	string s = "abc";
	cout << "请输入一个字符串x:";
	string x;
	cin >> x;
	if (s==x)
		cout << "两个字符串相等" << endl;
	else
		cout << "两个字符串不相等" << endl;
	cout << "单独输出各个字符如下:" << endl;
	for (int i=0;i<s.length();i++)
	{
    
    
		if (i!=s.length()-1)
			cout <<"[" << s[i] << "],";
		else
			cout <<"[" << s[i] << "]" << endl;
	}
	return 0;
}

Insert picture description here

Java:

package com.it;

import java.util.Scanner;

public class Str {
    
    
    public static void main(String[] args) {
    
    
        String s = "abc";
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入一个字符串x:");
        String x = sc.nextLine();
        if (s.equals(x))//s.equals(x)就是返回s和x字符串比较的结果
            System.out.println("两个字符串相等");
        else
            System.out.println("两个字符串不相等");
        System.out.println("单独输出各个字符如下:");
        for (int i = 0; i < s.length(); i++) {
    
    //字符串的长度是s.length(),但是如果返回数组的长度就是arr.length
            if (i != s.length() - 1) {
    
    
                System.out.print("[" + s.charAt(i) + "],");//s.charAt(i)相当于C++中的s[i];
            } else {
    
    
                System.out.println("[" + s.charAt(i) + "]");
            }
        }
    }
}

Insert picture description here

5. Reversal of string

C++

#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    
    
	string s;
	s="uidyfhusgs";
	cout << s << endl;
	cout << "---------" << endl;
	reverse(s.begin(),s.end());//需要algorithm头文件
	cout << s << endl;
	return 0;
}

Insert picture description here

Java

package com;

public class Reverse {
    
    
    public static void main(String[] args) {
    
    
        StringBuilder t = new StringBuilder("uidyfhusgs");
        System.out.println("原数据为:" + t);
        System.out.println("反转后的数据为:");
        //第一种String 转 StringBuild 通过 reverse() 再转 String
        String s = new String("uidyfhusgs");
        StringBuilder ss = new StringBuilder(s);//String转化成StringBuild类型
        ss.reverse();
        s = ss.toString();//StringBuild转化为String类型
        System.out.println(s);
        //第二种直接用StringBuild类型进行;
        StringBuilder st = new StringBuilder();
        st.append("uidyfhusgs");
        st.reverse();
        System.out.println(st);
    }
}

Insert picture description here

6. Generate random numbers

C++

#include <iostream>
#include <ctime>
using namespace std;

int main()
{
    
    
	srand(time(0));
	int x[10];
	for (int i=0;i<10;i++)
		x[i]=rand()%100;
	for (int i=0;i<10;i++)
		cout << x[i] << endl;
	return 0;
}

Insert picture description here

Java

import java.util.Random;

public class RandomTest{
    
    
	public static void main(String[] args) {
    
    
		Random r = new Random();
		int[] x = new int[10];
		for (int i=0;i<10;i++)
			x[i]=r.nextInt(100);
		for (int i=0;i<10;i++)
			System.out.println(x[i]);
	}
}

Insert picture description here

7.switch statement

C++

The expression in the switch statement must be an integer or enumeration type, or a class type, where class has a single conversion function to convert it to an integer or enumeration type.
There can be any number of case statements in a switch. Each case is followed by a value to be compared and a colon.
The constant-expression of the case must have the same data type as the variable in the switch, and it must be a constant or a literal.
When the tested variable is equal to the constant in the case, the statement following the case will be executed until the break statement is encountered.
When a break statement is encountered, the switch is terminated and the control flow will jump to the next line after the switch statement.
Not every case needs to include break. If the case statement does not contain break, the control flow will continue with subsequent cases until break is encountered.

Java

The variable type in the switch statement can be byte, short, int or char. Starting from Java SE 7, switch supports the String type, and the case label must be a string constant or literal.

The switch statement can have multiple case statements. Each case is followed by a value to be compared and a colon.

The data type of the value in the case statement must be the same as the data type of the variable, and it can only be a constant or a literal constant.

When the value of the variable is equal to the value of the case statement, the statement after the case statement starts to execute, and the switch statement will not exit until the break statement appears.

When a break statement is encountered, the switch statement terminates. The program jumps to the execution of the statement following the switch statement. The case statement does not have to include the break statement. If no break statement appears, the program will continue to execute the next case statement until a break statement appears.

It is not difficult to see that using python output is the easiest, which is one of the reasons why python is so popular now.
Later, load the difference between java and c++ first, and then write it after python. Writing these is also convenient for your own reference.
At present, I write so much, and try to update it every day. If it is helpful to you, you may wish to like it and follow it!

Guess you like

Origin blog.csdn.net/Freedom_cao/article/details/107280698