Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.96 KB | None | 0 0
  1. # basic
  2.  
  3. # 理解しておくべきことば
  4. - JDK(Java Development Kit) : 開発環境
  5. - JRE(Java Runtime Environment) : Javaの実行環境。JDKに含まれている
  6. - JVM(Java Virtual Machine) : Javaを動作させるための仮想マシン
  7. # コンパイル
  8. $ javac HelloWorld.java
  9. # 実行
  10.  
  11. 実行する際に`.class`をつける必要はない
  12.  
  13. $ java HelloWorld
  14. ## コメント
  15. - 一行
  16. // comment
  17. - 複数行
  18. /*
  19. comment
  20. comment
  21. */
  22. # 変数
  23. 型 変数名という形式で書く
  24. String msg = "Hello world";
  25. # 定数
  26. finalキーワードを使う
  27. String final MSG = "Hello world";
  28. # 基本データ型
  29. boolean
  30. char : 1文字
  31. byte / short / int / long : 整数値、通常はint
  32. float / double : 小数、通常はdouble
  33. - String : 文字列。`" "`で囲む
  34. # 配列
  35.  
  36. 次のように書く
  37.  
  38. int[] a = new int[4]; // new演算子で型指定、要素数を書く
  39. int[] a = new int[]{1, 2, 3, 4}; // 宣言と同時に初期化
  40. int[] a = {1, 2, 3, 4}; // 省略
  41. ## 多次元配列
  42. int[][] a = {
  43. {10, 20},
  44. {30, 40},
  45. {50, 60}
  46. };
  47. System.out.println(a[0][0]); // 10
  48. public class HelloWorld {
  49. public static void main (String[] args) {
  50. int sales[] = {150, 200, 140, 400};
  51.  
  52. for (int i = 0; i < sales.length; i++) {
  53. System.out.println(sales[i]);
  54. }
  55. }
  56. }
  57. # キャスト
  58. - データ型の違うものを演算するときはキャスト:型の明示的な変換を行う
  59. double d = 102.333;
  60. int i = (int)d;
  61. # 演算子
  62. ## インクリメント、デクリメント
  63. ++a,--aの前置とa++、a--の後置がある
  64. int x, a = 1;
  65. x = ++a; // aに1を足した後、代入され流のでxは2
  66. int y, b = 1;
  67. y = b++; // yにbを代入した後、bに1をたすのでyは1
  68. # switch
  69. breakを忘れないように
  70. - defaultでデフォルト値を設定
  71. - case文は複数の条件を列挙できる(ex. case 3, case 4)
  72. public class HelloWorld {
  73. public static void main (String[] args) {
  74. int n = 2;
  75.  
  76. switch (n) {
  77. case 1:
  78. System.out.println("One!");
  79. break;
  80. case 2:
  81. System.out.println("Two!");
  82. break;
  83. case 3:
  84. case 4:
  85. System.out.println("Three! or Four!");
  86. break;
  87. default:
  88. System.out.println("None of these!");
  89. break;
  90. }
  91. }
  92. }
  93. # for
  94. public class HelloWorld {
  95. public static void main (String[] args) {
  96. for (int n = 0; n < 10; n++) {
  97. System.out.println(n);
  98. }
  99. }
  100. }
  101. - continueでループをスキップ、breakはループから抜ける
  102. public class HelloWorld {
  103. public static void main (String[] args) {
  104. for (int n = 0; n < 10; n++) {
  105. if (n == 5) {
  106. continue;
  107. }
  108. System.out.println(n);
  109. }
  110. }
  111. } // 0, 1, 2, 3, 4, 6, 7, 8, 9
  112. forを使ってすっきりと書くこともできる
  113. public class HelloWorld {
  114. public static void main (String[] args) {
  115. int sales[] = {10, 20, 30, 40};
  116.  
  117. for (int sale : sales) {
  118. System.out.println(sales[i]);
  119. }
  120. }
  121. }
  122. # メソッド
  123. returnで値を返す際にはその型をメソッド名の前につける
  124. public static String sayHi(String name) {
  125. return "Hi " + name;
  126. }
  127. ## 抽象メソッド
  128. abstractキーワードを使うと、メソッドの内容は未定だが、子クラスで宣言されるべき、という表明になる。
  129.  
  130. 抽象メソッドを含むクラスは必ず抽象クラスでなければならない
  131. 抽象メソッドは子クラスで完成される必要がある(そうでない場合、子クラスは抽象クラスである必要がある)
  132.  
  133. public abstract class Character {
  134. public abstract void attack(Matango m) {
  135. }
  136. }
  137. # クラス
  138. - メンバー変数:クラス内で使うデータ
  139. - コンストラクタ:初期化処理
  140. class User {
  141. // member variables
  142. String name;
  143. String email;
  144.  
  145. // Constructor
  146. User(String name) {
  147. this.name = name;
  148. }
  149.  
  150. void sayHi() {
  151. System.out.println("Hi I'm "+this.name);
  152. }
  153. }
  154.  
  155. public class HelloWorld {
  156. public static void main (String[] args) {
  157. User tom = new User("Tom");
  158. tom.sayHi(); // Hi I'm Tom
  159. }
  160. }
  161. ## 別コンストラクタの呼び出し
  162.  
  163. コンストラクタをオーバーロードする歳は別にコンストラクタを`this();`で呼び出す
  164.  
  165. class User {
  166. String name;
  167. String email;
  168.  
  169. User(String name, String email) {
  170. this.name = name;
  171. email = email;
  172. }
  173.  
  174. User() {
  175. this("Example", "test@example.com");
  176. }
  177. }
  178. ## 継承
  179.  
  180. 上のUserクラスを継承する。
  181.  
  182. class SuperUser extends User {
  183. SuperUser(String name) {
  184. super(name); // 親クラスのコンストラクタを呼ぶ
  185. }
  186.  
  187. // メソッドのオーバーライド
  188. void sayHi() {
  189. System.out.println("HIIIIIII!!! " + this.name);
  190. }
  191. }
  192.  
  193. オーバーライドはアノテーション`@Override`を記述しておくと、メソッド名や引数が間違っている場合にエラーになる
  194.  
  195. @Override
  196. void sayHi() {
  197. //...
  198. }
  199. ## 抽象クラス
  200.  
  201. インスタンスを生成できないクラス(module)は`abstract`を使う
  202.  
  203. public abstract class Character {
  204. String name;
  205. int hp;
  206. public abstract void attack(Matango m);
  207. }
  208. # パッケージとアクセス修飾子
  209. ## アクセス修飾子
  210.  
  211. クラスやそのフィールド、メソッドに対してアクセス修飾子をつけることができる
  212. 変数のアクセス領域を制限する
  213.  
  214. - public : 自由にアクセスできる
  215. - protected : クラスとその継承クラスでアクセスできる
  216. - private : クラス内でしかアクセスできない
  217. # getter、setter
  218. class User {
  219. private String name;
  220. private int score;
  221.  
  222. public User(String name, int score) {
  223. this.name = name;
  224. this.score = score;
  225. }
  226.  
  227. public int getScore() {
  228. return this.score;
  229. }
  230.  
  231. public void setScore(int score) {
  232. if (score > 0) {
  233. this.score = score;
  234. }
  235. }
  236. }
  237.  
  238. public class MyApp {
  239. public static void main(String[] args) {
  240. User tom = new User("Tom", 65);
  241. tom.setScore(80);
  242. tom.setScore(-1);
  243. System.out.println(tom.getScore());
  244. }
  245. }
  246. # クラス変数・クラスメソッド
  247. static修飾子を使って、インスタンス化しなくてもクラスから直接扱えるフィールドやメソッドを定義できる(クラス変数・クラスメソッド)
  248. class User {
  249. private String name;
  250. private static int count = 0; // クラス変数
  251.  
  252. public User(String name) {
  253. this.name = name;
  254. User.count++;
  255. }
  256.  
  257. public static void getInfo() { // クラスメソッド
  258. System.out.println("# of instance " + User.count);
  259. }
  260. }
  261.  
  262. public class MyApp {
  263. public static void main(String[] args) {
  264. User.getInfo();
  265. User tom = new User("Tom");
  266. User.getInfo();
  267. User bob = new User("Bob");
  268. User.getInfo();
  269. }
  270. }
  271. # final修飾子
  272.  
  273. フィールドやメソッド、クラスを変更できなくする
  274. 変数なら再代入、クラスは継承、メソッドのオーバーライドなどを禁止する。
  275. アクセス修飾子(`public`、`private`、`protected`)、`static`、`final`はどの順番で書いても構わないが、左記の順番で書かれることが慣習となっている
  276.  
  277. # インターフェース
  278.  
  279. クラスの機能を拡張するためにインターフェースを使う。定数、抽象メソッド、defaultメソッド、staticメソッドを書くことができる。
  280. インターフェースに宣言されたメソッドは自動的に`public`かつ`abstract`になる(省略できる)
  281. フィールドは、`public static final`のもの(定数)のみ定義できる。省略しても自動的に追加される。
  282. 通常メソッドの処理内容は書けないが、Java8では`default`キーワードを用いることで処理内容を追加できる。
  283. クラスの継承と違い、インターフェースはクラスにいくつでも適用させることができる。
  284.  
  285. interface Printable {
  286. double VERSION = 1.2;
  287. void print();
  288. // defaultメソッド
  289. public default void getInfo() {
  290. System.out.println("I/F ver. " + Printable.VERSION);
  291. }
  292. }
  293.  
  294. class User implements Printable {
  295. @Override
  296. public void print() {
  297. System.out.println("Now printing user profile...");
  298. }
  299. }
  300.  
  301.  
  302. public class MyApp {
  303. public static void main(String[] args) {
  304. User tom = new User();
  305. tom.print();
  306. tom.getInfo();
  307. }
  308. }
  309. # 列挙型
  310.  
  311. 定数をまとめて扱いやすくする列挙型。
  312.  
  313. enum Result {
  314. SUCCESS,
  315. ERROR
  316. }
  317.  
  318. public class MyApp {
  319. public static void main(String[] args) {
  320. Result res;
  321.  
  322. res = Result.ERROR;
  323.  
  324. switch (res) {
  325. case SUCCESS:
  326. System.out.println("OK");
  327. break;
  328. case ERROR:
  329. System.out.println("NG");
  330. break;
  331. }
  332. }
  333. }
  334. # 例外処理
  335.  
  336. 例外が起こりそうな処理を`try`で囲み、例外を`catch`する、という形で例外処理を行う
  337.  
  338. class MyException extends Exception {
  339. public MyException(String s) {
  340. super(s);
  341. }
  342. }
  343.  
  344. public class MyApp {
  345. public static void div(int a, int b) {
  346. try {
  347. if (b < 0) {
  348. throw new MyException("Not minus");
  349. }
  350. System.out.println(a / b);
  351. } catch (ArithmeticException e) {
  352. System.err.println(e.getMessage());
  353. } catch (MyException e) {
  354. System.err.println(e.getMessage());
  355. } finally {
  356. System.out.println("--- end ---");
  357. }
  358. }
  359.  
  360. public static void main(String[] args) {
  361. div(3, 0); // / by zero (0 除算のエラー)
  362. div(5, -2); // Not minus
  363. }
  364. }
  365. # ジェネリクス
  366.  
  367. 型を実行時に指定できる汎用化したメソッドを定義できる
  368.  
  369. class MyData<T> {
  370. public void getThree(T x) {
  371. System.out.println(x);
  372. System.out.println(x);
  373. System.out.println(x);
  374. }
  375. }
  376.  
  377. public class MyApp {
  378. public static void main(String[] args) {
  379. MyData<Integer> i = new MyData<>();
  380. MyData<String> s = new MyData<>();
  381. i.getThree(30);
  382. s.getThree("hello");
  383. }
  384. }
  385. # 組み込みメソッド
  386. ## Math
  387. - ceil : 切り上げ
  388. - floor : 切り捨て
  389. - round : 四捨五入
  390. - 他にrandom()やPIなど
  391. ## Calendar
  392.  
  393. 標準ライブラリではない
  394.  
  395. import java.util.Calendar;
  396.  
  397. public class HelloWorld {
  398. public static void main (String[] args) {
  399. Calendar cal = Calendar.getInstance();
  400. System.out.println(cal.get(Calendar.DATE)); // 現在時刻
  401. cal.set(2011, 4, 12, 3, 22);
  402. System.out.println(cal.get(Calendar.DATE)); // 12
  403. cal.add(Calendar.DATE, 5);
  404. System.out.println(cal.get(Calendar.DATE)); //17
  405. }
  406. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement