[jdk14.0.2] 基礎1

compile
$ javac MyApp.java
$ java MyApp

basic

public class MyApp {

	public static void main(String[] args){
		System.out.println("Hello world!");
	}
}

variable

String msg;
		msg = "Hello world!";
		System.out.println(msg);

variable
L primitiveなデータ型はメモリ領域を確保する
L String, arrayなど参照型はメモリの番地を確保して、番地を参照している

public static void main(String[] args){
		// String, char
		char a = 'a';
		// byte short int long
		int x = 10;
		long y = 55555555L;
		// 浮動小数点数 float double
		double d = 23423.334;
		float f = 32.33F;
		// 論理値
		boolean flag = true;
		// \n
		String msg = "Hello wo\nr\tld";
		System.out.println(msg);
	}
int i;
		i = 10 / 3;
		System.out.println(i);
		i = 10 % 3;
		System.out.println(i);
		int x = 5;
		x++;
		System.out.println(x);
		x--;
		System.out.println(x);

cast

double d = 52343.213;
int i = (int)d;

		int i = 10;
		double d = (double)i / 4;
		System.out.println(d); 

if

		int score = 85;
		if(score > 80){
			System.out.println("great");
		} else if (score > 60){
			System.out.println("good"); 
		} else {
			System.out.println("soso"); 
		}

		int score = 25;
		String msg = score > 80 ? "great!": "soso ...!";
		System.out.println("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

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

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

for

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

配列

		int[] sales;
		sales = new int[3];

		sales[0] = 100;
		sales[1] = 200;
		sales[2] = 300;

		System.out.println(sales[1]);

		sales[1] = 1000;
		System.out.println(sales[1]);

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

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

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

		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";
		System.out.println(s);
		System.out.println(t);

method

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

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

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

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

	// overload
	public static void sayHi(){
		System.out.println("Hi! Nobody!");
	}

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

class

class User {
	String name = "Me!";

	void sayHi(){
		System.out.println("Hi!");
	}
}

public class MyApp {

	public static void main(String[] args){

		User tom;
		tom = new User();

		System.out.println(tom.name);
		tom.sayHi();
	}
}

constructor
L thisはclassのcontructorという意味

class User {
	String name;

	User(String name){
		this.name = name;
	}

	void sayHi(){
		System.out.println("Hi!" + this.name);
	}
}

public class MyApp {

	public static void main(String[] args){

		User tom;
		tom = new User("Tom");

		System.out.println(tom.name);
		tom.sayHi();
	}
}

継承

class AdminUser extends User{
	AdminUser(String name){
		super(name);
	}

	void sayHello(){
		System.out.println("Hello!" + this.name);
	}

}

public class MyApp {

	public static void main(String[] args){

		User tom;
		tom = new User("john");

		System.out.println(tom.name);
		tom.sayHi();

		AdminUser bob = new AdminUser("bob");
		System.out.println(bob.name);
		bob.sayHello();
	}
}