åã®å®éã®å
Javaã®å¯èœæ§ã«ã€ããŠå°ãè±ç·ããæ°åè¡ã®ãµã³ãã«ã³ãŒããèªãã åŸãåJavaããã°ã©ããäœãåŠçã§ããããç¥ããããšæããŸããã ããã§ã¯ãJavaã§ã®ããªããã£ãåãã¯ã©ã¹ïŒããã€ãã®åºæ¬çãªãã®ãå«ãïŒãæ¯èŒããã©ã¡ãŒã¿ãŒã®åãæž¡ããããã³åçŽãªæ§é ã«ã€ããŠè©±ããŸãããã
åºæ¬ã¿ã€ã
Java 8ã®ãããïŒ
- ããŒã«å€;
- tsiferkiïŒbyteãcharãshortãintãlong;
- éæŽæ°ã¿ã°ïŒfloatãdoubleã
ããã«ãvoidããŒã¯ãŒãããããŸããjavaã§ã¯ãããã¯åã§ã¯ãªããé¢æ°åã®åã§ã®ã¿äœ¿çšã§ããäœãè¿ããªãããšã瀺ããŸãã
è«ççã«æ³å®ãããããŒã«å€ã¯ãtrue / falseãbyte--128..127ïŒ1ãã€ãïŒãchar-0..65536ïŒ2ãã€ãïŒãshort--32768..32767ïŒ2ãã€ãïŒã«ãªããŸãã
4ãã€ãã®intã§ã¯ãå°ããª10åã§æ倧2åã8ãã€ãã®long-æ倧9 * 10 18ã§çªå·ãè©°ã蟌ãããšãã§ããŸãã
éæŽæ°ã®floatãšdoubleã¯ããããã4ãã€ããš8ãã€ãã§æ§æãããŸãïŒè©³çŽ°ã¯ãã¡ã ïŒã
ã芧ã®ãšããã笊å·ãªãã®åã¯charã®ã¿ã§ãã ã·ã³ãã«ã¯ãã®äžã«ä¿åãããããã«ãŠãã³ãŒãã§ä¿åãããŸãã
ãããã®ã¿ã€ãã¯ãã¹ãŠå€ã«ãã£ãŠã®ã¿æž¡ãããReflectionã¡ã«ããºã ã«ã¯ããŒã«ããªãå Žåããintãžã®åç §ãã¯ãªãããããŸããã
åã¿ã€ãã®ãµã€ãºã¯æããã«å¶éãããŠãããããã©ã®Javaãã©ãããã©ãŒã ã§ãåãããã«äœ¿çšã§ããŸãã å¯äžã®äŸå€ã¯ãæåã®J2MEä»æ§ã«æŽæ°ä»¥å€ã®æ°å€ãå«ãŸããŠããªãã£ãããšã§ãããçŸåšã§ã¯ãã¹ãŠãããã§åé¡ãããŸããã
äžè¬çã«ã¯ã
byte x = 100;
byte y = 100;
int i = x + y;
* This source code was highlighted with Source Code Highlighter .
ããªãã¯æ£ç¢ºã«200ãååŸããä»ã®äœãã¯ååŸããŸãã-å€æ°ã¯èªåçã«ç®çã®åã«å€æãããŸãã
æŽæ°å€æ°ã®å€ãèšå®ããã«ã¯ãããã€ãã®æ¹æ³ããããŸããäŸïŒ
byte a;
a = 1;
a = 07; //
a = 0x7; //hex
a = 'a' ; //
a = 1.0; // - float/double
* This source code was highlighted with Source Code Highlighter .
ãããã®ã¿ã€ãã¯åºæ¬ãŸãã¯ããªããã£ããšåŒã°ããç¶æ¿ããããšã¯ã§ããŸãããå®éãJavaã®ãªããžã§ã¯ãã§ã¯ãããŸããã ãã®ã¿ã€ãã®æ¬è³ªã«é¢ããæ å ±ã¯ããã®æå³ãé€ããŠåãåãããšã¯ã§ããŸããã ã¯ããããããäžè¬çã«ã
åºæ¬ã¿ã€ãã®æäœ
ãããã§äœãã§ããŸããïŒ
- =;ãä»ããŠå€ãå²ãåœãŠãŸãã
- == andïŒ=ïŒçãããªãïŒã§æ¯èŒããŸãã
ããŒã«å€ä»¥å€ã®ãã¹ãŠã®äººïŒãã ãcharã§ãïŒïŒ
- ++ãä»ããŠã€ã³ã¯ãªã¡ã³ã/ãã¯ãªã¡ã³ã/-;
- æãããªæ¹æ³ã§ã®å¢å /æžå°/ä¹ç®/å ±æïŒåå²-/ïŒ;
- ïŒ ã§é€ç®ã®æ®ããèŠã€ããŸãã
- ïŒfloat / doubleãé€ãïŒ>>ããã³<<ã䜿çšããã·ããã
- ïŒfloat / doubleãé€ãïŒïŒã|ã^ïŒxorïŒããïŒnotïŒãä»ããŠãã€ããªããžãã¯ãé©çšããŸãã
ããŒã«å€ã®å Žå-&&ã||ã^ ã! ïŒãªãïŒã
ã¯ã©ã¹
Javaã®ã¯ã©ã¹éå±€ã¯Objectã§å§ãŸããŸãã Javaéçºè ã«ãããšãåãªããžã§ã¯ãã¯äœãã§ããŸããïŒ
- ãªããžã§ã¯ãã¯ãgetClassã³ãã³ãã䜿çšããŠã¯ã©ã¹ãè¿ãããšãã§ããŸãã
- æ¯èŒã®ããã«ãequalsã¡ãœãããšhashCodeã¡ãœããããããŸãã
- ãªããžã§ã¯ãã¯ãcloneã¡ãœããã䜿çšããŠèªåèªèº«ã®ã¯ããŒã³ãäœæã§ããå ŽåããããŸãã
- ãªããžã§ã¯ãã¯toStringã¡ãœããããªãŒããŒã©ã€ããããããã°ã®ããã«èªèº«ã«é¢ããã¹ããŒãæ å ±ãè¿ãããšãã§ããŸãã
ãŸããfinalizeã¡ãœããããããŸããããã¯ãæ»ã¬åã«ãªããžã§ã¯ãããéºèšãæžããå Žåã«åå®çŸ©ããå¿ èŠããããçŸåšã®ã¹ã¬ãããç·©å/ãŠã§ã€ã¯ã§ããã¹ã¬ãããæäœããããã®äžé£ã®ã¡ãœããã§ãã
ãããåŠçããæ¹æ³ã¯ïŒ
ãã®ãããäžé£ã®åºæ¬åãšããããã®åãé€ããã¹ãŠãç¶æ¿ãããã¯ã©ã¹ããããŸãã 幞ãã«å¿ èŠãªãã®ã¯ä»ã«ãããŸããïŒ
ããããçµåãããã®ãå¿ èŠã§ãã ã©ãããŒã¯ã©ã¹ãæºãããŸãã
ãã©ã€ã ïŒããã³ããŒã«å€ïŒããªããžã§ã¯ããšããŠåŠçã§ããããã«ããããã«ãã©ãããŒã¯ã©ã¹ãèæ¡ãããŸããã
ååããïŒByteãShortãIntegerãLongãFloatãDoubleãBooleanãCharacterïŒãååå€æŽããããå¯äžã®ã¿ã€ãïŒã
Javaã®æ°ããããŒãžã§ã³ã§ã¯ãããããããªããã£ãã¿ã€ããšäžŠè¡ããŠäœ¿çšã§ããå®å šã«ééçã§ãã
Integer x = 1;
Boolean a = false ;
* This source code was highlighted with Source Code Highlighter .
å€ãããŒãžã§ã³ã§ã¯ãããªããã£ãåãã©ãããŒã§ãã©ãããããŠããããããããã¢ã³ã©ãããããå¿ èŠããããŸããã ããšãã°ã次ã®ããã«ïŒ
Integer x = new Integer(1);
int r = x.intValue();
* This source code was highlighted with Source Code Highlighter .
æ¯èŒ
å€ããå°ãªããã®æ¯èŒã¯æ°åã®ããã ãã§ããããã®åäœã®ã¡ã«ããºã ã¯æããã§ãã å¹³çã¯ããèå³æ·±ããã®ã§ãã
Javaã«ã¯ããªããžã§ã¯ãã®ç䟡æ§ãæ¯èŒãã2ã€ã®æ¹æ³ã==ãšequalsã¡ãœããããããŸãã
==ã¯ããªããã£ãåã«äœ¿çšãããŸãã ãªããžã§ã¯ãã䜿çšããå Žåãè³¢ã人ã¯è³ãbeatãããŸãã¯ããããæ¹æ³ã§æè¬ããŸãããªããžã§ã¯ã==ã®å Žåãããã¯åç §ã«ããæ¯èŒã«ãããŸããã ãªããžã§ã¯ãã¯ã1ã€ã®ãªããžã§ã¯ãã§ãããç°ãªããã®ã§ãã£ãŠã¯ãªããŸããïŒåããã£ãŒã«ããªã©ãããå Žåã§ããåããªããžã§ã¯ãã§ã¯ãããŸããïŒã
æ®ãã«ã€ããŠã¯ã.equalsã¡ãœããã䜿çšããå¿ èŠããããŸãã
ããã«ãhashCodeã¡ãœããã¯ïŒçè«çã«ïŒåãç®çã§æ©èœããŸãã çãããšåå®çŸ©ããå Žåã¯ãåå®çŸ©ããããšããå§ãããŸãã
äºå®ã¯ããªãŒããŒã©ã€ãããããšã¯ãæ¯èŒã®ããã®ç¬èªã®ã«ãŒã«ãæãã€ãããšã§ãã ããšãã°ãã¯ã©ã¹ã®ãã£ãŒã«ãã®äžéšã®ã¿ãèæ ®ã«å ¥ãããããã®ãã£ãŒã«ãã®ã¿ãæ¯èŒã«äœ¿çšã§ããŸãã
æ¯èŒã®é»éåŸïŒ
äžéšã®ãªããžã§ã¯ãaããã³bãåæåããåŸãåŒa.equalsïŒbïŒãtrueãè¿ãå Žåãa.hashCodeïŒïŒã¯b.hashCodeïŒïŒãšçãããªããã°ãªããŸããã
ãªããžã§ã¯ãã«==ãæžãããšãäžå¯èœãªçç±
å¯äžã®==ããŒã«ã¯ãããã£ãã·ã¥ããããŠãããããæ°åãšæååã§ãã ããã ãã§ã¯ãããŸããã åæŽæ°rrr = 1; ããã¯Javaã®å Žåãšåãå€æ°ã§ãããéãããç¯å²ã®å€ã§æ©èœããŸãã èšæ¶ãæ£ãããã°ã127ãã倧ãã-128ããå°ããå€ã¯ãã£ãã·ã¥ãããŸããã
ããããŠPHP 0ã§ã¯ '0'ããšããã¹ã¿ã€ã«ã§ãžã§ãŒã¯ãèŠãããã§ããïŒ
Integer a = new Integer(1);
Integer b = new Integer(1);
System. out .println(a>b);
System. out .println(a<b);
System. out .println(a==b);
* This source code was highlighted with Source Code Highlighter .
ãã®å¥è·¡ã¯ããªãã«3ååœããè¿ããŸãã åäžã®å€ãæã€æ°ãããªããžã§ã¯ãã®äœæãæ瀺çã«ç€ºããåç §ã«ããæ¯èŒãfalseãè¿ããããã§ãã
ãŸããæåã®2ã€ã®ã±ãŒã¹ã§ã¯ãæ¯èŒæäœãããªããã£ãåã«å¯ŸããŠã®ã¿å®çŸ©ãããŠãããããå±éãçºçããŸããã
é埳ïŒ==ããªããã£ãåã®ã¿ã
ããšããã§ãç·ã
ç§ããŸã èšåããªãã£ãããããªãã¯ãã§ã«æšæž¬ããïŒJavaã«ã¯ã¯ã©ã¹æååããããŸãã
ç°¡åãªäœ¿çšïŒ
String a = "hi!" ;
String b = new String ( "hi!" );
* This source code was highlighted with Source Code Highlighter .
ããã§ãããããã«ãåæåããæ¹æ³ã¯2ã€ãããŸãã2ã€ç®ã®æ¹æ³ã¯åé·ã§ãããè¡ããæ°ãããããšãä¿èšŒããŸãã
å³å¯ã«èšãã°ãJavaã®äºéåŒçšç¬Šã®æ§é ã¯ããã§ã«æ°ããè¡ãäœæããã³ãã£ãã·ã¥ãããŠããŸãïŒä»¥äžãåç §ïŒã
ããã«ãæååã®çµåïŒé£çµïŒæäœããããŸãã
æåår = "Hello" + "peace";
ã³ã³ã¹ãã©ã¯ã¿ãŒãåç §ãããã³å€ïŒã©ããžè¡ãããã©ããžè¡ããã
æ°ãããªããžã§ã¯ããäœæãããšããã®ã©ã€ããµã€ã¯ã«ãå§ãŸããŸãã
åŒã³åºããã³ã³ã¹ãã©ã¯ã¿ã§å§ãŸããŸãã ãïŒ ãã
ã¯ã©ã¹ã®æåã®èšåã§ã¯ãã¯ã©ã¹å ã«ããstatic {...}ãšãã圢åŒã®ãã¹ãŠã®éçãããã¯ãé çªã«åŒã³åºãããŸãã
芪ã®éçãããã¯ã¯ãèšåãããŠãããšããã«åŒã³åºãããŸãã
次ã«ãé çªã«ãäžäœã®èŠªã®åœ¢åŒ{...}ã®ãã¹ãŠã®ãããã¯ãåŒã³åºãããŸãã
次ã«ãåã芪ã®ã³ã³ã¹ãã©ã¯ã¿ãŒãåŒã³åºãããŸãã
ãã®åŸãéå±€ã®åã¯ã©ã¹ã«å¯ŸããŠæåŸã®2ã€ã®æé ãç¹°ãè¿ãããŸãã
æåŸã«ã{}ãããã¯ãšã¯ã©ã¹ã®ã³ã³ã¹ãã©ã¯ã¿ãŒãåŒã³åºãããŸãã
次ã«-ãªããžã§ã¯ããžã®ãªã³ã¯ãããéãããªããžã§ã¯ãã¯åç¶ããŸãã
ãªããžã§ã¯ããžã®åç §ã倱ããããšããã«ãã¬ããŒãžã³ã¬ã¯ã¿ãŒã«ãã£ãŠããã¯ã¢ãããããç Žæ£ãããŸãã ããªã³ã¯ãæ¶å»ãããããã«a = nullãèšè¿°ããå¿ èŠã¯ãããŸãã-Javaã¯ãªããžã§ã¯ãã®äœ¿çšããã€åæ¢ãããããã§ã«ç¥ã£ãŠããŸãã
ç Žæ£ããåã«ãçããªããžã§ã¯ãã®finalizeã¡ãœãããåŒã³åºãããŸãã
ãšããã§ããã®ãããªãšã©ãŒãçºçããããšããããŸãã人ãã¡ãœããã«ãªããžã§ã¯ããæž¡ããããã®æ¹æ³ã§ãã¹ãŠã®ãªã³ã¯ãããªããžã§ã¯ããæ¶å»ãããšèããŠãnullãå²ãåœãŠãŸãã ããããããã¯ãããŸããããã®ãªã³ã¯ã®ã¿ãç Žæ£ãããŸãã ãã ãããªã³ã¯ã1ã€ãããªãå Žå-ãã¡ããããã®å Žåããªããžã§ã¯ãã¯çµäºããŸãã
ãã¶ã€ããŒã®éžã³æ¹ã
åã®æ®µèœã§ãã³ã³ã¹ãã©ã¯ã¿ãŒã«ã€ããŠèšåããŸããã ã¯ã©ã¹ã«ã¯ãããã®å€ãããããããããã«ç¬èªã®ãã©ã¡ãŒã¿ãŒããããŸãã
ããã©ã«ãã§ã¯ããã³ã§äœããæžããŠããªãå Žåããã©ã¡ãŒã¿ãŒã®ãªã空ã®ã³ã³ã¹ãã©ã¯ã¿ãŒãäœæãããŸãã
ããããã芪ã¯ã©ã¹ã®ã³ã³ã¹ãã©ã¯ã¿ãŒãåŒã³åºãå¿ èŠããããŸãã
ãããè¡ãã«ã¯ãã³ã³ã¹ãã©ã¯ã¿ãŒãžã®ã¹ãŒããŒã³ãŒã«ãæåã®è¡ãšããŠè¿œå ããå¿ èŠããããŸãã
å ¬éHelloWorldïŒïŒ{
ã¹ãŒããŒïŒïŒ;
}
ã¹ãŒããŒã³ã³ã¹ãã©ã¯ã¿ãŒã¯èŠªã³ã³ã¹ãã©ã¯ã¿ãŒã§ãã 圌ãæã£ãŠãããã®ãªãã©ãã§ã䜿ããŸãã
å®éãããªãããããæžããªããªããã³ã³ãã€ã©ã¯ããªãã®ããã«ãããããŸãã ãããã芪ã¯ã©ã¹ã«ãã©ã¡ãŒã¿ãŒã®ãªãã³ã³ã¹ãã©ã¯ã¿ãŒããªãå Žåã¯ã©ããªããŸããïŒ
ãã®å Žåãã¹ãŒããŒã³ã³ã¹ãã©ã¯ã¿ãŒãåžžã«æ瀺çã«åŒã³åºãã芪ã¯ã©ã¹ã®ã³ã³ã¹ãã©ã¯ã¿ãŒã®å Žåãšåæ§ã«ãã©ã¡ãŒã¿ãŒãæž¡ãå¿ èŠããããŸãã
ãã£ãã·ã³ã°ã«ã€ããŠäœããããŸããïŒ
ãªããžã§ã¯ããã£ãã·ã³ã°ã«ã€ããŠèšåããŸããã å°ã説æããŸãã äºå®ã¯ãæ°å/è¡ã®èšåã¯åžžã«ç§ãã¡ã«åœãŠã¯ãŸãã«ãŒãã³ã§ãããšããããšã§ãã 1ã§1000ã®æŽæ°ãä¿åããªãããã«ããã£ãã·ã¥ã¡ã«ããºã ãå€ãšããŠäœæãããŸããã ãŠããããŸãã¯ã©ã€ã³ããšã«ãã¡ã¢ãªå ã«æ£ç¢ºã«1ã€ã®ãªããžã§ã¯ããäœæãããèªåæ¡åŒµã§äœ¿çšãããããšãä¿èšŒãããŸãã åè«ã¯ããã©ãããã©ãŒã ã®éçºè ããã£ãã·ã¥ãããçªå·ã®æ°ãäžèšã®å¶éã«å¶éããããšã§ãã
ããšããã§ãç·ããããŒã2-泚æããŠãã ããã
åè¡ã¯äžå€ãªããžã§ã¯ãã§ããããšã«æ³šæããŠãã ããã
ããã«ãããString x = "ads" + "dsada"; ããªãã¯8 * 2ã§ã¯ãªã16 * 2ãã€ããé£ã¹ãŠããŸãã æåã«æåã®2è¡ãäœæããã次ã«3è¡ç®ãäœæãããŸãã
ãããé¿ããããã«ãStringBufferãšStringBuilderãçºæãããŸããã é·ãè¡ãäœæããå¿ èŠãããå Žåã¯ããããã䜿çšããŠãã ããã ïŒæååãã³ããŒããªãããïŒããé«éã§ãã¡ã¢ãªæ¶è²»éãå°ãªããStringBufferãã¹ã¬ããã»ãŒãã§ãïŒãã ããé床ãå°ãé ããªããŸãïŒã
äŸïŒ
StringBuilder hi = new StringBuilder ;
hi.append( ", " );
hi.append( ". " );
hi.append( " ?" );
System. out .println(hi.toString());
* This source code was highlighted with Source Code Highlighter .
åçŽãªãªããžã§ã¯ãã®æäœæ¹æ³ãæ確ã«ã§ããããšãé¡ã£ãŠããŸãã ããã§ãããã«ã€ããŠã¯çµãããŸããããé åã«ã€ããŠã¯æãåºããŸããã
é å
Javaã®é åã¯Cã®é åã«ãã䌌ãŠããŸããããã€ã³ã¿ãŒæŒç®ã§ã¯åäœããŸããã
é åã®äŸïŒ
char [] asd = new char [5]; // 5
char [] ghj = new char []{ 'a' , 'n' };// 2
* This source code was highlighted with Source Code Highlighter .
çŽ æŽãããããŒãã¹ã®ãã¡ãé åã«ã¯é·ããã£ãŒã«ããã€ãŸããåžžã«ç¥ã£ãŠããé åã®é·ãããããåºå®ãããŠããŸãã
åºæ¬çãªJavaã³ã³ã¹ãã©ã¯ã
ifïŒä»»æã®ããŒã«åŒãŸãã¯å€æ°ïŒ{} else {}
forïŒã«ãŒãã®éå§åã®ã¢ã¯ã·ã§ã³;ã«ãŒãã®ç¶ç¶ã®æ¡ä»¶;åã¹ãããã®åŸã®ã¢ã¯ã·ã§ã³ïŒ{}
whileïŒã«ãŒãç¶ç¶æ¡ä»¶ïŒ{}
do {} whileïŒã«ãŒãç¶ç¶æ¡ä»¶ïŒ
ã¹ã€ããïŒæ°å€ãŸãã¯åæå€æ°ïŒ{case valueïŒ... break; ã±ãŒã¹å€ïŒ... break; ããã©ã«ãïŒ}
forïŒtype variableNameïŒarrayïŒ{}
åŸè ã«ã€ããŠããã«è©³ãã説æããããšæããŸãã Javaã§ã¯ãfor-eachã«ãŒãã¯æ¬¡ã®ããã«ãªããŸãã ããšãã°ã次ã®ããã«ãé åã®åèŠçŽ ãå埩åŠçããŸãã
char [] chars = new char []{ 'a' , 'n' };
for ( char a : chars){
System. out .println(a);
}
* This source code was highlighted with Source Code Highlighter .
ããã«ãfor-eachã¯ã³ã¬ã¯ã·ã§ã³ã ãã§ãªãããã¹ãŠã®ã³ã¬ã¯ã·ã§ã³ã«é©çšã§ããŸãã ããã«ã€ããŠã¯åŸã§èª¬æããŸãã
PSã©ãããã°Javaã³ãŒããé©åã«åŒ·èª¿ã§ããŸããïŒ æ®å¿µãªãããJavaã®ãœãŒã¹ã³ãŒããã€ã©ã€ã¿ãŒã¯æå³ãããŠããŸããã