部分內容截取之前的一篇博客:c++與java的幾個不同點
1
python、C++、Java都是強調數據類型的強類型語言。雖然python變量使用前無需聲明,但python的變量並不能進行隱式轉換。python變量進行轉換時實際上是重新創建了一個內存空間。
編程語言按計算機執行方式可分為三種語言:
編譯型:先將源代碼編譯成目標語言之后通過連接程序連接到生成的目標程序進行執行,例如C++。
解釋型:由解釋器根據輸入的數據當場執行而不生成任何的目標程序,例如python。
混合型:兩種語言的特征都存在,典型的就是Java。
2
C++可以定義頭文件(.h文件)用來防止重復編譯,Java和python不需要頭文件。
三者均可引入其他源代碼文件定義的類等等,比如C++的#include <stdio.h>,Java的import java.lang.;以及python的import itertools
C++:
#include<iostream>
using namespace std;
int main() { cout << "Hello World!" << endl; return 0; }
Java:
package test; import java.lang.*; public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } }
python:
import itertools print(list(itertools.permutations(['a','b','c'], 2)))
3
C++和Java對代碼縮進和{}要求不算嚴格,但是python必須嚴格遵守代碼縮進規則。python的代碼塊不需要用{}括起來,只要代碼縮進相同就認為這上下兩行代碼屬於同一個代碼塊。
C++
#include<iostream> using namespace std; int main() { cout << "Hello World!" << endl; return 0; }
Java
package test; public class HelloWorld { public static void main(String[] args) { System.out.println(“Hello World!”); } }
python
print("outside1") if __name__ == '__main__': print("inside") print("outside2")
4
C++和Java每行代碼結尾必須加上“;”,python不需要“;”參考不同點3示例代碼
5
C++和Java中++代表+=1,python不能用++,只可以用+=1
C++
#include<iostream> using namespace std; int main() { int i = 0; switch(i){ case 0: cout << "false" << endl; break; case 1: cout << "true" << endl; break; default: if(i == 2){ cout << 2 << endl; } else if(i > 2) { cout << "大於2" << endl; } else { cout << "小於0" << endl; } } while(i<=10){ if(i % 2 == 0){ cout << i << endl; } i++; } return 0; }
Java
package test; public class Test { public static void main(String[] args) { int i=0; switch(i){ case 0: System.out.println("false"); break; case 1: System.out.println("true"); break; default: if(i == 2){ System.out.println(2); } else if(i > 2) { System.out.println("大於2"); } else { System.out.println("小於0"); } } while (i <= 10){ if (i % 2 == 0){ System.out.println(i); } i++; } } }
python
i = 0 if i == 0: print("false") elif i == 1: print("true") else: if i == 2: print(2) elif i > 2: print("大於2") else: print("小於0") while i <= 10: if i % 2 == 0: print(i) i += 1
6
C++和Java判斷和循環等語句的條件邏輯表達式使用()括起來,python可以不用()。參考不同點5示例代碼
7
C++和Java條件語句為if……else if……else,選擇語句為switch,循環語句有while、do……while,python條件語句為if……elif……else,無選擇語句,使用條件語句代替選擇語句,循環語句只有while。參考不同點5示例代碼。
8
C/C++程序基本上是由n個函數組成,主函數調用其他函數實現所需功能。但是主函數內要使用的函數必須在主函數之前有過聲明或定義,否則編譯會不通過。即C/C++主方法要么寫在所有定義了的被調用函數后面,要么在主函數之前就必須有所調用函數的聲明。
Java程序是由n個類、m個方法組成,某個public類的主方法調用當前類的方法,或是調用其他類的公有(public)方法實現所需功能。主方法可以寫在與類內被調用方法同級的任意位置。
python的主函數是個判斷語句“if __name__ == '__main__':”,但實際上只要沒有縮進就默認該代碼為主函數中的代碼。python和C++較為相似,但不存在聲明的說法,只能在定義的函數之后進行調用。
C++
#include<iostream> //頭文件,必寫 using namespace std; //命名空間 void hello(); //如果某個函數是在主函數之后定義的,那么必須在主函數之前聲明,主函數才能調用這個函數 int main() { hello(); return 0; } void hello() { cout << "Hello World!" << endl; }
Java
package java.xingchen; //說明在java/xingchen這個文件夾內 import java.lang.*; //導包語句,可不寫,編譯器自動加 //Java程序運行的是public類內的主方法 //文件名必須和public類類名一樣(例如當前文件只能叫Xingchen.java),一個文件只能存在一個public類,主方法只能在public類內 //類只能由public修飾,即一個類只能是public class或class public class Xingchen { public static String out; //靜態方法(static)調用該變量,那么該變量必須為靜態變量(static) public Xingchen(){ //空參構造方法,當前類實例化時如果未傳參則執行此方法,一般空參構造方法可不寫 this.out = "World"; //this是指當前類 } public static void main(String[] args) { new Test("Hello").hello(); //Test類創建匿名對象並執行類內的public方法 Xingchen x = new Xingchen(); x.hello(); } public static void hello(){ System.out.println(out); } } class Test { String out; public Test(){} //如果一個類存在有參構造,那么必須定義空參構造方法 public Test(String str){ //有參構造方法,實例化時如果傳入指定參數,則執行當前方法 this.out = str; } public void hello(){ System.out.println(this.out); } }
python
def hello(): print("Hello World!") if __name__ == '__main__': hello()
9
c++的錄入輸出靠輸入流cin,輸出流cout、cerr、clog實現,需要寫#include<iostream>。
cin >> a;
cout << a << endl;
Java語言里沒有像c++那樣一個函數就完成錄入,它的錄入使用了一些基礎類。
首先導包
import java.util.Scanner;
然后在方法內使用以下語句進行錄入
Scanner sc = new Scanner(System.in); String a = sc.next(); //效果等同於String a = sc.nextLine(); int b = sc.nextInt(); float c = sc.nextFloat(); double d = sc.nextDouble(); boolean e = sc.nextBoolean(); sc.close(); //結束數據流。錄入語句結束及時釋放內存是個好習慣。
其實還有其他輸入的方法,但是這種比較常用。Scanner類中沒有nextChar方法,不能直接通過Scanner類從控制台讀取char類型的變量
Java的輸出語句用法很簡單:System.out.println();或System.out.print();System.out.println語句結束后自動換行,System.out.print語句結束后不換行。
int age = 18; String name = "張三"; System.out.println(age + "歲的" + name); //18歲的張三
python輸入靠input()函數,返回的是string類型,輸出靠print()函數。
a = input() print(a)
10
C++基本數據類型為布爾型 bool、字符型 char、整型int、浮點型float、雙浮點型double、無類型void、寬字符型 wchar_t。
Java語言提供了八種基本類型。四個整數型(byte、short、int、long),兩個浮點型(float、double),一種字符類型(char),還有一種布爾型(boolean)。
Python3有六個標准數據類型:不可變數據(3 個):Number(數字)、String(字符串)、Tuple(元組);可變數據(3 個):List(列表)、Dictionary(字典)、Set(集合)。
C++和Java中,變量在使用前必須聲明,python直接賦值使用,不聲明。
在 Python 中,變量就是變量,它沒有類型,我們所說的"類型"是變量所指的內存中對象的類型