Java 構文4

例外

// 例外処理
class MyException extends Exception {
  public MyException(String s){
    super(s);
  }
}

public class MyApp{

  public static void div(int a, int b){
    try {
      if (b < 0){
        throw new MyException("not minus!");
      }
      System.out.println(a / b);
    } catch (ArithmeticException e){
      System.err.println(e.getMessage());
    } catch (MyException e){
      System.err.println(e.getMessage());
    } finally{
      System.out.println(" -- end -- ");
    }
  }
  public static void main(String[] args){
    div(3, 0);
    div(5, -2);
  }
}

ラッパークラス

public class MyApp{
  public static void main(String[] args){
    // Integer i = new Integer(32);
    // int n = i.intValue();
    Integer i = 32; // auto boxing
    i = null;
    int n = i; // auto unboxing
    
    System.out.println();
  }
}

generics:型の汎用化

class MyData<T> {
  public void getThree(T x){
    System.out.println(x);
    System.out.println(x);
    System.out.println(x);
  }
}

public class MyApp{
  public static void main(String[] args){
    // MyInteger mi = new MyInteger();
    // mi.getThree(55);
    MyData<Integer> i = new MyData<>();
    i.getThree(32);
    MyData<String> s = new MyData<>();
    s.getThree("hello");
  }
}

Thread処理:

class MyRunnable implements Runnable{
  @Override
  public void run(){
    for (int i= 0; i < 500; i++){
      System.out.print('*');
    }
  }
}

public class MyApp{
  public static void main(String[] args){
    MyRunnable r = new MyRunnable();
    Thread t = new Thread(r);
    t.start();

    for (int i= 0; i < 500; i++){
      System.out.print('.');
    }
  }
}

無名クラス

public class MyApp{
  public static void main(String[] args){
    // MyRunnable r = new MyRunnable();
    // Thread t = new Thread(r);
    // t.start();
    new Thread(new Runnable(){
      @Override
      public void run(){
        for (int i= 0; i < 500; i++){
          System.out.print('*');
        }
      }
    }).start(); // 無名クラス

    for (int i= 0; i < 500; i++){
      System.out.print('.');
    }
  }
}

Stringクラス

public class MyApp{
  public static void main(String[] args){
      String s = "abcdef";
      System.out.print(s.length());
      System.out.print(s.substring(2, 5));
      System.out.print(s.replaceAll("ab","AB"));

      String s1 = "ab";
      String s2 = "ab";

      if (s1.equals(s2)){
        System.out.print("same!");
      }

      if (s1 == s2){
        System.out.print("same!same!");
      }
    }
}

printfメソッド

public class MyApp{
  public static void main(String[] args){
    int score=50;
    double height=165.8;
    String name ="yoshimoto";

      System.out.printf("name: %s, score: %d, height: %f\n", name, score, height);
      System.out.printf("name: %-10s, score: %10d, height: %5.2f\n", name, score, height);

      String s = String.format("name: %10s, score: %-10d, height: %5.2f\n", name, score, height);
      System.out.println(s);
    }
}

Math, randomクラス

import java.util.Random;

public class MyApp{
  public static void main(String[] args){
      double d = 53.234;
      System.out.println(Math.ceil(d));
      System.out.println(Math.floor(d));
      System.out.println(Math.round(d));
      System.out.println(Math.PI);

      Random r = new Random();
      System.out.println(r.nextDouble()); // 0-1
      System.out.println(r.nextInt(100)); // 0-1
      System.out.println(r.nextBoolean()); // 0-1
    }
}

ArrayList

import java.util.*;

public class MyApp{
  public static void main(String[] args){
      List sales = new ArrayList<>();

      sales.add(10);
      sales.add(20);
      sales.add(30);

      for (int i = 0; i < sales.size(); i++){
        System.out.println(sales.get(i));
      }

      sales.set(0, 100);
      sales.remove(2);

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

    }
}

Hash.set

import java.util.*;

public class MyApp{
  public static void main(String[] args){
      HashSet sales = new HashSet<>();

      sales.add(10);
      sales.add(20);
      sales.add(30);
      sales.add(10);

        System.out.println(sales.size());

        for (Integer sale: sales){
          System.out.println(sale);
        }
        sales.remove(30);
        for (Integer sale: sales){
          System.out.println(sale);
        }
    }
}