PythonとJavaの比較

Pythonをよく使っているので、ある程度知っているJavaとの比較をまとめた。

開発環境のセットアップ

Python
Anaconda, Pytorchなどをインストール Pythonインタプリタが同梱されている

Java
Eclipse, intelliJ IDEAをインストール EclipseにはJavaコンパイラが同梱されている

エディタとコンパイラの選択肢
エディタでプログラムを記述、コンパイルして実行できる
欠点
デバッグ、フォーマッター、構文チェックなど欲しい機能は自分で追加する必要がある

外部ライブラリの導入

Python

pip install hoge

Java
jarファイルをダウンロードして追加

構文

Python

#1つのデータ
a = 0 #型が自動で決まる(文字の0と数字の0は別のもの)

#行動を1つにまとめる
def aColumn():
    print('あ')
    print('い')
    print('う')
    print('え')
    print('お')

#複数のデータをグループにまとめる
b = [2, 4, 6] #リスト
c = {'文字': 'hoge', '数字': '0'} #辞書

#行動とデータのグループ
class Hoge:
    hogeA = 0
    hogeB = 'hoge'

    def hoge(): 
        return hogeA


#条件分岐
if hoge:
    print(hoge)

#繰り返し
for hoge in hoges:
    print(hoge)

Java

//1つのデータ
int a = 0; //aは数字だとPCに教える

#行動を1つにまとめる
public void aColumn() {
    System.out.println('あ');
    System.out.println('い');
    System.out.println('う');
    System.out.println('え');
    System.out.println('お');
} 

//複数のデータをグループにまとめる
ArrayList<Integer> b = new ArrayList(2, 4, 6); //リスト
HashMap<String, Integer> c = new HashMap(); //辞書
c.put('文字', 'hoge'); //データを追加
c.put('数字', '0'); //データを追加

//行動とデータのグループ
class Hoge {
    int hogeA = 0;
    String hogeB = 'hoge';

    public int hoge() {
        return hogeA;
    }
}


//条件分岐
if(hoge) {
    System.out.println(hoge);
}

//繰り返し
for(int i = 0; i < hoges.length; i++) {
    System.out.println(hoges[I]);
}