java 構文

条件分岐

int score = 35;
    if (score > 80){
      System.out.println("great");
    } else if (score> 60){
      System.out.println("good");
    } else {
    System.out.println("soso");
  }
int score = 65;
  String msg = score > 80 ? "great" : "soso...";

switch

String signal = "red";
    switch (signal){
      case "red":
        System.out.println("stop!");
        break;
      case "blue":
        System.out.println("go");
        break;
      case "yellow":
        System.out.println("caution");
        break;
      default:
        System.out.println("wrong signal");
        break;

while, do while

int i = 0;
    while (i < 10){
      System.out.println(i);
      i++;
    } 
int i = 0;
    do {
      System.out.println(i);
      i++;
    } while (i < 10);

for

for(int i = 0; i < 10; i++){
        if (i == 5){
          break;
        }
        System.out.println(i);
      }

array

      int[] sales;
      sales = new int[3];
      sales[0] = 100;
      sales[1] = 200;
      sales[2] = 300;
      sales[1] = 1000;
      System.out.println(sales[1]);
      // int[] sales:
      // sales = new int[](100, 200, 300);

      int[] sales = {100, 200, 300}

要素の操作

int[] sales = {700, 300, 500};

      for (int i = 0; i < sales.length; i++){
        System.out.println(sales[i]);
      }

int[] sales = {700, 300, 500};

      for (int sale : sales){
        System.out.println(sale);
      }

参照型にはメモリ領域のバッチが入ります。文字列の場合は、変更不可となります。

      int[] a = {3, 5, 7};
      int[] b = a;
      b[1] = 8;
      System.out.println(a[1]);
      System.out.println(b[1]);
      String s = "hello";
      String t = s;
      t = "world";

メソッド

public static void sayHi(String name){
    System.out.println("hi!" + name);
  }

  public static void main(String[] args){
    sayHi("jack");
  }

public static String sayHi(String name){
    return "hi!" + name;
  }

  public static void main(String[] args){
    String msg = sayHi("jack");
    System.out.println(msg);
  }

オーバーロード

public static void sayHi(){
    System.out.printl("hi! nobody");
  }

  public static void main(String[] args){
    sayHi("jack"); //引数
    sayHi();
  }