ECJ (Eclipse Compiler for Java) ã¯é¢åè¦ãè¯ããâ¦
Eclipse (ã® JDT(Java Development Tools)) ã®ç¬èªã³ã³ãã¤ã© ECJ (Eclipse Compiler for Java) ã®é¢åè¦ããããã¦ãJDKä»å±ã®ã³ã³ãã¤ã© javac ã«ãé¡ããããæãããâ¦
確èªããã®ã¯ãJDK 1.6.0_20ãEclipse SDK 3.5.0 *1
BOMä»ãUTF-8ã®javaã½ã¼ã¹ãã¡ã¤ã«
UTF-8ã®è¦æ ¼ä¸ã¯ãBOMãä»ãã¦ãã¦ãOKã
ãã ãjavac ã¯ãHoge.java:1: \65279 ã¯ä¸æ£ãªæåã§ãããã¨ã¨ã©ã¼ã«ãªãã
Eclipse ã§ã¯ãåé¡ãªãã³ã³ãã¤ã«ã§ãããããã«å°ã£ããã¨ã«ãã¨ã³ã³ã¼ãã¯å¤æã§ãããBOMä»ããã®å¤æãåºæ¥ãªã(ã©ã¡ãã§ãåãããã«ã¨ãã£ã¿é¨ã«è¡¨ç¤ºããã)ã
ã¢ããã¼ã·ã§ã³ã§ã®åè¦ç´ ã®å¤ã§é åã®æå¾ã«ã«ã³ã
é常ã®é åã®å ´åã¯ãæå¾ã«ã«ã³ãããã£ã¦ãç¡è¦ãããã
String str[] = { "aa", "bb", "cc", };
è¨èªä»æ§ã§ãããå®ç¾©ããã¦ããã
A trailing comma may appear after the last expression in an array initializer and is ignored.
The Java Language Specification, Third Edition - 10.6 Array Initializers
ããããã¢ããã¼ã·ã§ã³(ã®è¦ç´ ã®å¤)ã®å ´åãjavac ã§ã¯ãHoge.java:1: å¼ã®éå§ãä¸æ£ã§ãããã¨ã¨ã©ã¼ã«ãªãã
@XmlType(propOrder = { "aa", "bb", "cc", }) public class Hoge {
é
åã®åæåãªãã ãããé常ã®å ´åã¨åæ§ã«æå¾ã®ã«ã³ãã¯ç¡è¦ããã¦ãããããªãã ãã©â¦ã
ä»£å ¥æ¼ç®åã§ã® Integer å¤æ°ã¸ã® Double å¤æ°ã®å ç®
次ã®ãããªä»£å ¥æ¼ç®åã®è¨ç®ããECJ ã§ã¯ã¨ã©ã¼ã¨ãªããªãã
Integer i = 1; Double d = 2.0D; i += d;
javac ã®å ´åã¯æ¬¡ã®ãããªã¨ã©ã¼ã«ãªãã
Hoge.java:12: å¤æã§ããªãå æ¤åºå¤ : double æå¾ å¤ : java.lang.Integer i += d; ^
A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T)( (E1) op (E2) ), where T is the type of E1, except that E1 is evaluated only once.
The Java Language Specification, Third Edition - 15.26.2 Compound Assignment Operators
ã¨ããã®ã§ããi += d;ãã¯ãi = (Integer) ( (i) + (d) );ãã®æ§ã«å±éããããã¨ã«ãªãããã®å ´åãECJ ã§ã javac ã§ãã¨ã©ã¼ã¨ãªãã
ECJ ã§ãã«ãããã¯ã©ã¹ããã³ã³ãã¤ã«ããã¨ããi = Integer.valueOf( (int)( (double)i.intValue() + d.doubleValue() ) );ãã¨ãªã£ã¦ããã
ã§ãããããã double ã®å¤ã Integer (int) å¤æ°ã«ã»ããããã®ã£ã¦å±éºãªæ°ãâ¦ã
ã¸ã§ããªãã¯ãã©ã¡ã¼ã¿ ãã®ï¼
次ã®ãã㪠generic ã使ã£ãã½ã¼ã¹ã®å ´åã
public <T> T getValue(String key) { return getValueSub(key); } public <T> T getValueSub(String key) { return null; }
javac ã®å ´åã¯æ¬¡ã®ãããªã¨ã©ã¼ã«ãªãã
Hoge.java:13: åãã©ã¡ã¼ã¿ <T>T ãå¤å¥ã§ãã¾ãã; åå¤æ° T (ä¸é T,java.lang.Object) ã®åºæã®æ大ã¤ã³ã¹ã¿ã³ã¹ãåå¨ãã¾ããã return getValueSub(key); ^
ãreturn this.
ã¸ã§ããªãã¯ãã©ã¡ã¼ã¿ ãã®ï¼
ãã®ï¼ã¨ä¼¼ã¦ããã©ãããªããã£ãåã§åãåãå ´åã
private Object value; private <T> void setValue(T value) { this.value = value; } private <T> T getValue() { return (T) value; } private void exec() { setValue(123); int result = this.getValue(); System.out.println(result); }
ãã®ï¼ã¨åæ§ã«ãthis.
æ¡å¼µforæ (foreachæ) ã®ãã£ã¹ã
ã³ã³ãã¤ã«æã¨è¨ããããçæãããã¯ã©ã¹ãã¡ã¤ã«ã®éãã ãã
次ã®ãããªã½ã¼ã¹ãããããã§ã³ã³ãã¤ã«ãå®è¡ããå ´å(generic ã®è¦åã¯ç¡è¦)ã
public static void main(String[] args) { ArrayList list = new ArrayList<Long>(); list.add(1L); list.add(2L); ArrayList<Double> dlst = list; for(Number num : dlst) { System.out.print(num); System.out.println(); } }
Eclipse ã§ãã«ãããå ´åã¯æ£å¸¸ã«å®è¡ã§ãããã©ãjavac ã§ãã«ãããå ´åã¯ãException in thread "main" java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Doubleãã¨ä¾å¤ãçºçãã¦å®è¡ã§ããªãã
ãã«ããããã¯ã©ã¹ããããããã³ã³ãã¤ã«ããã¨ä»¥ä¸ã®ããã«ãªãã
// from ECJ for(Iterator iterator = dlst.iterator(); iterator.hasNext(); System.out.println()) { Number num = (Number)iterator.next(); System.out.print(num); }
// from javac for(Iterator iterator = arraylist1.iterator(); iterator.hasNext(); System.out.println()) { Double double1 = (Double)iterator.next(); System.out.print(double1); }
ãããã iterator.next() ããå¾ãå¤ã®ãã£ã¹ãã§ãECJã¯ãforeachæã§æå®ããåãã«ãjavacã¯ãå¤æ°(ã®åç·ç§°)ã®åãã«ãã¨ãªãã
Enum<?> ã¸ã®ãã£ã¹ã
â»TBãããã£ã¦ç¥ã£ãã®ã§è¿½è¨ããã£ã¨ã 6u18 ã§ä¿®æ£ããã¦ãã®ã§ãä¸è¨ã®ç¢ºèªããç°å¢ã§ã¯åç¾ã§ããªãï¼6u01 ã§ã¯ç¢ºèªï¼ã
public class EnumCast { private enum Planet { EARTH, MARS } public static void main(String[] args) { Enum<?> e1 = Planet.EARTH; // Hmm, doesn't compile Planet e2 = (Planet) e1; // Well this compiles, but it's not pretty! Planet e3 = (Planet) (Object) e1; // So does this .... Planet e4 = (Planet) (Enum<?>) e1; System.out.println("e=" + e1 + e2 + e3 + e4); } }
ãEnum<?> e1 = Planet.EARTH;ããã(Planet) (Enum<?>) e1ãã¨è¨ã£ãã¨ããã§ãå¤æã§ããªãåãã¨ãªãã
ãã£ã¨ããEnum<?>ãã¨ããã«ãEnumãã¨ããã¨ã¨ã©ã¼ã«ãªããªãã®ã§ããã®ããã javac ã®ãã°ãããæ°ãâ¦ã
åè
- ãªã Eclipse 㯠JRE ã使ã£ã¦ããã®ã«ã³ã³ãã¤ã«ã§ããã®ï¼ - MyEclipse ããã°
- Eclipse ã§ã³ã³ãã¤ã«ããéã® Java æºæ ãã¼ã¸ã§ã³è¨å® - MyEclipse ããã°
- ç¶ã»æ¨è«ãããåãã©ã¡ã¼ã¿ããClassãå¾ã(JDKã®ã³ã³ãã¤ã©ã§ã¯åä½ããªãã£ãã»ã»orz) - ããªã®æ¥è¨
- Eclipseããéããã©antã®javacã¿ã¹ã¯ããéããªãè¨è¿° - ãä»äºã®åå¿é²ã¿ãããªãã®
- JDTã³ã³ãã¤ã©ãªãOKã ãã©ãç´æ£ã³ã³ãã¤ã©ããéããªãè¨è¿° - ãä»äºã®åå¿é²ã¿ãããªãã®
- Enumã«ãã£ã¹ããããã¨ããã¨ã³ã³ãã¤ã«ã¨ã©ã¼å¤æã§ããªãåãçºçãã: éªç¾½ã®çºç«å¾å¿å¤±
- NetBeans7ä¸ã§JDK7ãç¨ãã¦Eclipseã¨çµæãç°ãªã£ã¦ããã³ã³ãã¤ã©ã®æåã確èªãã: éªç¾½ã®çºç«å¾å¿å¤±