Java基礎文法最速マスター - いろいろ解析日記ããªã¹ãã¯ãã
Javaã®å¤æ ææ³ã»ææ³ä¸è¦§ã§ããJavaã®åºç¤ãããç¨åº¦ç¥ã£ã¦ãã人ã¯ãããèªãã°Javaã®å¤æ ããã¹ã¿ã¼ãã¦Javaãæ¸ããã¨ãã§ããããã«ãªã£ã¦ãã¾ããç°¡æãªãã¡ã¬ã³ã¹ã¨ãã¦ãå©ç¨ã§ããã¨æãã¾ãã®ã§ãããã¯è¶³ããªãã¨æããã®ãããã°æãã¦ãã ããã
1.åºç¤
ã¨ã³ã¯ãã¼ã¸ã³ã°åå é¨classã®ä½æ
å¤é¨classã®ã¤ã³ã¹ã¿ã³ã¹ã«ç´ä»ãã¤ã³ã¹ã¿ã³ã¹ãçæãã¾ããå¤é¨ã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ - å é¨ã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹éã«ãã¯ã©ã¹ - ã¤ã³ã¹ã¿ã³ã¹ã®ãããªé¢ä¿ãæããããã¨ãã§ãã¾ãã
public class Outer { public class Inner { } }
ã¨ãããããªã¯ã©ã¹ãä½ã£ãå ´åã
Outer o = new Outer(); Inner i = o.new Inner();
ã¨ãªãã¾ããnewæ¼ç®åã®åã«å¤é¨ã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ãè¨è¿°ããããã§ããoã®ä»£ããã«thisã渡ãå ´åã¯this.newã¨ããè¨è¿°ã«ãªãããã§ãããthis.ã¯çç¥ã§ããããOuterã¯ã©ã¹ã®ã¡ã½ããã§Innerãnewããå ´åã«ã¯çç¥ãã¦åã«new Inner()ã¨æ¸ããã¨ãåºæ¥ã¾ãã
public class Outer { public class Inner { public class Inner2 { public class Inner3 { } } } public void hoge() { Inner i = new Inner(); //this.newãçç¥ãã¦newã¨æ¸ãã Inner.Inner2 i2 = i.new Inner2(); Inner.Inner2.Inner3 i3 = i2.new Inner3(); } }
ãã¡ãããå¤é層ã«ãããã¨ãã§ãã¾ããé£éçã«å¼ã³åºãã¨é¢ç½ãã§ãããã
new Outer().new Inner().new Inner2().new Inner3();
mainã¡ã½ããã®ä½æãªãã§ã®ããã°ã©ã ã®å®è¡
main()ã¡ã½ãããå¼ã³åºãããåã«classã®åæåãèµ°ãããããã®ãããã«ã³ã¼ããæ¸ããã¨ã§mainã¡ã½ããããªãã¯ã©ã¹ããå¦çãå®è¡ãããã¨ãåºæ¥ã¾ãã
public class Starter { static { System.out.println("Hello!"); } }
ä¸è¨ãµã³ãã«ã¯ãªã¼ã½ããã¯ã¹ãªstaticåæåãããã¯ãç¨ããæ¹å¼ã§ããã·ã§ã¼ãã³ã¼ãã£ã³ã°ããããã¯ã³ã¼ãã´ã«ãã§ã¯å®çªã®ææ³ã§ãããã³ã³ã½ã¼ã«ããã®å®è¡ãè¡ãã¨ä»¥ä¸ã®ããã«ãªãã¾ãã
C:\hoge>java Starter Hello! Exception in thread "main" java.lang.NoSuchMethodError: main
ã¯ã©ã¹ã®åæåã®æ®µéã§Hello!ãåºåããããã®å¾ã«NoSuchMethodErrorã§public static void main()ããªããã¨æããã¾ãã
staticãªåæåãè¡ããããã§ã¼ã¹ã§å¼ã³åºãã°å®è¡ã¯å¯è½ãªã®ã§staticåæåãããã¯ä»¥å¤ã«ãstaticãã£ã¼ã«ãã®åæåã§ã¡ã½ãããå¼ã³åºããªã©ã®ææ³ã使ãã¾ãã
public class Starter { static int i = hoge(); private static int hoge() { System.out.println("Hello!"); return 0; } }
System.out.printlnã¡ã½ãã
System.outãã£ã¼ã«ãã¯System.setOut()メソッドã§ç½®ãæãããã¨ãã§ãã¾ãã
java.io.PrintStreamクラスãç¶æ¿ããã¯ã©ã¹ãä½æãã¦ãã«ããã«ããã¦ããã¨ãSystem.out.println()ã¡ã½ãããããªã¬ã¼ã«ããããè¡ããã¨ãåºæ¥ã¾ãã
æ¨æºåºåã¸ã®æ¸ãåºããããå ´åã«èªå°¾ã«ãã«ãããã¤ããã¨ãããããæ¾é¡ã§ãã
ã³ã¡ã³ã
//ã«ããã³ã¡ã³ãã¯è¡æ«ã¾ã§ãæå¹ç¯å²ã§ãã//ã³ã¡ã³ãä¸ã«ã¦ãã³ã¼ãã¨ã¹ã±ã¼ãããæ¹è¡æåãç½®ããã¨ã§ã³ã¡ã³ãã«å½è£ ãã¦ããã°ã©ã ãè¨è¿°ãããã¨ãåºæ¥ã¾ãã
ã³ã¡ã³ãä¸ã®ã¦ãã³ã¼ãã¨ã¹ã±ã¼ãã¨ããããªãã¯ã使ã£ããã¿ã¨ãã¦ã¯Javaã§ãã¤ã³ã¿æ¼ç®åã使ãã¨ã¤ããªã«ãã¼ã«ãã¿ãªã©ãé¢ç½ãã§ããã
public class Test { public static void main(String[] args) { int i = 5; // #start unsafe# http://java.sun.com\u000a\u002f\u002a int *ip = &i; // ãã¤ã³ã¿ã®å 容ã«3ãå ç® *ip += 3; // æ¼ç®çµæãiã«ä»£å ¥ i = *ip; // #end unsafe# http://java.sun.com\u002a\u002fi+=\u0033\u003b System.out.println(i); } }*演算子を用いてJavaでポインタを扱う
å¤æ°ã®å®£è¨
å¤æ°ã®å®£è¨ã«ãã¢ããã¼ã·ã§ã³ãã¤ãããã¨ãåºæ¥ã¾ããã¾ãã@Targetã«LOCAL_VARIABLEãå«ãã¢ããã¼ã·ã§ã³ãä½æãã¾ãã
import java.lang.annotation.Target; import java.lang.annotation.ElementType; @Target(ElementType.LOCAL_VARIABLE) public @interface Hoge { }
ãã®ã¢ããã¼ã·ã§ã³ã¯ãã¼ã«ã«å¤æ°ã®å®£è¨ã«ã¤ãããã¨ãã§ããã®ã§
@Hoge Hoge Hoge = Hoge();
ã®ãããªã³ã¼ããæ¸ããã¨ãã§ããããã«ãªãã¾ãã*1
ãã£ã¼ã«ã宣è¨
transient ãã¼ã¯ã¼ããvolatile ãã¼ã¯ã¼ããã¤ãããã¨ã§ãåå¯éãã®ã§ãã¨ãããç¨åº¦ã®è»½å¾®ãªãããããããã¾ãããã¸ã§ããªã¯ã¹ã®åå¤æ°ãæ±ç¨æ§ãé«ãå¹æçã ã¨æãã¾ãã
public class Hoge<Hoge> { Hoge Hoge; }
ããã§å®ç¾©ãããHogeãã£ã¼ã«ãã¯åHogeã§ã¯ãªããåå¤æ°Hogeã§ããã¨ããããã¤ã³ãã§ããã
ããã°ã©ã ã®ã³ã³ãã¤ã«
javacã³ãã³ãã«ã¯å¹¾ã¤ããªãã·ã§ã³ãããã¾ããOracle Technology Network for Java Developers | Oracle Technology Network | Oracle
ãªãã·ã§ã³ã«-g:noneã使ç¨ããã¨ãããã°æ å ±ãåºåãããªããããã¹ã¿ãã¯ãã¬ã¼ã¹ã§è¡çªå·ãunknownã«ãªãã¾ãã大æµã®æ°äººã¯ãã®ç°å¢ã§ãããã°ãããããã¨æ³£ããããã¨ãã§ãã¾ãã
ãªãã·ã§ã³-processor ã§アノテーションプロセッサを動かすãªã©ããã¨æ®éã«æçãªãã¨ãã§ãã¾ããã¢ããã¼ã·ã§ã³ããã»ããµãæªç¨ãã¦ç¡çãªãã¨ãããã¢ã¤ãã¢ãç·´ãã®ãããã§ãããã
ããã°ã©ã ã®å®è¡
javaã³ãã³ãã«ãå¹¾ã¤ãã®ãªãã·ã§ã³ãããã¾ããOracle Technology Network for Java Developers | Oracle Technology Network | Oracle
ãªãã·ã§ã³-Dãå©ç¨ãã¦システムプロパティãããããã¨ãã§ãã¾ããããããã¯path.separatorã®æ¸ãæãã§ããããã§æ¬æ¥ã®è¨å®ãã¡ã¤ã«ã¨ã¯éããã¹ã®ãã¡ã¤ã«ãèªã¿è¾¼ã¾ãããªã©ããã¨ãããã°ä½æ¥ã¯å¤§ãã«æ··ä¹±ãããã¨ã§ãããã
ãªãã·ã§ã³-javaagentãå©ç¨ãã¦エージェントãèµ·åãããããããã¨æªãããããã¨ããã®ã楽ããã§ãããã
2.æ°å¤
æ°å¤ã®è¡¨ç¾
æ°å¤è¡¨ç¾ã§æ³¨ç®ãã¹ãã¯8é²æ°ãªãã©ã«ã§ãããã
System.out.println(012);
10
ç¹å¥ãªè¨å·ãç¨ãããããªãããèµ·ãã¦ããäºè±¡ãæ¤ç´¢ãã«ãããããã«å ¥ãã°é·èããããã¨ãåºæ¥ã¾ãã
ååæ¼ç®
è¦ãã¦ããããã®ã¯Double.NaNã§ããdoubleåã®å¤ã®1種ã§ãæ¥æ¬èªã§ã¯éæ°ã¨å¼ã°ãã¾ãã 0.0 / 0.0 ã¨ãã£ãæ¼ç®ãè¡ãã¨çºçãã¾ããNaNã¨ä»ã®å¤ãæ¼ç®ããã¨çµæã¯NaNã¨ãªãã¾ããå¦çããã¼ä¸ã§å¤ã«NaNãç´ãè¾¼ãã¨ã以å¾ã®å¤ããã¹ã¦NaNã«ãªãã®ã§åå ç®æãçªãæ¢ããã®ã«é£åãã¾ãã
ã¾ããNaNã¨NaNã==ã§æ¯è¼ããå ´åãfalseã¨ãªãã®ãåµãã©ããã§ããããããå©ç¨ãã¦JAVA PUZZLERSã§ã¯ä»¥ä¸ã®ãããªåºé¡ãããã¦ãã¾ãã
ããºã«29 : ã«ã¼ãè·äººã®è±å«(Bride of Looper)
ãã®ã«ã¼ããç¡éã«ã¼ãã¨ãªãiã宣è¨ããªããã
while(i != i) {
}
ã¤ã³ã¯ãªã¡ã³ãã¨ãã¯ãªã¡ã³ã
åç½®ã®ã¤ã³ã¯ãªã¡ã³ãã¯ç¹è¨äºé ã¯ãªãã§ãããå¾ç½®ã®ã¤ã³ã¯ãªã¡ã³ãã¯åå¿è 殺ãã§ãããã¤ã³ã¯ãªã¡ã³ããè©ä¾¡ã®å¾ã«è¡ããããã¨ããã®ãå¦çé åºã®ã²ã·ã¥ã¿ã«ãå´©å£ãèªãã¾ãã
int i = 0; i = i++ + ++i; System.out.println(i);
3. æåå
æååã¨Objectã®+æ¼ç®åã«ããé£çµãªã©ã§ã¯Object#toString()ã¡ã½ãããç¨ãããã¾ããEclipseãªã©ã®ãããã¬ã§å¤æ°ã®å¤ã確èªããéã«ããã®å¤ãç¨ãããã¾ãã®ã§ãä¸é©åã«ãªã¼ãã¼ã©ã¤ããã¦ãããã¨ã§ãããã°ãæ··ä¹±ããããã¨ãã§ãã¾ãã
4. é å
Java5以éã§ã¯é åã使ãæ©ä¼ããã£ããæ¸ãã¾ããããå¯å¤é·å¼æ°ã§ç¨ããããã*2ã¨ä½¿ãæ©ä¼ã¯ã¾ã æ®ã£ã¦ãã¾ããã
public class Hoe<E> { private Class<E> type; public Hoe(E... e) { @SuppressWarnings("unchecked") Class<E> type = (Class<E>) e.getClass().getComponentType(); this.type = type; } public Class<E> getType() { return type; } }ããããã¨ãå ·ä½çãªåãã©ã¡ã¼ã¿ãåå¾ã§ããã
ä¾ãã°
Hoe<Fuga> hoe = new Hoe<Fuga>();
ã¨ããæããã£ãå ´åãhoe.getType()ã¯Fuga.classã«ãªãã
型パラメータつきクラスの中から指定された型パラメータを知る方法 - Skirnirnismal
ãã¡ãã®ã¨ã³ããªã§ç´¹ä»ããã¦ããææ³ã¯ãã³ã³ã¹ãã©ã¯ã¿ã«åå¤æ°ã®å¯å¤é·å¼æ°ãã¨ããã¨ã§ãnew Hoe
ãªããé åã¨ã¯é¢ä¿ããã¾ããããç¶æ¿ã®ããªãã§åå¤æ°ã«ä½åããã¤ã³ããããããå¾ãã«ã¯ジェネリックなクラスの階層を遡って適用された具象型を得るã§ç´¹ä»ããã¦ãããããªãªãã¬ã¯ã·ã§ã³ãç¨ããææ³ã使ãã¾ãã
5.ããã·ã¥
Object#hashCode()ã¨Object#equals()ã®å®è£ 次第ã§ã¯æ¨æºAPIã®java.util.HashMapãªã©ãåä½ä¸è¯ã«è¿½ãè¾¼ããã¨ãã§ãã¾ãã
java.lang.Comparableãããã¯java.util.Comparatorã®å®è£ ãä¸é©åã§ç価ã§ã¯ãªããªãã¸ã§ã¯ãã«å¯¾ãã¦0ãè¿ããããªå®è£ ã«ãªã£ã¦ããå ´åãjava.util.TreeMapã¯putãããªãã¸ã§ã¯ããæ¶æ» ããã¾ãã
ãã®ãããªæåã¯ãããã°ãã«ããããã®æ§è³ªãå©ç¨ããã³ã¼ãã¯è§£èªå°é£ã¨ãªããã¨ã§ãããã
6.å¶å¾¡æ
ifæ
æ¡ä»¶å¼ã«^æ¼ç®åï¼XORï¼ãç¨ãããã¨ãã§ãã¾ãããããããã·ãã¥ã¨ã¼ã·ã§ã³ãå°ãªãã®ã§å©ç¨ã®ãã£ã³ã¹ãéããªãããã«ãã¦ãã ããã
if (a == null ^ b == null) { throw new NullPointerException(); }
foræ
å¶å¾¡æã§ç¥å度ãä½ããã®ã¯ã©ãã«ã§ããããforæãwhileæã§ã¯breakãcontinueãå©ç¨ã§ãã¾ãããå¤éã«ã¼ãã®éã«ã¯ã©ã®ã«ã¼ãã«å¯¾ãã¦breakã»continueããããæ示ããããã«ã©ãã«ãç¨ãã¾ãã
yLoop: for (int y=0; y<height; y++) { xLoop: for (int x=0; x<width; x++) { if (map[x][y] == Type.BOMB) { break yLoop; } } }
ã¡ããã©URLã®æ¸å¼ãã©ãã«ï¼//ã³ã¡ã³ãã®å½¢å¼ã«ãªãã®ã§
http://example.jp/ for (int i=0; i<size; i++) { }
ã¨ãã£ããããªè¨è¿°ãã§ãã¾ãã
ãããã¯
breakã¯ã«ã¼ããããªãã¨ãå©ç¨å¯è½ã§ãåãªããããã¯ã«ã©ãã«ãã¤ãã¦breakããããã¨ãã§ãã¾ãã
hoge: { piyo: { break hoge; } }
7.ã¡ã½ãã
strictfpãã¼ã¯ã¼ããã¤ãã¦é¦ãå¾ãããããã¨ãã§ãã¾ãã
åãããã«è¦ãã¦ãsynchronizedãã¼ã¯ã¼ãã®å ´åã¯ããµãã®ã§ããã¾ãããã*3
8.ä¾å¤å¦ç
finallyç¯ã§ã®returnãthrowã¯ããè¨ç·´ãããJavaããã°ã©ãã§ãªãã¨æåãäºæ¸¬ã§ããªããã¨ã§ãããã
9.ãªãã¬ã¯ã·ã§ã³
é»éè¡ã®åºç¤ã®ã²ã¨ã¤ã§ããjava.lang.reflectパッケージãå©ç¨ãããã¨ã§ãåçãªãã£ã¼ã«ãåç §ãã¡ã½ããå¼ã³åºããè¡ãã¾ããsetAccessibleメソッドãå©ç¨ããã°privateã¡ã½ããã§ããå¤é¨ããå¼ã³åºããã¨ãã§ãã¦ãã¾ãã¾ãã
10.ã¯ã©ã¹ãã¼ãã¼
ã¯ã©ã¹ãã¼ãã¼ãèªä½ãããã¨ã§ãå®è¡æã«ã¯ã©ã¹ã®å
容ãæ¸ãæãã¦ãã¾ããã¨ãã§ãã¾ãã
defineClassメソッドã§byteé
åã®ç¶æ
ããClassãçæããã®ã§ããããªã¼ãã¼ã©ã¤ããã¦byteé
åã«ç´°å·¥ãããããã¨ãå¯è½ã§ãã
注æäºé
ãããã®ææ³ã¯é常ç¨ããã¹ãã§ã¯ãªãå¤æ çãªãã¯ããã¯ãå¤ãå«ã¿ã¾ãããå©ç¨ã¯èªå·±è²¬ä»»ã§ãé¡ããã¾ãã
é¢é£ã¨ã³ããª
æéãã¹ã¿ã¼ã·ãªã¼ãºã®æ¬å®¶
Perl基礎文法最速マスター - Perl入門ゼミ
ãªã¹ãã¯ãå
Java基礎文法最速マスター - いろいろ解析日記
æéãã¹ã¿ã¼ã·ãªã¼ãºã®ã¾ã¨ãã¯ãã¡ãã解ãæãã¨æãã¾ãã
はてな的プログラミング言語人気ランキング - Life like a clown
*1:é常ãJavaã®å½åè¦ç´ã§ã¯å¤æ°åã¯å°æåå§ã¾ãã ããããã§ã¯æ¢ãã¦Hogeã¨ãããã¨ã§ã¢ããã¼ã·ã§ã³åãååãå¤æ°åãã³ã³ã¹ãã©ã¯ã¿åã¨ã4ã¤ã®Hogeã«å¥ã ã®å½¹å²ãããã¦ãã
*2:Java5以éã§ã¯ã¸ã§ããªã¯ã¹ã主ä½ã¨ãã¦ä½¿ãã®ã§é åããListãç¨ããã®ãé常ã ããå¯å¤é·å¼æ°ã¯é åã§ã®åã渡ãã¨ãªã£ã¦ããã®ã§çã 使ãåæãæªã
*3:synchronizedã¯ã¡ã½ããã«ãã¼ã¯ã¼ãã¨ãã¦ã¤ããã¨ããã¯ãªãã¸ã§ã¯ããé²åºããã®ã§é²è¡çããã°ã©ãã³ã°ããããªãã¡ã½ããå é¨ã§synchronizedãããã¯ãç¨ããããªã®ã§synchronizedãã¼ã¯ã¼ãã使ãã¨ãããããã¤ä¸¦åå¦çããã£ã¦ããï½ãã¨ãè¨ããããã¾ãã