1. Java Language Specification (Java Language ¼³¸í¼)
º» ¹®¼´Â Java Language¸¦ ¼Ò°³ÇÑ Sun MicrosystemÀÇ
"Java Language Specification"À» Çѱ۷Π¹ø¿ªÇÑ °ÍÀÔ´Ï´Ù.
º» ¹®¼´Â ¾î´ÀÁ¤µµ ANSI C¿¡ Àͼ÷ÇÑ »ç¿ëÀÚ¸¦ À§ÇØ ÀÛ¼ºµÇ¾ú´Ù.
1.1. ÇÁ·Î±×·¥ ±¸Á¶(Program sturucture)
Java ÇÁ·Î±×·¥Àº Çϳª ¶Ç´Â ¿©·¯°³ÀÇ compilation unit(ÄÄÆÄÀÏ ´ÜÀ§)À¸·Î µÇ¾îÀÖ´Ù.
ÀÌ·¯ÇÑ °¢°¢ÀÇ compilation unitÀ¸·Î ´ÙÀ½°ú °°Àº °ÍÀ¸·Î ±¸¼ºµÇ¾îÀÖ´Ù.
- a package statment.
- import statements.
- class declarations.
- interface declarations.
ºñ·Ï JavaÀÇ compilation unitÀÌ ¿©·¯°³ÀÇ Å¬·¡½º¿Í ÀÎÅÍÆäÀ̽º¸¦ °¡Áö°í
ÀÖ´õ¶ó°í,compilation unit¿¡¼ ±â²¯ÇØ¾ß ÇϳªÀÇ Å¬·¡½º³ª, ÀÎÅÍÆäÀ̽º¸¸ÀÌ
publicÀÌ´Ù.
Java ¿ø½ÃÈÀÏÀÌ compileµÉ¶§, ±× °á°ú´Â java bytecodeÀÌ´Ù.
Java bytecode´Â Java Runtime system¿¡ ÀÇÇؼ È¿À²ÀûÀ¸·Î Çؼ®µÉ¼ö ÀÖ´Â
±â°èµ¶¸³ÀûÀÎ ¸í·É¾î·Î ±¸¼ºµÇ¾îÀÖ´Ù.
Java runtime systemÀº °¡»ó±â°è(virtual machine)ó·³ ½ÇÇàµÈ´Ù.
±×¹ÛÀÇ ³»¿ëÀº "The Java Virtual Machine Specification"À» ÂüÁ¶ÇÑ´Ù.
1.2 ¹®ÀåÇ¥Çö. (Lexical Issues)
CompileÀ» ÇÏ´Â µ¿¾È¿¡, Java soruce code¿¡ ÀÖ´Â character´Â ¿©·¯°³ÀÇ
¿¬¼ÓÀûÀÎ ÅäÅ«(token)À¸·Î Ç¥ÇöµÈ´Ù. Java Compiler´Â 5°¡ÁöÀÇ token À¸·Î
ÀνÄÇÑ´Ù : identifiers, keywords, literals, operators, micellaneoous
separators.
ÁÖ¼®¹®°ú ºó°ø¹éÀ̳ª ÅÇ, °³Çà°ú °°Àº white space´Â tokenÀ» ±¸º°Çϱâ À§Çؼ¸¸
¾²ÀÌ°í ¹«½ÃµÈ´Ù.
Java ÇÁ·Î±×·¥Àº Unicode character setÀ» ÀÌ¿ëÇϰųª, ÄÄÆÄÀϽà Unicode·Î
º¯È¯ °¡´ÉÇÑ character setÀ¸·Î ÀÛ¼ºÇÒ¼ö ÀÖ´Ù.
1.2.1 ÁÖ¼®¹®. (Comments)
Java¾ð¾î´Â ÁÖ¼®À» Ç¥ÇöÇÒ¶§ 3°¡Áö ¹æ¹ýÀ» »ç¿ëÇÑ´Ù.
// text
'//'ÀÌÈÄÀÇ ¸ðµç ¹®ÀÚ´Â ´ÙÀ½ÁÙ±îÁö ¹«½ÃµÈ´Ù.
/* text */
'/*'¿Í '*/'»çÀÌÀÇ ¹®ÀÚ´Â ¹«½ÃµÈ´Ù.
/** text */
1.2.2 ¼±¾ðÀÚ. (Identifiers)
¼±¾ðÀÚ(Identifiers)´Â ¹®ÀÚ(letter), "_", "$"·Î¸¸ ½ÃÀ۵Ǿî¾ß ÇÑ´Ù ;
±× ´ÙÀ½ÀÇ ¹®Àڷδ ¼ýÀÚ(0-9)°¡ µé¾î°¥ ¼ö ÀÖ´Ù. Java´Â
Unicode character setÀ» »ç¿ëÇÑ´Ù. ÀûÇÕÇÑ ¹®ÀÚ(letter)·Î´Â ´ÙÀ½°ú °°´Ù.
- "A" ~ "Z"±îÁöÀÇ ¹®ÀÚ.
- "a" ~ "z"±îÁöÀÇ ¹®ÀÚ.
- 16Áø¼ö·Î 00c0ÀÌ»óÀÇ ¹®ÀÚ¿Í, ¸ðµç Unicode character.
´õ ÀÌ»óÀÇ Á¤º¸´Â "The Unicode Standard, Worldwide chacter Encoding,
Version 1.0, Volumes 1&2"¸¦ ÂüÁ¶ÇÑ´Ù.
1.2.3 ¿¹¾à¾î. (Keywords)
´ÙÀ½Àº ¿¹¾à¾î·Î½á ´Ù¸¥ ¿ëµµ·Î ¾²ÀÏ ¼ö ¾ø´Â°ÍµéÀÌ´Ù.
(aÇ¥ µÇ¾î Àִ°ÍÀº ¿äÁò Àß¾²Áö ¾Ê´Â °ÍÀÌ´Ù.)
1.2.4 µ¥ÀÌŸ Ç¥Çö(Literal)
LiteralÀ̶õ integer, floating point, boolean, character, string value¸¦
Ç¥ÇöÇϴ°ÍÀ» ¸»ÇÑ´Ù.
1.2.4.1 Á¤¼ö Ç¥Çö(integer LIterals)
Á¤¼öÇüÀº 10Áø¼ö, 16Áø¼ö, 8Áø¼ö·Î Ç¥ÇöÇÒ ¼ö ÀÖ´Ù.
10Áø¼ö´Â (0~9)±îÁöÀÇ °ªÀ» °¡Áö°í Á¢µÎ¾î´Â »ý·«ÇÑ´Ù.
16Áø¼ö´Â (0~9, A~F)±îÁöÀÇ °ªÀ» °¡Áö°í Á¢µÎ¾î·Î '0X ¶Ç´Â 0x'¸¦ ºÙÈù´Ù.
8Áø¼ö´Â (0~7)±îÁöÀÇ °ªÀ» °¡Áö°í Á¢µÎ¾î·Î '0'°¡ ºÙ´Â´Ù.
Á¤¼öÇüÀÇ 32bitÀÌ»óÀÇ °ªÀº longÀ» »ç¿ëÇϴµ¥, Á¢¹Ì¾î·Î 'L ¶Ç´Â l'À» »ç¿ëÇÑ´Ù.
´ÙÀ½Àº ¸ðµÎ ¿Ã¹Ù¸¥ Á¤¼öÇüÀÇ Ç¥ÇöÀÌ´Ù.
2, 2L 0777 0xDeadBeef
1.2.4.2 ½Ç¼ö Ç¥Çö(Floating Point Literals)
½Ç¼öÇü Ç¥ÇöÀº "."À» ÀÌ¿ëÇÏ¿© ¼Ò¼ýÁ¡ÀÌ»ó, ÀÌÇϸ¦ Ç¥ÇöÇÑ´Ù. 'E ¶Ç´Â e'¸¦ »ç¿ëÇÏ¿© Áö¼ö¸¦ Ç¥ÇöÇÑ´Ù.
´ÙÀ½Àº ¸ðµÎ ¿Ã¹Ù¸¥ ½Ç¼öÇüÀÇ Ç¥ÇöÀÌ´Ù.
3.1415 3.1E12 .1e12 2E12
½Ç¼öÇü ¿ª½Ã float, double, µÎ°¡Áö data typeÀÌ ÀÖ´Ù.
µÎ°¡Áö¸¦ Ç¥ÇöÇÏ´Â ¹æ¹ýÀº ´ÙÀ½°ú °°´Ù.
2.0d or 2.0D double
2.0f or 2.0F or 2.0 float
1.2.4.3 ³í¸®Çü Ç¥Çö(Boolean Literals)
true¿Í falseÀ¸·Î 2°¡Áö °ªÀ» Ç¥ÇöÇÑ´Ù.
1.2.4.4 ¹®ÀÚ Ç¥Çö(Character Literals)
ÀÛÀº µû¿ÈÇ¥ "'"·Î µÑ·¯½×ÀÎ ÇϳªÀÇ ¹®ÀÚ¸¦ Ç¥ÇöÇÑ´Ù.
1.2.4.5 ¹®ÀÚ¿ Ç¥Çö(String Literals)
Å«µû¿ÈÇ¥ ·Î µÑ·¯½×ÀÎ ¹®ÀÚ¿À» Ç¥ÇöÇÏ´Â ¹æ¹ýÀº ´ÙÀ½°ú °°´Ù.
"" \\ the empty string
"\""
"This is a string"
"This is a \
two-line string"
1.2.5 ¿¬»êÀÚ¿Í ´Ù¾çÇÑ ±¸ºÐÀÚ. (Operators and Miscellaneous Separators)
´ÙÀ½Àº source code¿¡¼ »ç¿ëµÇ´Â ¿¬»êÀÚ¿Í ±¸ºÐÀÚÀÌ´Ù.
+ - ! % ^ & * | ~ / > <
( ) { } [ ] ; ? : , . =
±×¹Û¿¡ À§Àǰ͵éÀ» Á¶ÇÕÇÑ ÇüÅÂÀÌ´Ù.
++ -- == <= >= != << >>
>>> += -= *= /= &=|=
^= %= <<= >>= >>>= || &&
1.3 ÀÚ·áÇü. (Types)
¸ðµç º¯¼ö¿Í Ç¥Çö(expression)Àº ÀÚ·áÇüÀ» °¡Áø´Ù. ÀÚ·áÇü¿¡ ÀÇÇؼ º¯¼ö°¡ °¡Áú
¼ö ÀÖ´Â °ªÀÇ ¹üÀ§¸¦ °áÁ¤ÇÏ°í, ±× °ªÀ» °è»êÇÒ ¿¬»êÀÚ¸¦ °áÁ¤ÇÑ´Ù.
Java Language¿¡¼´Â ±âº»ÀûÀÎ Built-in typeÀ» Á¦°øÇÑ´Ù.
Java language¿¡¼´Â µÎ°¡Áö simple, composite ÇüÅÂÀÇ ÀÚ·áÇüÀÌ ÀÖ´Ù.
Simple typeÀº atomicÇϱ⠶§¹®¿¡ Àý´ë ¼Õ»óµÇÁö ¾Ê´Â typeÀ¸·Î integer,
floating point, boolean, charter µîÀÌ ÀÖ°í, Composite typeÀº simple typeÀ¸·Î
±¸¼ºµÈ ÀÚ·áÇüÀ¸·Î, array, classes, interface 3°¡Áö°¡ ÀÖ´Ù.
1.3.1 ¼ýÀÚÇü. (Numeric Types)
1.3.1.1 Á¤¼öÇü. (Integer Types)
Á¤¼öÇüÀº C/C++°ú ºñ½ÁÇÏÁö¸¸, C¿¡¼´Â ±â°è¿¡ µû¶ó¼ ´Ù¸£Áö¸¸ Java´Â
±â°è µ¶¸³Àû(machine independent)À¸·Î º¯¼öÀÇ ÀúÀå¿ë·®°ú´Â °ü°è¾øÀÌ
¼öÇÐÀûÀΠƯ¡°ú, ÇÑ°è°ª¿¡ µû¶ó Á¤ÇØÁ®ÀÖ´Ù.
´ÙÀ½Àº 4°¡Áö Á¤¼öÇü typeÀÌ´Ù.
1.3.1.2 ½Ç¼öÇü. (Integer Types)
float´Â 32bitÀÌ°í, doubleÀº 64bitÀÌ´Ù. µÎ°³ÀÇ float¸¦ ¿¬»êÇÏ¸é °á°ú´Â
float ÇüÀÌÁö¸¸, ¾î´À Çϳª¶óµµ doubleÀÌ¸é °á´Â doubleÇüÀÌ´Ù.
1.3.1.3 ¹®ÀÚÇü. (Character Types)
¹®ÀÚÇüÀº 16bit·Î Á¤ÀǵǾî ÀÖ´Ù.
1.3.2 ³í¸®Çü. (Boolean Types)
³í¸®ÇüÀº true, flase°ª¸¸À» °¡Áö´Â typeÀÌ´Ù.
³í¸®ÇüÀº Àý´ë ¼ýÀÚ°ªÀÌ ¾Æ´Ï°í, ¼ýÀÚ°ªÀ¸·Î ¹Ù²Ü ¼ö ¾ø´Ù.
1.3.3 ¹è¿. (Array)
¹è¿Àº ¾ð¾î¿¡¼ first class objectÀÌ´Ù.
¹è¿Àº new ¿¬»êÀÚ¸¦ »ç¿ëÇÏ¿© »ý¼ºÇÑ´Ù.
char s[] = new char[30];
¹è¿ÀÇ Ã¹¹ø° index´Â 0ÀÌ´Ù.
¹è¿ÀÇ Å©±â´Â new¸¦ ÀÌ¿ëÇÏ¿© ¸í½ÃÇØ¾ß ÇÑ´Ù.
int i[] = new int[3];
Multi-dimensional ¹è¿Àº Á¦°øµÇÁö ¾Ê´Â´Ù. ´ë½Å, programmer´Â ¹è¿ÀÇ ¹è¿À»
Á¤ÀÇÇÒ ¼ö ÀÖ´Ù.
int i[][] = new int[3][4];
¹è¿ÀÇ ¹è¿¿¡¼ ¹è¿ÀÇ Å©±â´Â ÃÖ¼ÒÇÑ Çϳª´Â ¸í½ÃµÇ¾î¾ß ÇÑ´Ù. ¸í½ÃµÇÁö ¾ÊÀº
ºÎºÐÀº ´ÙÀ½¿¡ ¸í½ÃÀûÀ¸·Î Å©±â¸¦ Á¤ÇÒ ¼ö ÀÖ´Ù.
int i[][] = new int[3][];
Java¿¡¼´Â C¿¡¼ÀÇ ´ë°ýÈ£¸¦ ÀÌ¿ëÇÑ ¹è¿¼±¾ðÀÇ È®ÀåµÈ Ç¥ÇöÀÌ´Ù.
´ÙÀ½ µÎ ¹æ¹ýÀº ¸ðµÎ ¿Ã¹Ù¸¥ ¹è¿ ¼±¾ð ¹æ¹ýÀÌ´Ù.
int iarray[];
int[] iarray;
´ÙÀ½ÀÇ Ç¥Çö¹æ¹ýÀÌ ¿Ã¹Ù¸¥Áö È®ÀÎÇغ¸ÀÚ.
int a[] = new int[10];
a[5] = 1;
a[1] = a[0] + a[2];
a[-1] = 4; // Throws an ArrayIndexOutOfBoundsException
// at runtime
a[10] = 2; // Throws an ArrayIndexOutOfBoundsException
// at runtime
¹è¿ÀÇ index´Â Á¤¼öÇüÀÌ¿©¾ß ÇÑ´Ù.
int n;
...
float arr[] = new float[n + 1];
¹è¿ÀÇ Å©±â´Â .length¸¦ »ç¿ëÇÏ¿© ¾Ë¾Æ ³¾¼ö ÀÖ´Ù.
int a[][] = new int[10][3];
println(a.length); // prints 10
println(a[0].length); // prints 3
1.3.3.1 ¹è¿ÀÇ ³»ºÎ±¸Á¶. (Array Detail)
Array´Â Object classÀÇ subclassÀÇ instanceÀÌ´Ù. ¸ðµç typeÀÇ ¹è¿Àº ArrayÀÇ
subclassÀÌ´Ù. Áï, ´ÙÀ½ÀÇ ThreadÀÇ ¹è¿Àº
new Thread[n]
Thread[]ÀÇ instance¸¦ »ý¼ºÇÑ´Ù. ¸¶Âù°¡Áö·Î ´ÙÀ½ ±×¸²¿¡¼ class A°¡ class BÀÇ
superclass À̸é A[]´Â B[]ÀÇ superclassÀÌ´Ù.
±×·¯¹Ç·Î, ¹è¿¿¡´Â Object¸¦ ´ëÀÔÇÒ ¼ö ÀÖ´Ù.
Object o;
int a[] = new int[10];
o = a;
¶ÇÇÑ Object¸¦ ¹è¿·Î Çüº¯È¯(cat)ÇÒ ¼ö ÀÖ´Ù.
a = (int[])o;
Array class´Â Çüº¯È¯ ¿¬»êÀÚ¸¦ ¸í½ÃÇØ¾ß ÇÑ´Ù.
1.4 Ŭ·¡½º. (Classes)
Class´Â °íÀüÀûÀ¸·Î °´Ã¼ÁöÇâ ÇÁ·Î±×·¡¹Ö modelÀ» Ç¥ÇöÇϴµ¥ »ç¿ëµÇ¾ú´Ù.
µ¥ÀÌŸ Ãß»óÈ(Data abstraction)È data¸¦ ¿«´Â ¿ªÇÒ À» ÇÑ´Ù. Java¿¡¼
class¸¦ »ý¼ºÇѴٴ°ÍÀº »õ·Î¿î typeÀ» »ý¼ºÇÏ´Â °ÍÀ» ÀǹÌÇÑ´Ù.
»õ·Î¿î class¸¦ Á¤ÀÇÇϱâ À§Çؼ, programmer´Â ±âÁ¸ÀÇ classÀ» ±â¹ÝÀ¸·Î Çؾß
ÇÑ´Ù. À̶§ ÀÌ·± »õ·Î¿î class´Â ±âÁ¸ÀÇ class¿¡¼ ÆÄ»ý(derived)µÇ¾ú´Ù°í ¸»ÇÑ´Ù.
ÆÄ»ý class´Â ¶ÇÇÑ subclass¶ó°íµµ ºÎ¸£°í ±âÁ¸ÀÇ class´Â superclass¶ó°í ºÎ¸¥´Ù.
Class´Â Àü´ÞµÉ ¼ö Àִµ¥, C°¡ BÀÇ subclassÀÌ°í, B°¡ AÀÇ subclassÀ̸é,
C´Â AÀÇ subclassÀÌ´Ù.
'extend'¶ó´Â keyword¸¦ ÀÌ¿ëÇÏ¿© classÀÇ Á÷¼Ó superclass¸¦ Ç¥ÇöÇÏ°í,
'implements'¶ó´Â keyword¸¦ ÀÌ¿ëÇÏ¿© class°¡ ¼öÇàÇÏ´Â interface¸¦ Ç¥ÇöÇÑ´Ù.
[Doc comment] [Modifiers] class Classname
extends Superclassname]
implements Interface{, Interface}] {
ClassBody
}
¿¹¸¦ µé¸é ´ÙÀ½°ú °°´Ù.
/** 2 dimensional point */
public class Point {
float x, y;
...
}
/** Printable point */
class PrintablePoint extends Points implements Printable {
...
public void print() {
...
}
}
¸ðµç class´Â ÇϳªÀÇ root class·ÎºÎÅÍ ÆÄ»ýµÈ´Ù. Object¸¦ Á¦¿ÜÇÑ ¸ðµç class´Â
ÇϳªÀÇ Á÷¼Ó superclass¸¦ °¡Áø´Ù. ¸¸¾à class°¡ Á÷¼Ó superclass¸¦ Á¤ÀÇÇÏÁö
¾ÊÀ¸¸é, superclass´Â Object·Î °£ÁֵȴÙ.
¿¹¸¦ µé¾î¼ ´ÙÀ½ code´Â °°ÀºÀǹÌÀÌ´Ù.
class Point {
float x, y;
}
class Point extends Object {
float x, y;
}
Java¿¡¼´Â ¿ÀÁ÷ ÇϳªÀÇ class·ÎºÎÅÍÀÇ »ó¼Ó(simgle inheritance)¸¸À» Çã¿ëÇÑ´Ù.
´Ù¸¥ ¾ð¾î¿¡¼ Á¦°øÇÏ´Â ´ÙÁß »ó¼ÓÀº interfaces¸¦ ÀÌ¿ëÇÏ¿© ±Øº¹ÇÒ ¼öµµ ÀÖ´Ù.
1.4.1 ClassÀÇ Çüº¯È¯. (Casting Between Class Types)
Java´Â Çüº¯È¯À» Á¦°øÇÑ´Ù. ¶ÇÇÑ, class´Â ÇϳªÀÇ »õ·Î¿î typeÀ̱⠶§¹®¿¡
class type°£ÀÇ Çüº¯È¯À» Á¦°øÇÑ´Ù. ¸¸¾à B°¡ AÀÇ subclass¶ó¸é, BÀÇ instance´Â
AÀÇ instance·Î »ç¿ëµÉ ¼ö ÀÖ´Ù. Çüº¯È¯ ¿¬»êÀÚ°¡ ÇÊ¿ä¾øÁö¸¸ ¸í½ÃÇϴ°ÍÀÌ ÁÁ´Ù.
ÀÌ°ÍÀ» 'widening' À̶ó°í ºÎ¸¥´Ù. ¸¸¾à AÀÇ instance°¡ BÀÇ instanceó·³ »ç¿ëµÉ
ÇÊ¿ä°¡ ÀÖ´Ù¸é Çüº¯È¯ÀÌ °¡´ÉÇѵ¥ ÀÌ°ÍÀ» 'narrowing'À̶ó°í ÇÑ´Ù.
±× ÀϹÝÀûÀΠǥÇöÀº ´ÙÀ½°ú °°´Ù.
(classname)ref
ref´Â classnameÀ¸·Î Çüº¯È¯ µÈ´Ù.
Çüº¯È¯Àº ´ÜÁö ÂüÁ¶Çϱâ À§Çؼ¸¸ ¾²ÀÌ°í, objectÀÚü°¡ º¯Çϴ°ÍÀº ¾Æ´Ï´Ù.
´ÙÀ½Àº Çüº¯È¯½Ã, ÂüÁ¶µÇ´Â type¿¡ ¿µÇâÀ» ¹Þ´Â´Ù´Â ¿¹Á¦ÀÌ´Ù.
class ClassA {
String name = "ClassA";
}
class ClassB extends ClassA { // ClassB is a subclass of ClassA
String name= "ClassB";
}
class AccessTest {
void test() {
ClassB b = new ClassB();
println(b.name); // print: ClassB
ClassA a;
a = (ClassA)b;
println(a.name); // print: ClassA
}
}
1.4.2 ¸Þ¼Òµå. (Mothods)
Method´Â object³ª classÀÌ ¼öÇàÇÏ´Â ÀÏ(¿¬»ê)À» Ç¥ÇöÇÑ´Ù. Method´Â class³ª
interface¿¡¼ ¼±¾ðÇÒ ¼ö ÀÖÀ¸³ª, ¿ÀÁ÷ class¾È¿¡¼¸¸ ÀÏÀ» ¼öÇà ÇÒ ¼ö ÀÖ´Ù.
(¸ðµç »ç¿ëÀÚ Á¤ÀÇ ¿¬»ê(user-defined operation)Àº method·Î ±¸ÇöµÈ´Ù.)
class¿¡¼ method¸¦ Á¤ÀÇÇÏ´Â Çü½ÄÀº ´ÙÀ½°ú °°´Ù.( native, abstract method´Â
method body¸¦ °¡Áö°í ÀÖÁö ¾Ê´Ù.)
[Doc comment] [Modifiers] returnType methodName ( parameterList ) {
[methodBody]
}
Methods:
- Construtor¸¦ Á¦¿ÜÇÑ ¸ðµç method´Â return typeÀ» °¡Áö°í ÀÖ´Ù. ¸¸¾à
return °ªÀÌ ¾ø´õ¶óµµ return typeÀº 'void'À̾î¾ß ÇÑ´Ù.
- parameter list´Â parameter name°ú typeÀ» Æ÷ÇÔÇÏ¿©, comma·Î ±¸ºÐÇÑ´Ù.
parameter°¡ ¾øÀ¸¸é ºñ¿öµÑ ¼ö ÀÖ´Ù.
º¯¼ö´Â method¾È¿¡ Á¤ÀÇÇÒ ¼ö ÀÖ´Ù.(Áö¿ªº¯¼ö : local variable) °°Àº method
¾È¿¡¼ Áö¿ªº¯¼ö¿Í parameter´Â °°Àº À̸§ÀÌ À־ ¾ÈµÈ´Ù. ´ÙÀ½ÀÇ ¿¹´Â
À߸øµÈ ¿¹·Î½á compile-time error°¡ ¹ß»ýµÈ´Ù.
class Rectangle {
void vertex(int i, int j) {
for (int i = 0; i <= 100; i++) { // ERROR
...
}
}
}
º¯¼ö "i"´Â Áö¿ªº¯¼ö¿Í, parameter¿Í À̸§ÀÌ °°À¸¹Ç·Î errorÀÌ´Ù.
Java´Â overriding°ú overloading±â¹ýÀÇ polymorphic method¸¦ Á¦°øÇÑ´Ù.
overridingÀº superclass·Î ºÎÅÍ »ó¼Ó¹ÞÀº °°Àº method¸¦ ´ë½ÅÇÏ¿© ´Ù¸¥ÀÏÀ» ÇÏ´Â
°°Àº À̸§ÀÇ »õ·Î¿î methodÁ¤ÀÇ Çϴ°ÍÀ» ÀǹÌÇÏ°í, overloadingÀº method°¡ ¿ÀÁ÷
À̸§¸¸ °°°í ¼·Î ´Ù¸¥ method¸¦ »ç¿ëÇÏ´Â °ÍÀ» ÀǹÌÇϸç, parameter list·Î
°°Àº À̸§ÀÇ method¸¦ ¼·Î ±¸º°ÇÒ ¼ö ÀÖ´Ù.
´Ü, return typeÀ¸·Î´Â ±¸º°ÇÏÁö ¾Ê´Â´Ù.
1.4.2.1 Instance Variables.
methodÀÇ ³»ºÎ¿¡ Á¤ÀǵÇÁö¾Ê°í, staticÀÌ ¾Æ´Ñ ¸ðµç º¯¼ö´Â
instance vairableÀÌ´Ù. Instance variableÀº modifier¸¦ °¡Áú ¼ö ÀÖ´Ù.
Instance variableÀº ¾î¶² typeÀ̾ µÇ°í, ÃʱâÈÇÒ ¼ö ÀÖ´Ù. ¸¸¾à ÃʱâÈ
µÇÁö ¾ÊÀ¸¸é 0À¸·Î ÃʱâȵȴÙ.; booleanÀº false, object´Â null. ´ÙÀ½Àº
instance variableÀÇ ¼±¾ð°ú ÃʱâÈ¿¡ ´ëÇÑ ¿¹ÀÌ´Ù.
class A {
int j = 23;
...
}
ÀÚ±â ÀÚ½ÅÀ» °¡¸®Å°´Â this¶ó´Â °ÍÀÌ ÀÖ´Ù. ´Ù¸¥ object¿¡°Ô ÀÚ±â ÀÚ½ÅÀ»
argument·Î ³Ñ°ÜÁÙ¶§ »ç¿ëµÈ´Ù.
class MyClass {
void aMethod(OtherClass obj) {
...
obj.Method(this);
...
}
}
¶ÇÇÑ, Method ³»¿¡¼ ÀÚ½ÅÀÇ instance variableÀ̳ª method¸¦ ÂüÁ¶ÇÒ ¶§
¾Ï½ÃÀûÀ¸·Î "this."ÀÌ »ý·«µÈ °ÍÀÌ´Ù. ´ÙÀ½Àº ±× ¿¹ÀÌ´Ù.
class Foo {
int a, b, c;
...
void myPrint(){
print(a + "\n"); // a == "this.a"
}
...
}
super´Â this¿Í ºñ½ÁÇѵ¥, this´Â ÇöÀç ÀÚ±â ÀÚ½ÅÀÇ object¸¦ ÂüÁ¶ÇÒ¶§ »ç¿ëµÇ°í,
super´Â superclass¸¦ ÂüÁ¶ÇÒ¶§ »ç¿ëµÈ´Ù.
1.4.2.3 Setting Local Variables.
Method´Â local variableÀ» ÂüÁ¶ÇÏ¿© »ç¿ëÇϱâ Àü¿¡ ÃʱâÈ µÇ¾ú´ÂÁö ¾ö°ÝÇÏ°Ô
°Ë»çÇÑ´Ù. ÃʱâÈÇÏÁö ¾Ê°í local variableÀ» »ç¿ëÇÏ°Ô µÇ¸é compile-time error¸¦
¹ß»ý½ÃŲ´Ù.
1.4.3 Overriding Methods
Method¸¦ overrideÇϱâ À§Çؼ, ¿ø·¡ÀÇ method¸¦ °¡Áö°í ÀÖ´Â classÀÇ subclass´Â
À̸§ÀÌ °°°í, parameter, return typeµµ °°Àº method¸¦ Á¤ÀÇÇØ¾ß ÇÑ´Ù.
SubclassÀÇ interface·ÎºÎÅÍ method°¡ È£Ã⠵ɶ§ ¿ø·¡ method°¡ È£ÃâµÇÁö ¾Ê°í
»õ·Î¿î method°¡ È£Ã⠵ȴÙ. ¶ÇÇÑ, overridden method´Â super¸¦ »ç¿ëÇÏ¿© È£Ãâ
ÇÒ ¼öµµ ÀÖ´Ù.
setThermostat(...) // refers to the overriding method
super.setThermostat(...) // refers to the overridden method
1.4.4 Overload Resolution.
Overloaded method´Â argumentÀÇ °¹¼ö³ª, argumentÀÇ typeÀÌ ´Ù¸¥ °°Àº À̸§ÀÇ
method¸¦ ¸»ÇÑ´Ù. Overload resolutionÀº ¾î¶² overloaded method¸¦ È£Ãâ ÇÒ°ÍÀÎÁö
°áÁ¤ÇÏ´Â ÀÏÀ» ÇÑ´Ù. method´Â °°Àº class¾È¿¡¼µµ overloadÇÒ ¼ö ÀÖ´Ù.
class³»¿¡¼ methodÀÇ ¼±¾ð ¼ø¼¿Í´Â ÀüÇô ¹«°üÇÏ´Ù.
AargumentÀÇ °¹¼ö³ª, typeÀ» ´Ù¸£°Ô ÇÔÀ¸·Î½á method¸¦ overloadÇÑ´Ù. Compiler´Â
´ëÀÀµÇ´Â ÃÖ¼ÒÇÑÀÇ ºñ¿ëÀ¸·Î method¸¦ ¸¦ ã´Â´Ù. ¿ÀÁ÷ °°ÀºÀ̸§ÀÇ °°Àº¼öÀÇ
argument¿Í °°Àº typeÀÇ argument¸¦ °¡Áö´Â method¸¦ ã´Â´Ù.
class A {
int method(Object o, Thread t);
int method(Thread t, Object o);
void g(Object o, Thread t) {
method(o, t); // calls the first method.
method(t, o); // calls the second method.
method(t, t); // ambiguous - compile-time error
}
}
ÁÖÀÇÇÒÁ¡Àº parameterÀÇ °¹¼ö³ª, type¿¡ ¿µÇâÀ»¹Þ°í parameterÀÇ À̸§°ú´Â
¹«°üÇÏ´Ù.
1.4.5 »ý¼ºÀÚ. (Constructors)
»ý¼ºÀÚ´Â Ãʱ⸦ À§ÇØ Æ¯º°È÷ Á¦°øµÇ´Â methodÀÌ´Ù. return °ªÀÌ ¾ø°í, classÀÇ
À̸§°ú °°À¸¹Ç·Î ´Ù¸¥ method¿Í ±¸º°µÈ´Ù. »ý¼ºÀÚ´Â object°¡ »ý¼ºµÉ¶§ ÀÚµ¿À¸·Î
È£ÃâµÈ´Ù. »ý¼ºÀڴ ǥ¸éÀûÀ¸·Î È£Ãâ ÇÒ¼ö´Â ¾ø´Ù. ¸¸¾à package ¿ÜºÎ¿¡¼ »ý¼ºÀÚ¸¦
È£ÃâÇÒ ÇÊ¿ä°¡ ÀÖ´Ù¸é, »ý¼ºÀÚ¸¦ publicÀ¸·Î ÇؾßÇÑ´Ù.
class Foo {
int x;
float y;
Foo() {
x = 0;
y = 0.0;
}
Foo(int a) {
x = a;
y = 0.0;
}
Foo(float a) {
x = 0;
y = a;
}
Foo(int a, float b) {
x = a;
y = b;
}
static void myFoo() {
Foo obj1 = new Foo(); //calls Foo();
Foo obj2 = new Foo(4); //calls Foo(int a);
Foo obj3 = new Foo(4.0); //calls Foo(float a);
Foo obj4 = new Foo(4, 4.0); //calls Foo(int a, float b);
}
}
ÇöÀç classÀÇ constructor¸¦ È£ÃâÇϰųª, SuperclassÀÇ constructor¸¦
È£ÃâÇÔÀ¸·Î½á superclassÀÇ instance variableÀ» ÃʱâÈ ÇÑ´Ù. ¸¸¾à code¿¡¼
¸í½Ã¸¦ ÇÏÁö ¾ÊÀ¸¸é, parameter¸¦ °®Áö¾Ê´Â superclassÀÇ constructor¸¦ ÀÚµ¿À¸·Î
È£ÃâÇÏ°í, ¸í½ÃµÇ¾îÀÖÀ¸¸é ¿ì¼±ÀûÀ¸·Î ¸í½ÃµÇ¾îÀÖ´Â classÀÇ constructor body¿¡ ÀÖ´Â
superclassÀÇ constructor¸¦ È£ÃâÇÏ°Ô µÈ´Ù. Constructor°¡ È£ÃâµÇ±â ÀÌÀü¿¡
instance variableÀº ÂüÁ¶µÉ ¼ö ¾ø´Ù. ´ÙÀ½Àº superclassÀÇ constructor¸¦
È£ÃâÇÏ´Â ¿¹ÀÌ´Ù.
class MyClass extends OtherClass {
MyClass(someParameters) {
/* Call immediate superclass constructor */
super(otherParameters);
...
}
...
}
´ÙÀ½Àº ÀÚ½ÅÀÇ constructor¸¦ È£Ãâ ÇÏ´Â ¿¹ÀÌ´Ù.
class MyClass extends OtherClass {
MyClass(someParameters) {
...
}
MyClass(otherParameters) {
/* Call the constructor in this class that has the
specified parameter list. */
this(someParameters);
...
}
...
}
´ÙÀ½Àº counsturctor Foo¿Í FooSub methodÀÌ´Ù.
class Foo extends Bar {
int a;
Foo(int a) {
// implicit call to Bar()
this.a = a;
}
Foo() {
this(42); // calls Foo(42) instead of Bar()
}
}
class FooSub extends Foo {
int b;
FooSub(int b) {
super(13); // calls Foo(13); without this line,
// would have called Foo()
this.b = b;
}
}
¸¸¾à constructor°¡ ¾øÀ¸¸é, compiler´Â ´ÙÀ½°ú °°Àº Çü½ÄÀÇ constructor¸¦
ÀÚµ¿À¸·Î ó¸®ÇÑ´Ù.
class MyClass extends OtherClass {
MyClass() { // automatically generated
super();
}
}
1.4.6 new¿¬»êÀÚ¸¦ ÀÌ¿ëÇÑ °´Ã¼ »ý¼º. (Object Creation-the new Operator)
Class´Â objectÀÇ »óÅ¿Í, Çൿ¹æ½ÄÀ» Á¤ÀÇÇÑ templateÀÌ´Ù. Object´Â classÀÇ
instanceÀÌ´Ù. ¸ðµç classÀÇ instance´Â garbage collected heap¿¡ ÇÒ´çµÈ´Ù.
ObjectÀÇ ¼±¾ð¸¸À¸·Î´Â ¸Þ¸ð¸®¸¦ ÇÒ´çÇÏÁö ¾Ê´Â´Ù. Programmer´Â object¸¦ À§ÇÑ
¸Þ¸ð¸® ÇÒ´çÀ» ¸í½ÃÇؾ߸¸ ÇÑ´Ù. ±×·¯³ª, ¸í½ÃÀûÀÎ ¸Þ¸ð¸® ¹Ý³³Àº ÇÊ¿ä¾ø´Ù.
¿Ö³ÄÇϸé, garbage collector´Â ´õÀÌ»ó ÇÊ¿ä°¡ ¾øÀ»±ú ÀÚµ¿À¸·Î ¸Þ¸ð¸®¸¦ Á¤¸®ÇÑ´Ù.
ObjectÀÇ ¸Þ¸ð¸® ÇÒ´çÀº new ¿¬»êÀÚ¸¦ »ç¿ëÇÑ´Ù. ¶ÇÇÑ, new ¿¬»êÀÚ´Â instance
variableÀ» ÃʱâÈ ÇÑ ÈÄ instanceÀÇ constructor¸¦ È£ÃâÇÑ´Ù. Constructor´Â
object¸¦ ÃʱâÈ ÇÑ´Ù. ´ÙÀ½Àº ClassA¶ó´Â classÀÇ instance¸¦ »õ·Ó°Ô »ý¼ºÇÏ´Â
¿¹ÀÌ´Ù.
a = new ClassA();
´ÙÀ½Àº argument°¡ ÀÖ´Â constructor¸¦ ÈÄÃâÇÏ´Â ¿¡ÀÌ´Ù.
b = new ClassA(3,2);
1.4.6.1 ¾²·¹±âÁ¤¸®. (Garbage Collection)
Garbage collector´Â °£´ÜÇÏ°í °·ÂÇÑ ¸Þ¸ð¸® °ü¸®ÀÚÀÌ´Ù. Program¿¡¼´Â ¸í½ÃÀûÀÎ
¸Þ¸ð¸® ¹Ý³³À» Àý´ë ÇÊ¿äÇÏÁö ¾Ê´Â´Ù.; ÀÚµ¿À¸·Î ó¸®ÇØÁØ´Ù. Garbage collector´Â
¿©ÀüÈ÷ ÂüÁ¶µÇ´Â ¸Þ¸ð¸®´Â Àý´ë ¹Ý³³ÇÏ´Â °æ¿ì°¡ ¾ø°í, ´õÀÌ»ó ¾²ÀÌÁö ¾Ê´Â ¸Þ¸ð¸®´Â
Ç×»ó ¹Ý³³ÇÑ´Ù. ÀÌ°ÍÀº dangling pointer bug¿Í storage leak¸¦ ±ÙÀý½ÃŲ´Ù.
¶ÇÇÑ, designer·Î ÇÏ¿©±Ý systemÀûÀÎ ¸Þ¸ð¸® °ü·ÃºÎºÐÀ» ½Å°æ¾²Áö ¾Êµµ·Ï ÇØÁØ´Ù.
1.4.6.2 The null Reference.
¿¹¾à¾î 'null'Àº "no instance"¸¦ ³ªÅ¸³»´Â »ó¼ö°ªÀÌ´Ù. nullÀº ¾î¶°ÇÑ
instance¿¡µµ Àû¿ëÀÌ µÇ¸ç, ¾î¶°ÇÑ class typeÀ¸·Îµµ Çüº¯È¯ÀÌ °¡´ÉÇÏ´Ù.
1.4.7 Static Methods, Variables, and Initializers.
Class¿¡¼ variable°ú method´Â staticÀ¸·Î ¼±¾ðµÉ ¼ö ÀÖ´Ù. ÀÌ°ÍÀº classÀÇ
instance¸¦ À§ÇÑ°ÍÀ̶ó±â º¸´Ù´Â, class ÀÚü¸¦ À§ÇÑ °ÍÀÌ´Ù. ¶ÇÇÑ, class ³»ºÎ
codeÀÇ blockµµ staticÀ¸·Î Á¤ÀÇÇÒ ¼ö ÀÖ´Ù. ÀÌ°ÍÀ» static initializer¶ó°í ÇÑ´Ù.
Static variableÀº instance variableó·³, initializer¸¦ °¡Áú¼ö ÀÖ´Ù.
Static variableÀº instanceÀÇ ¼ö¿¡´Â »ó°ü ¾øÀÌ ¿ÀÁ÷ class¿¡ Çϳª¾¿¸¸ ¸Þ¸ð¸®
ÇÒ´çÀÌ µÇ¾î °øÀ¯ÇÑ´Ù. Static variable°ú static method´Â µÑ´Ù classÀÇ À̸§À»
ÀÌ¿ëÇÏ¿© Á¢±ÙÇÒ ¼ö ÀÖ´Ù. ¶ÇÇÑ classÀÇ instance¸¦ ÀÌ¿ëÇؼµµ Á¢±ÙÇÒ ¼ö ÀÖ´Ù.
class Ahem {
int i; // Instance variable
static int j; // Static variable
static int arr[] = new int[12];
static { // static initializer:
// initialize the array
for (int i = 0; i < arr.length; i++) {
arr[i] = i;
}
}
void seti(int i) { // Instance method
this.i = i;
}
static void setj(int j) { // Static method
Ahem.j = j;
}
static void clearThroat() {
Ahem a = new Ahem();
Ahem.j = 2; // valid; static var via class
a.j = 3; // valid; static var via instance
Ahem.setj(2); // valid; static method via class
a.setj(3); // valid; static method via instance
a.i = 4; // valid; instance var via instance
Ahem.i = 5; // ERROR; instance var via class
a.seti(4); // valid; instance method via instance
Ahem.seti(5); // ERROR; instance method via class
}
}
1.4.7.1 ¼±¾ð ¼ø¼. (Order of Declarations)
Class, method, instance variableÀÇ ¼±¾ð ¼ø¼´Â »ó°üÀÌ ¾ø´Ù. ±×·¯³ª,
ÃʱâÈ ÇÏ´Â ¼ø¼¿¡´Â ¿µÇâÀ» ÁÙ ¼ö ÀÖ´Ù. ´ÙÀ½Àº ¸ðµÎ ¿Ã¹Ù¸¥ ¿¹ÀÌ´Ù.
class A {
void a() {
f.set(42);
}
B f;
}
class B {
void set(long n) {
this.n = n; }
long n;
}
1.4.7.2 ÃʱâÈ ¼ø¼. (Order of Initialization)
Class°¡ loadµÉ¶§, ¤±¸ðµç static initialization code°¡ ½ÇÇà µÈ´Ù.
Static initializer´Â static variableÀÌ ÃʱâÈ µÇ´Â°Í°ú °°Àº ½Ã°£¿¡ ½ÇÇàµÈ´Ù.
ÃʱâÈ´Â ¾îÈÖ¼ø¼·Î 󸮵ȴÙ. ´ÙÀ½Àº class C¸¦ ¼±¾ðÇÑ °ÍÀÌ´Ù.
class C {
static int a = 1;
static {
a++;
b = 7;
}
static int b = 2;
}
class C°¡ loadµÉ¶§ ´ÙÀ½ÀÇ ¼ø¼·Î ÀÌ·ç¾îÁø´Ù.
- a°¡ 1·Î ÃʱâÈ µÈ´Ù.
- static initializer°¡ ½ÇÇàµÇ¾î, a´Â 2, b´Â 7·Î ÃʱâÈÇÑ´Ù.
- b¸¦ 7·Î ÃʱâÈÇÑ´Ù.
¸¸¾à loadµÇÁö ¾ÊÀº classÀÇ static initialization code°¡ ´Ù¸¥ loadµÇÁö ¾ÊÀº
class·Î¸¦ ÂüÁ¶Çϸé, ÀÌ ÂüÁ¶µÇ´Â class´Â loadµÇ¾î¼ Á¦ÀϸÕÀú ÀÚ½ÅÀÇ
static initialization code¸¦ ½ÇÇàÇÑ´Ù.
À̶§ ÂüÁ¶µÇ´Â classÀÇ static initialization code¿¡¼ ´Ù½Ã loadµÇÁö ¾ÊÀº
class¸¦ ÂüÁ¶ÇÏ°Ô µÇ¸é, cycleÀÌ »ý¼ºµÇ¾î, NoClassDefFoundExceptionÀÌ
½ÇÇàµÈ´Ù.
¿¹¸¦ µé¾î¼, ¸¸¾à ClassA°¡ loadµÇ¸é, ÀÚ½ÅÀÇ static initialization code°¡
½ÇÇà µÈ´Ù. ±×·¯³ª, ClassAÀÇ static initialization code´Â ´Ù¸¥ loadµÇÁö ¾ÊÀº
´Ù¸¥ ClassB¸¦ ÂüÁ¶ ÇÒ ¼ö°¡ Àִµ¥, À̶§ ClassB´Â loadµÇ¾î¼, ClassAº¸´Ù ¸ÕÀú
ÀÚ½ÅÀÇ static initialization code¸¦ ½ÇÇàÇÑ´Ù. ±×·±ÈÄ, ClassAÀÇ static
initializationÀÌ ½ÇÇàµÈ´Ù. ¸¸¾à ClassB°¡ ´Ù½Ã ClassAÀÇ static initialization
code¸¦ ÂüÁ¶Çϸé, cycleÀÌ »ý¼ºµÈ´Ù.
´ÙÀ½Àº static variableÀÇ ÃʱâÈ °úÁ¤¿¡¼ forward dependency¿¡ µû¸¥
compilie-time error ¿¹Á¦ÀÌ´Ù.
int i = j + 2;
int j = 4;
instance variableÀÇ ÃʱâÈ ¿¡¼ static vaariable¿¡ µû¸¥ forward dependency·Î
º¸ÀÌ´Â ¿¹Á¦ÀÌ´Ù.
int i = j + 2; // Instance variable
static int j = 4; // Static variable
À§¿¡¼, i´Â j¿¡ÀÇÇؼ forward dependencyó·³ º¸À̳ª, i´Â 6À¸·Î j´Â 4·Î ÃʱâÈ
µÈ´Ù. ¿Ö³ÄÇϸé static variableÀÎ j°¡ instance variableÀÎ iº¸´Ù ¸ÕÀú ÃʱâÈ°¡
µÇ±â ¶§¹®ÀÌ´Ù.
Static method´Â instance variableÀ» ÂüÁ¶ ÇÒ ¼ö ¾ø´Ù. ¿ÀÁ÷ static method¿Í¤¿
staatic variable¸¸À» ÂüÁ¶ ÇÒ ¼ö ÀÖ´Ù.
1.4.8 Access Specifiers.
Access specifier´Â programmer°¡ method¿Í variableÀÇ Á¢±ÙÀ» controlÇϱâÀ§ÇÑ
°ÍÀÌ´Ù. public, private, proteced°¡ ÀÖ´Ù. publicÀº ¾î´À°÷¿¡¼µçÁö Á¢±Ù ÇÒ ¼ö
ÀÖÀ½À» ÀǹÌÇϸç, private´Â ÀÚ½ÅÀÌ ¼±¾ðµÇ class ³»¿¡¼¸¸ Á¢±Ù ÇÒ ¼ö ÀÖ´Ù.
private method´Â class ¿ÜºÎ¿¡¼´Â º¼ ¼ö°¡ ¾øÀ¸¹Ç·Î, finalÀÌ°í overrideÇÒ ¼ö
¾ø´Ù. protected´Â ¿ÀÁ÷ subclass¸¸ÀÌ method¿Í variableÀ» ÂüÁ¶ ÇÒ ¼ö ÀÖ´Ù.
Public Á¢±ÙÀº class, method, variable¿¡ Àû¿ëÇÒ ¼ö ÀÖ´Ù. Class, method,
variableÀÌ publicÀ¸·Î Ç¥½ÃµÇ¸é, ´Ù¸¥ class³ª method¿¡¼ ¾î´À °÷ ¿¡¼µçÁö
Á¢±ÙÀÌ °¡´ÉÇÏ´Ù.
Package³»¿¡¼ class, method, variableÀº priavate, public À» ²À ¸í½ÃÇÒ
ÇÊ¿ä´Â ¾ø´Ù.
1.4.9 Varable Scoping Rules.
Class°¡ subclass·Î Á¤ÀÇµÉ ¶§, package³»¿¡¼ superclassÀÇ ¼±¾ðµÈ ³»¿ëÀº
subclass³»¿¡¼ º¼ ¼ö ÀÖ´Ù. method³»¿¡¼ ¼±¾ðµÈ variableÀº, ´ÙÀ½ÀÇ
scoping ±ÔÄ¢¿¡ µû¸¥´Ù.
- 1. Á¦ÀϸÕÀú ÇöÀç blockÀ»Ã£°í, ÇöÀç method¿¡ µÑ·¯½ÎÀÎ Àüü blockÀ»
Àæ´Â´Ù. ÀÌ°ÍÀº local scope¿¡ °üÇÑ À̾߱âÀÌ.
- 2. ÇöÀç classÀÇ variableÀ» ã´Â´Ù.
- 3. ¸¸¾à variableÀ» ãÁö ¸øÇϸé, °¡Àå °¡±î¿î superclass·ÓÅÍ,
¸ðµç superclassÀÇ variable ¿¡¼ ã¾Æ¼ Object class±îÁö ¿Ã¶ó°£´Ù.
±×·¡µµ ãÁö ¸øÇϸé, ImportµÈ class¿Í package¸¦ ã´Â´Ù. ±×·¡µµ ¸øãÀ¸¸é,
compile-time error¸¦ ³»º¸ÀÌ°Ô µÈ´Ù.
°°Àº class³»¿¡¼ °°Àº À̸§ÀÇ multiple variableÀº Çã¿ëÇÏÁö ¾Ê°í, compile-time
error¸¦ ³»º¸³½´Ù.
1.4.10 Modifiers.
1.4.10.1 Threadsafe Variables.
Instance variable ¶Ç´Â static variableÀº 'threadsafe'¶ó°í ¸í½ÃÇÏ¿©,
ÇÑ thread°¡ º¯¼ö¸¦ »ç¿ëÇÏ°í ÀÖÀ»¶§, ´Ù¸¥ thread°¡ Á¢±ÙÇÏ¿© °ªÀ» Àý´ë ºÎ²ÙÁö
¸øÇϵµ·Ï º¸È£ÇÏ¿©¾ß ÇÑ´Ù.(º¯¼ö°ª ºñµ¿±âÀûÀ¸·Î ¹Ù²ÙÁö ¸øÇϵµ·ÏÇÑ´Ù.)
Primary optimizationÀº threadsafe¸¦ ÀÌ¿ëÇÏ¿© instance variableÀ» register¿¡
cachingÀ» ÇÑ´Ù.
1.4.10.2 Transient Variables.
'transient' flag´Â interpreter·Î ÇÏ¿©±Ý, classÀÇ instance¸¦
persistent object·Î ó¸®Çϵµ·Ï ÇÑ´Ù.
1.4.10.3 Final Classes, Methods, and Variables.
'final'·Î Ç¥½ÃµÈ class´Â Àý´ë subclass¸¦ ÀÚÁöÁö¸øÇϵµ·Ï ÇÑ´Ù. Final method´Â
method bodyÀÇ inline±â´ÉÀ» ÇÑ´Ù.
´ÙÀ½Àº finalÀÇ ´Ù¾çÇÑ ¿¹ÀÌ´Ù.
class Foo {
final int value = 3; // final variable
final int foo(int a, int b) { // final method
...
}
}
1.4.10.4 Native Methods.
'native'·Î Ç¥½ÃµÈ method´Â...
1.4.10.5 Abstract Methods.
Abstract method´Â subclassµé°£ÀÇ ±Ô¾àÀ» Á¤Çϱâ À§ÇÑ superclass ¶Ç´Â
interfaceÀÇ ±â´ÉÀ» Á¦°øÇÑ´Ù. Abstract method´Â subclass¿¡¼´Â ¹Ýµå½Ã
Á¤ÀǸ¦ ÇØ¾ß ÇÑ´Ù. Abstract method´Â method body°¡ ¾ø°í, ´ë½Å ¼±¾ðÀº semicolon
À¸·Î ³¡³´Ù.
'abstract' keyword´Â ´ÙÀ½°ú °°Àº ±ÔÄ¢À» °¡Áö°í ÀÖ´Ù.
- Constructor´Â abstract ÇÒ ¼ö ¾ø´Ù.
- Static method´Â abstract ÇÒ ¼ö ¾ø´Ù.
- Private method´Â abstract ÇÒ ¼ö ¾ø´Ù.
- Abstract method´Â abstract method°¡ ¼±¾ðµÇ¾îÀÖ´Â classÀÇ subclass¿¡
Á¤ÀÇÇÑ´Ù.
- SuperclassÀÇ method·ÎºÎÅÍ overrideµÈ method´Â abstract ÇÒ ¼ö ¾ø´Ù.
- Abstract method¿Í overriding ÇÏÁö ¾Ê°í abstract method¸¦ »ó¼Ó¹ÞÀº class¸¦
abstract class¶ó°í ÇÑ´Ù.
- Abstract class¸¦ instantiateÇϰųª, abstract method¸¦ Á÷Á¢ È£ÃâÇϸé
compile-time error¸¦ ³»º¸³½´Ù.
1.4.10.6 Synchronized Methods and Blocks.
'synchronized'´Â method³ª blockÀÇ code¿¡ lockÀ» °Å´Â°ÍÀÌ´Ù. LockÀº
synchronized code°¡, °°Àº ½Ã°£¿¡ °°Àº resource¸¦ accessÇÏ´Â ¼·Î´Ù¸¥ code
·Î ½ÇÇà µÇÁö ¾Êµµ·Ï ¹æÁöÇÏ´Â °ÍÀÌ´Ù. °¢°¢ÀÇ object´Â ÇϳªÀÇ lockÀ» °¡Áö°íÀÖ´Ù.
Synchronized method°¡ È£Ã⠵Ǹé, ÇöÀç instance¸¦ À§ÇÑ lockÀ» ¾òÀ»¶§ ±îÁö
±â´Ù¸°´Ù. LockÀ» ¾òÀº ÈÄ¿¡´Â code¸¦ ½ÇÇà ½ÃŲÈÄ lockÀ» ¹Ý³³ÇÑ´Ù.
Synchronized blockÀº synchronized methodó·³ ¼öÇàÇÑ´Ù. ´Ü ´Ù¸¥Á¡Àº,
ÇöÀçÀÇ instance³ª class¸¦ À§ÇÑ lockÀ» »ç¿ëÇϴ´ë½Å, sysnchronized blockÀº
object¿Í °ü·ÃµÈ lockÀ̳ª, class¿¡¼ Á¦°øÇÏ´Â blockÀÇ synchronized ¹®ÀåÀ»
»ç¿ëÇÑ´Ù´Â °ÍÀÌ´Ù.
Synchronized blockÀº ´ÙÀ½°ú °°ÀÌ ¼±¾ðÇÑ´Ù.
/* ...preceding code in the method... */
synchronized(<object or class name>) { //sync. block
/* code that requires synchronized access */
}
/* ...remaining code in the method... */
´ÙÀ½Àº synchronized method ¼±¾ð ¹æ¹ýÀÌ´Ù.
class Point {
float x, y;
synchronized void scale(float f) {
x *= f;
y *= f;
}
}
´ÙÀ½Àº synchronized blockÀÇ ¿¹ÀÌ´Ù.
class Rectangle {
Point topLeft;
...
void print() {
synchronized (topLeft) {
println("topLeft.x = " + topLeft.x);
println("topLeft.y = " + topLeft.y);
}
...
}
}
1.5 Interface.
Interface´Â body¸¦ ¼öÇàÇÏÁö ¾Ê´Â methodÀÇ ¹À½ÀÌ´Ù.
Interface´Â method protocolÀ» ĸ½¶ÈÇÏ¿© Á¦°øÇÔÀ¸·Î½á ÇϳªÀÇ inheritance tree¸¦
±¸¼ºÇÒ ¼ö ÀÖµµ·Ï Á¦ÇÑÇϴ°ÍÀÌ´Ù.
Interface´Â runtimeÀÇ ºÎÇϸ¦ ¾ø¾Ö±âÀ§Çؼ Á¦°øÇÏÁö ¾Ê´Â mulitple
inheritanceÀÇ ¹®Á¦¸¦ ÇØ°áÇÑ´Ù.
±×·¯³ª, interface synamic method bindingÀ» Æ÷ÇÔÇÏ°í ÀÖÀ¸¹Ç·Î, »ç¿ëÇϴµ¥ Å«
ºÎÇÏ°¡ ¾ø´Ù.
Interface¸¦ »ç¿ëÇÔÀ¸·Î½á class´Â ¼·Î ¾î¶² ÀÏÀ» ÇÏ´ÂÁö ¾Ë ÇÊ¿ä°¡ ¾ø´Â
programming interface¸¦ °øÀ¯ ÇÒ ¼ö ÀÖ´Ù. ´ÙÀ½Àº 'interface' keyword¸¦ ÀÌ¿ëÇÏ¿©
interfaceÀÇ ¼±¾ðÀ» º¸¿©ÁÖ°í, interface¸¦ ¼öÇàÇÏ´Â class¸¦ º¸¿©ÁØ´Ù.
public interface Storing {
void freezeDry(Stream s);
void reconstitute(Stream s);
}
public class Image implements Storing, Painting {
...
void freezeDry(Stream s) {
// JPEG compress image before storing
...
}
void reconstitute (Stream s) {
// JPEG decompress image before reading
...
}
}
1.5.1 Interface as Types.
¼±¾ð¹® "interfaceName variableName"´Â interfaceNameÀ» ¼öÇàÇÏ´Â classÀÇ
interface·Î variable, parameter¸¦ ¼±¾ðÇÏ´Â ¹®ÀåÀÌ´Ù. ObjectÀÇ typeÀÌ
¹«¾ùÀÌ°í, »ó¼Ó¹ÞÀº°ÍÀ» Á¤È®È÷ ¾ËÁö ¸øÇÏ´õ¶óµµ, Object°¡
ÁÖ¾îÁø interfaece¸¦ ¼öÇàÇØ¾ß ÇÑ´Ù´Â °ÍÀ» Programmer´Â ¸í½ÃÇØ¾ß ÇÑ´Ù.
´ÙÀ½ÀÇ pseudocode´Â "interfaceName variableName"À» ¼³¸íÇÏ°í ÀÖ´Ù.
class StorageManager {
Stream stream;
...
// Storing is the interface name
void pickle(Storing obj) {
obj.freezeDry(stream);
}
}
1.5.2 Methods in Interfaces.
Interface¾ÈÀÇ method´Â ´ÙÀ½°ú °°ÀÌ ¼±¾ðÇÑ´Ù.
returnType methodName ( parameterList );
InterfaceÀÇ method´Â public, abstract.. µî ¸ðµç modifier¸¦ Àû¿ëÇÒ ¼ö ÀÖ´Ù.
1.5.3 Variables inInterfaces.
InterfaceÀÇ variableÀº final, public, staticÀÌ´Ù. ±×¹ÛÀÇ modifier´Â Çã¿ëÇÏÁö
¾Ê´Â´Ù. VarableÀº ÃʱâÈ µÇ¾î¾ß ÇÑ´Ù.
1.5.4 Combining Interfaces.
Interface´Â 'extends' keyword¸¦ ÀÌ¿ëÇÏ¿© Çϳª ¶Ç´Â ±×ÀÌ»óÀÇ interface¿Í
È¥ÇÕÇÒ ¼ö ÀÖ´Ù. ´ÙÀ½Àº ±× ¿¹ÀÌ´Ù.
interface DoesItAll extends Storing, Painting {
void doesSomethingElse();
}
1.6 Packages.
Package´Â class¿Í interfaceÀÇ ¹À½ÀÌ´Ù. ¸¹Àº À̸§µéÀ» °ü¸®ÇÏ°í, ¼·ÎÀÇ Ãæµ¹À»
ÇÇÇÏ´Â toolÀÌ´Ù. ¸ðµç class¿Í interfaceÀ̸§Àº °°Àº package¿¡ Æ÷ÇԵǾî ÀÖ´Ù.
ÆíÀÇ»ó, packages À̸§Àº ¸¶Ä§Ç¥·Î ±¸ºÐÇÑ´Ù.(period-separated words)
ù¹ø° À̸§Àº package¸¦ °³¹ßÇÑ ±¸Á¶¸¦ ³ªÅ¸³½´Ù.
1.6.1 Specifying a Compilation Unit's Package.
Compilation unitÀÇ·ÎÀÇ package´Â 'package'¹®ÀåÀ¸·Î ³ªÅ¸³½´Ù.
À̹®ÀåÀÌ ³ªÅ¸³ª¸é, Compilation unit¿¡¼´Â non-comment, non-white space line
À̾î¾ß ÇÑ´Ù. ±×Çü½ÄÀº ´ÙÀ½°ú °°´Ù.
package packageName;
Compilation unit¿¡ package¹®ÀåÀÌ ¾øÀ¸¸é, À̸§ÀÌ ¾ø´Â default package·Î
´ëÄ¡µÈ´Ù.
1.6.2 Using Classes and Interfaces from Other Packages.
Java´Â packageÀÇ class¿Í interface¸¦ Á¤ÀÇÇÏ°í, »ç¿ëÇÒ ¼ö ÀÖ´Â ±¸Á¶¸¦
Á¦°øÇÑ´Ù. 'import' keyword´Â class¿¡¼ package¸¦ »ç¿ëÇÒ ¼ö Àִٴ°ÍÀ»
³ªÅ¸³½´Ù. Compilation uniÀº ÀÚµ¿À¸·Î ±× packageÀÇ class¿Í interface¸¦ °¡Á®¿Â´Ù.
PackageÀÇ code´Â ´Ù¸¥ packageÀÇ class³ª interface¸¦ ÇÑ°¡Áö ¶Ç´Â µÎ°¡Áö
¹æ¹ýÀ¸·Î ¸í½Ã ÇÒ ¼ö ÀÖ´Ù.
- package À̸§°ú ÇÔ²² class³ª interfaceÀ̸§À» ¾Õ¿¡¼ºÎÅÍ ³ª¿ÇÏ¿© Ç¥ÇöÇÑ´Ù.
// prefacing with a package
acme.project.FooBar obj = new acme.project.FooBar();
- 'import' ¹®ÀåÀ» ÀÌ¿ëÇÏ¿© »ç¿ëµÇ´Â class³ª interface, ¶Ç´Â packageÀÇ
³»¿ëÀ» »ç¿ëÇÔÀ» ¾Ë¸°´Ù. ´ÙÀ½Àº »ç¿ëµÇ´Â acme.project·ÎºÎÅÍ ¸ðµç public class¸¦
»ç¿ëÇѴٴ°ÍÀ» ÀǹÌÇÑ´Ù.
// import all classes from acme.project
import acme.project.*;
´ÙÀ½Àº acme.pro¿¡¼ class Emplyee_List Çϳª¸¸À» importÇÑ´Ù.
// import Employee_List from acme.project
import acme.project.Employee_List;
Employee_List obj = new Employee_List();
±×·¯³ª ÀÌ°ÍÀº À߸ø µÇ¾ú´Ù. class À̸§ÀÌ ¸ðÈ£ÇϹǷΠcompile-time error¸¦
³»º¸³»°Ô µÈ´Ù. PackageÀÇ classÀ̸§À» Æ÷ÇÔÇÔÀ¸·Î½á ¿ÏÀüÈ÷ ºÎ¿©µÈ classÀ̸§À»
»ç¿ëÇÒ ¼ö ÀÖ´Ù.
1.7 Expressions
JavaÀÇ expressionÀº C¿Í ¾ÆÁÖ À¯»çÇÏ´Ù.
1.7.1 Operations.
´ÙÀ½Àº ¿ì¼± ¼øÀ§¿¡ µû¸¥ ¿¬»êÀÚ ÀÌ´Ù.
. [] ()
++ -- ! ~ instanceof
* / %
+ -
<< >> >>>
< > <= >=
== !=
&
^
|
&&
||
?:
= op=
,
1.7.1.1 Operators on Integers.
´ÙÀ½ÀÇ ¿¬»êÀÚ´Â °á°ú°ªÀÌ integerÀÌ´Ù. ÇÇ¿¬»êÀÚÁß longÀÌ ÀÖÀ¸¸é,
°á°ú°ªÀº longÀÌ°í, ±×¿ÜÀÇ °ÍÀº ¸ðµÎ int typeÀÌ´Ù. ±×·¯¹Ç·Î, ¸¸¾à º¯¼ö i°¡
short³ª byte·Î ¼±¾ðµÇ¾îÀ־ i+1Àº intÀÌ´Ù. °á°ú°ªÀÌ ¿¬»êÀÚÀÇ ¹üÀ§¸¦ ¹þ¾î³ª¸é,
modulo½ÃŲ´Ù.
´ÙÀ½Àº unary integer ¿¬»êÀÚÀÌ´Ù.
"++lvalue"´Â "lvalue += 1"°ú °°°í, "lvalue = lvalue+1"°úµµ °°´Ù.
´ÙÀ½Àº binary integer ¿¬»êÀÚÀÌ´Ù.
³ª´©±â¿¡¼ " (a/b)*b + (a%b) == a "ó·³ 0À¸·Î ³ª´©´Â ¿¬»êÀº
ArithmeticExceptionÀ» ½ÇÇà½ÃŲ´Ù.
<, >, <=, >=, ==, !=´Â °á°ú°ªÀÌ booleanÀÌ´Ù.
abs(x), max(x, y), min(x, y) ¿¬»êÀÚ´Â integer¸¦ Æ÷ÇÔÇÑ ¸ðµç ¼ýÀÚ¿¡¼ ½ÇÇàµÈ´Ù.
1.7.1.2 Operators on Boolean Values.
boolean¿¬»ê°ú ¼¯ÀΠǥÇöÀº °á°ú°ªÀÌ booleanÀÌ´Ù. !´Â boolean negationÀ»
ÀǹÌÇÏ´Â ¿¬»êÀÚ ÀÌ°í, ÀÌÁø ¿¬»êÀÚ(binary operator) &, |, ^Àº ÀǹÌÀûÀ¸·Î
AND, OR, XOR¸¦ ÀǹÌÇÑ´Ù. µû¶ó¼ &&, ||, ==, !=, &=, !=, ^=À» »ç¿ëÇÒ ¼ö ÀÖ´Ù.
1.7.1.3 Operators on Floating Point Values.
+, -, *, /, +=, -=, *=, /=, ++, --, %, %=¸¦ »ç¿ëÇÒ ¼ö ÀÖ´Ù.
´ÙÀ½ÀÇ ³ª¸ÓÁö ¿¬»êÀº °¢°¢ °°Àº ÀÇ¹Ì ÀÌ´Ù.
a % b
a - ((int)(a / b) * b)
abs(x), max(x, y), min(x, y)¿ª½Ã »ç¿ëÇÒ ¼ö ÀÖ´Ù.
>, <, >=, <=, ==, !=¸¦ »ç¿ëÇÒ ¼ö ÀÖ´Ù.
±×¹ÛÀÇ ³»¿ëÀº ºÎ·Ï¿¡¼ ¼³¸íÇÏ°Ú´Ù.
1.7.1.4 Operators on Arrays.
´ÙÀ½Àº ¹è¿ÀÇ ÇÑ¿ø¼ÒÀÇ °ªÀ» °¡Àú¿À´Â Çü½ÄÀÌ´Ù. IndexÀÇ ¹üÀ§´Â 0¿¡¼ ¹è¿ÀÇ
±æÀÌ -1±îÁöÀε¥, ±× ¹üÀ§´Â ¿ÀÁ÷ runtime¿¡¸¸ °Ë»çÇÑ´Ù.
<expression>[<expression>]
1.7.1.5 Operators on Strings.
¹®ÀÚ¿Àº String object·Î 󸮵ȴÙ. +¿¬»êÀÚ´Â StringÀ» ¿¬°á ½ÃÄÑÁִµ¥,
¸¸¾à ÇÇ¿¬»êÀÚ°¡ StringÀÌ ¾Æ´Ï¸é ÀÚµ¿À¸·Î ÇÇ¿¬»êÀÚ¸¦ StringÀ¸·Î ¹Ù²Ù¾î¼
ó¸®ÇØÁØ´Ù. ¶ÇÇÑ, ÇÇ¿¬»êÀÚ°¡ objectÀ̸é objectÀÇ String°ªÀ» returnÇØÁÖ´Â
toString() method¸¦ È£ÃâÇÑ´Ù.
// Examples of the + operator used with strings
float a = 1.0;
print("The value of a is " + a + "\n");
String s = "a = " + a;
+= ¿¬»êÀÚµµ Çã¿ëµÈ´Ù.
s1 += a; //s1 = s1 + a; // a is converted to String if necessary
1.7.1.6 Operators on Objects.
'instanceof'´Â ƯÁ¤ object°¡ ƯÁ¤ class¶Ç´Â subclassÀÇ instanceÀÎÁö °Ë»çÇÑ´Ù.
if (thermostat instanceof MeasuringDevice) {
MeasuringDevice dev = (MeasuringDevice)thermostat;
...
}
À§ÀÇ ¿¹´Â tthermostaat°¡ MeasuringDevice objectÀÇ instanceÀÎÁö ¾Ë¾Æº»´Ù.
1.7.2 Casts and Conversion.
Java language¿Í runtime systemÀº systemÀ» ¸ÁÄ¡Áö ¾Êµµ·Ï Çüº¯È¯¿¡ Á¦ÇÑÀ» µÐ´Ù.
Integer´Â floaaating point´Â Çüº¯È¯ÀÌ °¡´ÉÇÏ´Ù. ±×·¯³ª, integer´Â arrary³ª
object·Î Çüº¯È¯À» ÇÒ ¼ö ¾ø´Ù. Object´Â base typeÀ¸·Î Çüº¯È¯À» ÇÒ ¼ö ¾ø´Ù.
Instance´Â superclass·Î ¾Æ¹« Á¦ÇÑ ¾øÀÌ Çüº¯È¯À» ÇÒ ¼ö ÀÖÀ¸³ª, subclass·ÎÀÇ
Çüº¯È¯Àº runtime check¸¦ ÇÑ´Ù. ¸¸¾à object°¡ subclassÀÇ instance°¡ ¾Æ´Ñ
subclass·Î Çüº¯È¯ÀÌ µÇ¸é, runtime systemÀº ClassCastExceptionÀ» ½ÇÇà½ÃŲ´Ù.
1.8 Statements.
1.8.1 ¼±¾ð¹®. (Declarations)
¼±¾ðÀº statement°¡ Çã¿ëµÈ°÷ÀÌ¸é ¾î´À°÷¿¡¼µç ÇÒ ¼ö ÀÖ´Ù. Scope´Â ¼±¾ðµÈ
ºí·°À» ¹þ¾î³ª¸é º¼ ¼ö ¾ø´Ù.
¶ÇÇÑ, for¹® ³»¿¡¼µµ °¡´ÉÇÏ´Ù.
for (int i = 0; i < 10; i++) {
...
}
À§¿Í°°ÀÌ º¯¼ö¸¦ ¼±¾ðÇϸé for¹® ³»¿¡¼¸¸ À¯¿ëÇÏ´Ù.
´ÙÀ½Àº À§¿Í µ¿µîÇÑ ¿¹¹®ÀÌ´Ù.
{
int i = 0;
for (; i < 10; i++) {
...
}
}
1.8.2 Expressions.
´ÙÀ½Àº expressionÀÌ´Ù.
a = 3;
print(23);
foo.bar();
1.8.3 È帧 Á¦¾î. (Control Flow)
´ÙÀ½Àº È帧 Á¦¾î¸¦ ¿ä¾àÇؼ º¸¿©ÁÖ´Â ¿¹ÀÌ´Ù.
if(boolean) statement
else statement
switch(e1) {
case e2: statements
default: statements
}
break [label];
continue [label];
return e1;
for([e1]; [e2]; [e3]) statement
while(boolean) statement
do statement while(boolean);
Java´Â labeled loop¿Í labeled break¸¦ Áö¿øÇÑ´Ù. ´ÙÀ½Àº ±× ¿¡ÀÌ´Ù.
outer: // the label
for (int i = 0; i < 10; i++) {
for (int j= 0; j< 10; j++) {
if (...) {
break outer;
}
if (...) {
}
}
}
Loop¿Í break¿¡¼ labelÀ» »ç¿ëÇÒ ¶§´Â ´ÙÀ½°ú °°Àº ±ÔÄ¢ÀÌ ÀÖ´Ù.
- ¸ðµç ¹®ÀåÀº labelÀ» °¡Áú ¼ö ÀÖ´Ù.
- break¹®ÀÇ lableÀº enclosing statementÀÇ labelÀ̾î¾ß ÇÑ´Ù.
- continue¹®ÀÇ labelÀº enclosing loopÀÈ labelÀ̾î¾ß ÇÑ´Ù.
1.8.4 Exception.
Java program¿¡¼ ¿À·ù°¡ ¹ß»ýµÇ¸é, ¿¹¸¦ µé¾î¼ argunemt°ªÀÌ À߸øµÇ¾ú´Ù¸é,
¿À·ù¸¦ ¹æÁöÇϱâ À§ÇÑ code°¡ ½ÇÇàµÈ´Ù. ¸¸¾à ±×·¯ÇÑ code°¡ ¾øÀ» °æ¿ì¿¡´Â
¿À·ù message¸¦ º¸¿©ÁÖ°í ¸¶Ä¡°Ô µÈ´Ù. ±×·¯³ª, programÀº catch, recoverµîÀ»
ÀÌ¿ëÇÏ¿© exception handler¸¦ °¡Áú ¼ö ÀÖ´Ù.
ExceptionÀº runtime system¿¡ ÀÇÇؼ 󸮵ȴÙ. ±×·¯³ª, ¸ðµç class´Â
'throw'¹®À» ÀÌ¿ëÇؼ exceptionÀ» ´ëºñÇÒ ¼ö ÀÖ´Ù. 'throw'¹®Àº object¸¦ Æ÷ÇÔÇÑ
'throw' keyword¸¦ ÀÌ¿ëÇÑ´Ù. ÆíÀÇ»ó, ÀÌ object´Â ExceptionÀ̳ª Exception
subclassÀÇ instanceÀ̾î¾ß ÇÑ´Ù. 'throw'¹®Àº Àû´çÇÑ exception haandleÀ»
½ÇÇà ½ÃŲ´Ù. 'throw'¹®ÀÌ ½ÇÇà µÉ¶§, ¾Æ¹«·± code°¡ ½ÇÇà µÇ´Â°ÍÀÌ ¾Æ´Ï¶ó,
return °ªÀÌ ¾ø´Â µÑ·¯½ÎÀÎ method°¡ ½ÇÇàµÈ´Ù. ´ÙÀ½Àº ExceptionÀÇ subclass¸¦
»ý¼ºÇÏ¿© exceptionÀ» ó¸®(throw)ÇÏ´Â ¿¹ÀÌ´Ù.
class MyException extends Exception {
}
class MyClass {
void oops() {
if (/* no error occurred */) {
...
} else { /* error occurred */
throw new MyException();
}
}
}
Exception handler¸¦ Á¤ÀÇÇϱâ À§Çؼ, Á¦ÀϸÕÀú programÀº 'try'¹®À» »ç¿ëÇÏ¿©
code¸¦ µÑ·¯½ÑÈÄ, ¿À·ù°¡ ¹ß»ýÇß´ÂÁö °Ë»çÇÑ´Ù. ¿À·ù°¡ ¹ßÇàµÇ¸é, 'catch'¹®À»
»ç¿ëÇÏ¿© exception handling code¸¦ ½ÇÇà ½ÃŲ´Ù. ´ÙÀ½Àº ±× ¿¹ÀÌ´Ù.
try {
p.a = 10;
} catch (NullPointerException e) {
println("p was null");
} catch (Exception e) {
println("other error occurred");
} catch (Object obj) {
println("Who threw that object?");
}
'catch'¹®Àº parameterÇϳª¸¦ °®°í, return typeÀÌ ¾ø´Â method¿Í ºñ½ÁÇÏ´Ù.
Parameter´Â class¶Ç´Â instanceÀÌ¸é µÈ´Ù. ExceptionÀÌ ¹ß»ýÇϸé, 'try/catch'¹®ÀÇ
parameter¿Í ÀÏÄ¡ÇÏ´Â exceptionÀ» ã´Â´Ù. ExceptionÀÇ parameter´Â
´ÙÀ½°ú °°ÀÌ Ã£´Â´Ù.
- ÇöÀç classÀÇ exception.
- superclassÀÇ exception.
- ¸¸¾à parameter°¡ interface¸é, exception class´Â interface¸¦ ½ÇÇà½ÃŲ´Ù.
'try/catch'¹®Àº catch¹®À» ½ÇÇà ½Ãų exception¿¡ ÇØ´çÇÏ´Â parameter¸¦ °®´Â´Ù.
'catch'¹®ÀÌ ½ÇÇàµÈÈÄ, 'try/catch'¹® ´ÙÀ½ÀÇ ½ÇÇàÀ» ´Ù½ÃÇÑ´Ù.
ExceptionÀÌ ¹ß»ýÇÑ ½ÃÁ¡¿¡¼´Â, exception handler¸¦ Àç½ÇÇà Çϴ°ÍÀº ºÒ°¡´ÉÇÏ´Ù.
´ÙÀ½Àº ±× ¿¹ÀÌ´Ù.
print("now ");
try {
print("is ");
throw new MyException();
print("a ");
} catch(MyException e) {
print("the ");
}
print("time\n");
À§ÀÇ ¿¹´Â "now is the time"À̶ó°í Ãâ·ÂµÈ´Ù. À§ÀÇ ¿¹¿¡¼Ã³·³, exceptionÀº
¿ÀÁ÷ error handlingÀ» À§Çؼ¸¸ ¾²ÀÏ ÇÊ¿ä´Â ¾ø´Ù. ±×·¸Áö¸¸, ÀÌ·¯ÇÑ °æ¿ì´Â
code¸¦ ÀÌÇØÇϱ⠾î·Æ°Ô ¸¸µç´Ù.
Exception handler´Â Æ÷°³Áú ¼ö ÀÖ´Ù. Áï, ÇϳªÀÌ»óÀÇ exception handlingÇÒ
¼ö ÀÖ´Ù. Nested exception handlingÀº ù¹ø° handler¿¡¼ ¿À·ù¸¦ ¿ÏÀüÈ÷
º¹±¸ ½Ãų ¼ö ¾øÁö¸¸, ¾î´ÀÁ¤µµ ó¸®ÇØ¾ß ÇÒÀÏÀÌ ÀÖÀ» °æ¿ì¿¡ »ç¿ëÇÑ´Ù.
´ÙÀ½´Ü°èÀÇ ´õ ÈǸ¢ÇÑ exception handler·Î ³Ñ¾î°¥¶§, ´ÙÀ½ ¿¹¹®°ú °°ÀÌ
°°Àº object¸¦ »ç¿ëÇÏ¿© 'throw' keyword¸¦ ÀÌ¿ëÇÏ¿© ´ÙÀ½ ´Ü°è handler·Î
³Ñ¾î°£´Ù. ´Ü, ÀÌ·¸°Ô ´ÙÀ½ ´Ü°è·Î ³Ñ¾î°¡¸é 'throw'¹®Àº returnÇÏÁö ¾Ê±â ¶§¹®¿¡
´Ù½Ã Àç½ÇÇà ÇÒ ¼ö ¾ø´Ù.
try {
f.open();
} catch(Exception e) {
f.close();
throw e;
}
1.8.4.1 The finally Statement.
'finally'¹®Àº exceptionÀÌ ¹ß»ýÇÏ´õ¶óµµ ¸î¸î code¸¦ ½ÇÇàÇÏ°í ½ÍÀ»¶§ »ç¿ëÇÑ´Ù.
'catch'¹®À̳ª 'finally'¹®Àº 'try'¹® ´ÙÀ½¿¡ »ç¿ëÇÒ ¼ö ÀÖ´Ù. ÇÏÁö¸¸ µ¿½Ã¿¡
µÑÀ» »ç¿ëÇÒ ¼ö ¾ø´Ù. ´ÙÀ½Àº ±× ¿¹ÀÌ´Ù.
try {
// do something
} finally {
// clean up after it
}
À§ÀÇ code´Â ´ÙÀ½ ¾Æ·¡ÀÇ °Í°ú ºñ½ÁÇÏ´Ù.
try {
// do something
} catch(Object e){
// clean up after it
throw e;
}
// clean up after it
'finally'¹®Àº 'try' block¿¡¼ return, break, continue, throw¹®À» ½ÇÇà
½ÃÅ°´õ¶óµµ ½ÇÇàµÈ´Ù. ¿¹¸¦ µé¾î, ´ÙÀ½ codeÀÇ °á°ú´Â "finally"¶ó°í Ãâ·ÂµÈ´Ù.
±×·¯³ª "after try"´Â a!=10À϶§¸¸ Ãâ·Â µÈ´Ù.
try {
if (a == 10) {
return;
}
} finally {
print("finally\n");
}
print("after try\n");
1.8.4.2 Runtime Exceptions.
À̹ø¿¡´Â ¿©·¯°¡ÀÚ ´Ù¾çÇÑ ¿À·ù°¡ ¹ß»ýÇßÀ»¶§ ¼öÇàµÇ´Â Java runtime system
throwÀÇ exception ¸ñ·ÏÀ» ¾Ë¾Æº¸°Ú´Ù.
Arithmetic Exception.
0À¸·Î ³ª´¶§ ¹ß»ýµÈ´Ù. ±×¹ÛÀÇ °æ¿ì´Â ¾ø´Ù. ArithmeticExceptionÀÌ ½ÇÇàµÇ´Â
°æ¿ì´Â ´ÙÀ½ ¿¹¿Í °°´Ù.
class Arith {
public static void main(String args[]) {
int j = 0;
j = j / j;
}
}
NullPointerException
VariableÀ̳ª method°¡ null object¸¦ ÂüÁ¶Çϰųª, ¹è¿ÀÇ null element¸¦
Á¢±Ù ÇÒ°æ¿ì¿¡ NullPointerExceptionÀÌ ½ÇÇàµÈ´Ù. ¿¹¸¦ µé¾î¼ o.length¿Í a[0]À»
Á¢±ÙÇÔÀ¸·Î ½ÇÇ൵Áß¿¡ NullPointerExceptionÀÌ ½ÇÇàµÈ´Ù.
class Null {
public static void main(String args[]) {
String o = null;
int a[] = null;
o.length();
a[0] = 0;
}
}
IncompatibleClassChangeException
IncompatibleClassChageExceptionÀÌ ½ÇÇàµÇ´Â °æ¿ì´Â ´ÙÀ½°ú °°´Ù.
- class ³»ºÎÀÇ variableÀÇ ¼±¾ðÀÌ static¿¡¼ non-staticÀ¸·Î ¹Ù²î¾ú´Âµ¥,
´Ù¸¥ class°¡ ÀÌ ¹Ù²ï variableÀ» ÂüÁ¶ÇÒ¶§.
- class ³»ºÎÀÇ variableÀÇ ¼±¾ðÀÌ non-static¿¡¼ staticÀ¸·Î ¹Ù²î¾ú´Âµ¥,
´Ù¸¥ class°¡ ÀÌ ¹Ù²ï variableÀ» ÂüÁ¶ÇÒ¶§.
- class ³»ºÎÀÇ field°¡ Áö¿öÁ³´Âµ¥, ´Ù¸¥ class°¡ ÀÌ field¸¦ ÂüÁ¶ÇÒ¶§.
- class ³»ºÎÀÇ method°¡ Áö¿öÁ³´Âµ¥, ´Ù¸¥ class°¡ ÀÌ field¸¦ ÂüÁ¶ÇÒ¶§.
ClassCastException
object O°¡ class C·Î Çüº¯È¯ ÇÒ¶§, O´Â C°¡ ¾Æ´Ï°í CÀÇ subclassµµ ¾Æ´Ï¹Ç·Î
ClassCastExceptionÀÌ ½ÇÇà µÈ´Ù. ´ÙÀ½Àº ±× ¿¹ÀÌ´Ù.
class ClassCast {
public static void main(String args[]) {
Object o = new Object();
String s = (String)o; // the cast attempt
s.length();
}
}
NegativeArraySizeException
¹è¿ÀÇ Å©±â°¡ À½¼öÀ̸é NegativeArraySizeExceptionÀÌ ½ÇÇàµÈ´Ù.
´ÙÀ½Àº ±× ¿¹ÀÌ´Ù.
class NegArray {
public static void main(String args[]) {
int a[] = new int[-1];
a[0] = 0;
}
}
OutOfMemoryException
´õÀÌ»ó ¸Þ¸ð¸® ÇÒ´çÀ» ÇÒ¼ö ¾øÀ»¶§ OutOfMemorryExceptinÀÌ ½ÇÇàµÈ´Ù. ÀÌ°ÍÀº
¿ÀÁ÷ new ¿¬»êÀÚ¸¦ ÀÌ¿ëÇÏ¿© object¸¦ »ý¼ºÇÒ¶§¸¸ ¹ß»ýµÈ´Ù. ´ÙÀ½Àº ±× ¿¹ÀÌ´Ù.
class Link {
int a[] = new int[1000000];
Link l;
}
class OutOfMem {
public static void main(String args[]) {
Link root = new Link();
Link cur = root;
while(true) {
cur.l = new Link();
cur = cur.l;
}
}
}
NoClassDefFoundException
Class¸¦ ÂüÁ¶ÇÒ·Á°í Çϳª, runtime systemÀÌ ÂüÁ¶ÇÒ class¸¦ ãÁö ¸øÇÒ¶§
½ÇÇà µÈ´Ù. ´ÙÀ½Àº ±× ¿¹ÀÌ´Ù.
class NoClass {
public static void main(String args[]) {
C c = new C();
}
}
À§ÀÇ ¿¹¿¡¼ runtime systemÀº C.class¸¦ ãÁö ¸øÇß´Ù. C.class´Â NoClass°¡
compileµÉ¶§ ÀÌ¹Ì Á¸ÀçÇØ¾ß ÇÑ´Ù.
IncompatibleTypeException
InterfaceÀÇ instance¸¦ »ý¼ºÇÒ¶§ ¹ß»ýÇÑ´Ù. ´ÙÀ½Àº ±× ¿¹ÀÌ´Ù.
interface I {
}
class IncompType {
public static void main(String args[]) {
I r = (I)new("I");
}
}
ArrayIndexOutOfBoundsException
¹è¿ÀÇ À߸øµÈ element¸¦ indexÇÒ °æ¿ì ½ÇÇàµÈ´Ù. ´ÙÀ½Àº ±× ¿¹ÀÌ´Ù.
class ArrayOut {
public static void main(String args[]) {
int a[] = new int[0];
a[0] = 0;
}
}
UnsatisfiedLinkException
Method°¡ native·Î ¼±¾ðµÇ¾ú°í, method°¡ runtime system routine°ú ´õÀÌ»ó
¿¬°áµÇÁö ¾ÊÀ»¶§ ¹ß»ýÇÑ´Ù.
class NoLink {
static native void foo();
public static void main(String args[]) {
foo();
}
}
InternalException
runtime systemÀÇ À̸§¸ð¸¦ ¿À·ù°¡ ¹ß»ýÇÒ¶§ ½ÇÇàµÈ´Ù.
ÀÌ·¯ÇÑ °æ¿ì°¡ ¹ß»ýÇϸé WebRunner@Sun.COMÀ¸·Î ¿¬¶ôÇغ»´Ù.