up: ðJava
Javaèšèªã«é¢ããããš.
æªæŽç
éå»ã¡ã¢. å°ããã€æŽçãã.
Java ããã°ã©ã å®è¡æ¹æ³
ã³ã³ãã€ã«
- javac ã³ãã³ãã§ã³ã³ãã€ã«.
- .java ãã .class ãã¡ã€ã«ãçæãã.
javac -cp jar/netty-3.6.10.Final.jar src/Program.java
å®è¡
- java ã³ãã³ãã§å®è¡.
- -cp 㧠classpath ãæå®.
java -cp .;jar/netty-3.6.10.Final.jar;src Program
ãµã³ãã«ããåŠã¶ classpath ã®èšå®ãš Java ããã°ã©ã ã®å®è¡ - Java å ¥é
Hello World
public class HelloWorld {
public static void main (String[] args) {
System.out.println ("Hello World !!");
}
}
ãã«ãããŒã«
Apatch Ant
Apatch Maven
Gradle
-
ç¹åŸŽ
次ããåŒçš: ãã«ãããŒã« Gradle ã®ã€ã³ã¹ããŒã«ãšäœ¿ãæ¹ - ïŒ IT
- Groovy ã§ãã«ãæé ãèšè¿°
- Apache Ivy ããŒã¹ã®åŒ·åãªäŸåé¢ä¿ç®¡ç
- 人æ°ã® IDE (Eclipse, NetBeans) ã§ã䜿ãã
- Ant ãã«ãã®åŒã³åºããã§ãã
Java ææ³
interface
å 容ã«æœè±¡ã¡ãœããããæããªãã¯ã©ã¹ã®ãããªãã®.
class InterfaceImpl implements Interface1, interface2, interface3 {
...
}
extend
ç¶æ¿.
interface,extend,implement ã®ã¡ãã
誀解ããããã®ã§æŽçããŠãã.
以äžã®é¢ä¿ããã.
- interface ãš implements
- class ãš extends
Java ã§ã¯, å€éç¶æ¿ãèš±å¯ããŠããªã. ã²ãšã€ã® class ã extends ã§ç¶æ¿ããã, å¥ã®æ®ãã㯠interface ãšããŠå®£èšããŠ, implements ãšã㊠Mix-in ãã å¿ èŠããã. Ruby ã§ãããšããã®, < ãš include ã®é¢ä¿ãšåã.
Enum
èå¥åããã®ãŸãŸæééåãšããŠæã€æœè±¡ããŒã¿å. ãšãã«, Java ã§ã¯ Enum ã¯ç¹æ®ãªã¯ã©ã¹.
Effective Java ã§ã® Enum ãŸãšã.
int enum ãã¿ãŒã³
ååã€ã int å®æ°ã®ã°ã«ãŒãã宣èšããããš.ãããããŠããŠ.
public static final int FOO = 0;
public static final int BAR = 1;
- ã³ã³ãã€ã©ã«ããåæ€æ»ã®æ©æµãåããããšãã§ããªã.
- åãååãã€ãããã®ãåå空éã§åºå¥ããããšãã§ããªã.
- å€æŽã«ããåã³ã³ãã€ã«ãå¿ èŠ.
- 衚瀺å¯èœãªæååãžå€æããæ¹æ³ããªã.
- int ãš enum ã§ã¯å®å¹é床ã¯ããã»ã©ããããªã.
å®æ°åºæã¡ãœããå®è£ (constant-specific method implementation)
enum å®æ°ã«å¯ŸããŠæ¯ãèããé¢é£ä»ããããã®æ¹æ³.
ãã¿ãŒã³é©çšå.
public enum Operation {
PLUS, MINUS;
double apply (double x, double y) {
switch (this) {
case PLUS: return x + y;
case MINUS: return x - y;
}
throw new AssertionError () ("Unknown op:" + this);
}
}
ãã¿ãŒã³é©çšåŸ. enum åã§æœè±¡ã¡ãœããã宣èšããŠ, å®æ°åºæã¯ã©ã¹æ¬äœã§, å®æ°ããšã«å ·è±¡ã¡ãœããã§ãã®æœè±¡ã¡ãœããããªãŒããŒã©ã€ã.
switch æãæé€ããã®ã§, ãšã¬ã¬ã³ã!! æœè±¡ã¡ãœããã«ãã£ãŠå®è£ ãã«ãã»ã«å.
public enum Operation {
PLUS { double apply (double x, double y) {return x + y;} },
MINUS { double apply (double x, double y) {return x - y;} };
abstract double apply (double x, double y);
}
å®æ°åºæã¯ã©ã¹
ããã«, å®æ°åºæããŒã¿ãšå®è£ ãçµã¿åãããããšã§, 匷åãªè¡šçŸåã.
public enum Operation {
PLUS ("+") { double apply (double x, double y) {return x + y;} },
MINUS ("-") { double apply (double x, double y) {return x - y;} };
private final String symbol;
Operation (String symbol) { this.symbol = symbol; }
@Override public String toString () { return symbol; }
abstract double apply (double x, double y);
}
Enum ã® toString ã¯å®æ°è¡šçŸã¯ æååãžå€æããããšãã§ãã.
æŠç¥ Enum (Strategy Enum)
æœè±¡ã¡ãœãããã¯ã©ã¹ã«å€æŽããŠå€éšããäžããŠããããã«ããã°, ããã¯ãããã Strategy Pattern ã .
è©äŸ¡æŠç¥ãå€éšããäžããŠ, Operation ã¯å§è²ã§è©äŸ¡ããã.
public enum Operation {
PLUS (StrategyType.PLUS), MINUS (StrategyType.MINUS);
private final Strategy strategy;
Operation (EvaluateType type) {
this.type = type;
}
double apply (double x, double y) {
return type.evaluate (x, y);
}
// Strategy Enum Type
private enum EvaluateType {
PLUS { double apply (double x, double y) {return x + y;} },
MINUS { double apply (double x, double y) {return x - y;} };
abstract double apply (double x, double y);
}
}
enum å®æ°ãšå€ã®é¢é£ä»ãã« ordinal ãã€ãããªãããš
enum ãšé¢é£ä»ãããã int å€ãååŸããã¡ãœãããšã㊠ordinal ã¡ãœããããã.
ãããå®æ°ãšå€ãé¢é£ä»ãããšãã«ã¯, 䜿ããªã. ãªããªã, ã³ãŒãã®ä¿®æ£ã§, æ¯ãããçªå·ãå€ãããã.
public enum Number {
ONE, TWE;
public int getNumber{ return ordinal () + 1; }
}
代ããã«ã€ã³ã¹ã¿ã³ã¹ãã£ãŒã«ããå©çšããã°ãã.
public enum Number {
ONE (1), TWE (2);
private final int number;
Number (int number) { this.number = number;}
public int getNumber{ return number; }
}
éåãšéåã®å¯Ÿå¿ã¥ãã«åºæ°ã€ã³ããã¯ã¹ (é å) ãã€ãããªã
2 ã€ã®éåã察å¿ä»ãããšãã«ã¯, é åãã€ãããããããæ¹æ³ããã.
ããã¯, EnumMap. EnumMap ã¯å éšå®è£ ã¯é åã§ãããŠãããã®ã®, ã€ã³ããã¯ã¹ãæèããå¿ èŠããªããšããã¡ãªããããã.
é åãã€ã³ããã¯ã¹ããããã«åºæ°ã䜿çšããããšãé©åã§ããããšã¯ã»ãšãã©ãªã.代ããã«, EnumMap ã䜿çšããããš.
é¢é£ãå€æ¬¡å ãªãã°, EnumMap<âŠ, EnumMap<âŠ>> ãšããããã«é£ãªã£ãŠãã.
Map<Herb.Type, Set<Herb>> herbsByType =
new EnumMap<Herb.Type, Set<Herb>>(Herb.Type.class);
for (Herb.Type t : Herb.Type.values ())
herbsByType.put (t, new HashSet<Herb>());
for (herb h: garden)
herbsBytpe.get (h.type).add (h);
å éšã¯ã©ã¹: Inner Class
ä»ã®ã¯ã©ã¹ã®å éšã«å®£èšãããã¯ã©ã¹. ãã¹ãã¯ã©ã¹ãšããã.
以äžã® 4 ã€ã«åé¡ã§ãã
- static ã¡ã³ãã¯ã©ã¹
- é static ã¡ã³ãã¯ã©ã¹
- ç¡åã¯ã©ã¹
- ããŒã«ã«ã¯ã©ã¹
å éšã¯ã©ã¹ã®äž»ãªåœ¹å²ã¯, å€åŽã®ã¯ã©ã¹ã® Utility.
static ã¡ã³ãã¯ã©ã¹
ãšãã«çç±ããªãéã, å éšã¯ã©ã¹ã¯ private static ãã€ãã.
static ãã€ããªããš, 芪ã®ã¯ã©ã¹ãã€ã³ã¹ã¿ã³ã¹åããããšãšãã«, åãã€ã³ã¹ã¿ã³ã¹åããã.
ããã¯ããªãã¡, äžå¿ èŠãªæéãšã¡ã¢ãªã®æ¶è²»ã«ãªãã®ã§.
é static ã¡ã³ãã¯ã©ã¹
äž»ãªäœ¿ãã©ããã¯, Adapter ãã¿ãŒã³ãå§è²ã§å®çŸãããšã.
ãžã§ããªãã¯ã¹ãåãããŠå©çšããã.
ç¡åã¯ã©ã¹
äž»ãªäœ¿çšç®çã¯, é¢æ°ãªããžã§ã¯ããçæãããã.
Java7 以åã¯, Java ã§ã¯ããŒãžã£ãå®çŸããããã«ã¯, ç¡åã¯ã©ã¹ãå©çšããå¿ èŠããã.
Java8 ã ãš lambda ãããã®ã§, ç¡åã¯ã©ã¹ã®äœ¿ãã©ããããªããã.
ç¡åã¯ã©ã¹ã¯, ããã°ã©ã ã®éäžã«ããããã®ã§, å¯èªæ§ã®ããã«ã¯, ãããã 10 è¡çšåºŠã®çããã®ãé©å.
ããŒã«ã«ã¯ã©ã¹
ããŸãã€ãããªã.
Cuncurrency
Thread
Java 㧠Thread ãå©çšããæ¹æ³ã¯ 2 ã€ãã.
Thread ã®ç¶æ¿
Thread ã¯ã©ã¹ãå©çšãã.
public class MyThread extends Thread {
public void run () {
// code to run
}
}
Mythread myThread = new MyThread ();
myThread.start ();
Runnable I/F ã®å®è£
ã¹ã¬ãããå®è£ ããã¯ã©ã¹ãå¥ã®ã¯ã©ã¹ã®ãµãã¯ã©ã¹ã§ãªããã°ãªããªãæã« Runnable ã€ã³ã¿ãã§ãŒã¹ã䜿çšãã. Java ã¯å€éç¶æ¿ããµããŒãããŠããªãã®ã§.
public interface Runnable {
public void run ();
}
public class Myrunnable implements Runnable {
public void run () {
// code to run
}
}
Myrunnable myRunnable = new Myrunnable ();
new Thread (myRunnable).start ();
ExecuterService
ThreadPool
è€æ°ã®ã¹ã¬ããããããããäœæããŠåŸ æ©ãããŠãã, ã¿ã¹ã¯ãæ¥ããåŸ ã£ãŠããã¹ã¬ããã«ã¿ã¹ã¯ãå²ãåœãŠãŠåŠçãéå§ãããä»çµã¿ãã¹ã¬ããããŒã«ãšèšããŸã
Java Annotation
ã¯ã©ã¹ãã¡ãœãã, ããã±ãŒãžã«å¯ŸããŠã¡ã¿ããŒã¿ãšããŠæ³šéãèšå ¥ãã.
3 ã€ã®çš®é¡ããã.
- ããŒã«ãŒã»ã¢ãããŒã·ã§ã³ ã»ã»ã» ããŒã¿ãç¡ãååã ããæã€ã¢ãããŒã·ã§ã³.
- åäžå€ã¢ãããŒã·ã§ã³ ã»ã»ã» ããŒã¿ãäžã€ã ãæã€ã¢ãããŒã·ã§ã³. èŠããã¯ã¡ãœããåŒã³åºãã«äŒŒãŠãã.
- ãã«ã»ã¢ãããŒã·ã§ã³ ã»ã»ã»è€æ°ã®ããŒã¿ãæã€ã¢ãããŒã·ã§ã³.
éçºç°å¢ã»ããŒã«
Emacs
ðEffective Java
- ã Effective Java 第 2 çãæ£èª€è¡š
- Effective Java ã®ãœãŒã¹: marhan/effective-java-examples
- Effective Java ã®ãŸãšãã®ãŸãšã - Qiita
第 2 ç« ãªããžã§ã¯ãã®çæãšæ¶æ» (é ç® 1 ã é ç® 7)
第 3 ç« ãã¹ãŠã®ãªããžã§ã¯ãã«å ±éã®ã¡ãœãã (é ç® 8 ã é ç® 12)
第 4 ç« ã¯ã©ã¹ãšã€ã³ã¿ãã§ãŒã¹ (é ç® 13 ãé ç® 22)
ç¶æ¿ãããããšæµå¯ŸèŠããŠãã
-
ç¶æ¿ãããã³ã³ããžã·ã§ã³ãéžã¶ç¶æ¿ã¯äžå¿ èŠãªã¡ãœãããå ¬éãã. ã€ãŸã, ç¶æ¿ã¯ã«ãã»ã«åãç Žã.
-
ç¶æ¿ãã€ãããªãã°èšèšãææžåãã, ã§ãªããã°çŠæ¢ã .
-
æœè±¡ã¯ã©ã¹ãããã€ã³ã¿ãã§ãŒã¹ãéžã¶å®è£ ã®èŠ³ç¹ã§ã¯,
- æœè±¡ã¯ã©ã¹ã¯ã¡ãœããã«å¯Ÿããå®è£ ãå«ãããšãèš±ãããŠãã.
- ã€ã³ã¿ãã§ãŒã¹ã¯ã¡ãœããã«å¯Ÿããå®è£ ãå«ãããšãèš±ãããŠããªã.
æ©èœã®èŠ³ç¹ã§ã¯,
- æœè±¡ã¯ã©ã¹ã¯ããæ©èœã®å®è£ ã匷å¶ãã.
- ã€ã³ã¿ãã§ãŒã¹ã¯ä»»æã®æ©èœãæ··ãåããã.
éå±€åã®èŠ³ç¹ã§ã¯,
- æœè±¡ã¯ã©ã¹ã¯ç©äºãéå±€åããããšã«åªãã.
- ã€ã³ã¿ãã§ãŒã¹ã¯éå±€ãæããªããã®ããŸãšããããšã«åªãã.
ã€ã³ã¿ãã§ãŒã¹ã¯, éå±€ãæããªãåã·ã¹ãã ãæ§ç¯ãã.