Java °æ (¾«»ªÇø)
·¢ÐÅÈË: wazy (·Ø), ÐÅÇø: Java
±ê Ìâ: JAVAÓïÑԹ淶£¨4£©
·¢ÐÅÕ¾: ×϶¡Ïã (Tue Sep 16 16:16:00 1997)
7. ±í´ïʽ
JavaÓïÑԵıí´ïʽºÍCÓïÑԷdz£ÀàËÆ¡£
7.1 ÔËËã·û
ÔËËã·û(operator)ÓÅÏȼ¶´Ó¸ßµ½µ×ÅÅÁÐÈçÏ£º
.. [ ] ()
++ -- ! ¡« instanceof
* / %
+ -
<< >> >>>
< > <= >\
== ! =
&
^
&&
||
? :
= op =
,
7.1.1 ÕûÊýÉϵÄÔËËã·û
ÔÚÕûÊýÔËËãʱ£¬Èç¹û²Ù×÷ÊýÊÇlongÀàÐÍ£¬ÔòÔËËã½á¹ûÊÇlongÀàÐÍ£¬
·ñÔòΪintÀàÐÍ£¬¾ø²»»áÊÇbyte£¬short»òcharÐÍ¡£ÕâÑù£¬Èç¹û±äÁ¿i±»
ÉùÃ÷Ϊshort»òbyte£¬i+1»áÊÇint¡£Èç¹û½á¹û³¬¹ý¸ÃÀàÐ͵ÄÈ¡Öµ·¶Î§£¬
Ôò°´¸ÃÀàÐ͵Ä×î´óֵȡģ¡£µ¥Ä¿ÕûÊýÔËËã·ûÊÇ£º
ÔËËã·û ²Ù×÷
- µ¥Ä¿·Ç
¡« λ²¹Âë
++ ¼Ó1
-- ¼õ1
++ÔËËã·ûÓÃÓÚ±íʾֱ½Ó¼Ó1²Ù×÷¡£ÔöÁ¿²Ù×÷Ò²¿ÉÒÔÓüÓÔËËã·ûºÍ¸³
Öµ²Ù×÷¼ä½ÓÍê³É¡£++ lvalue£¨×óÖµ£©±íʾlvalue+=1, ++lvalue Ò²±íʾ
lvalue =lvalue +1 (Ö»ÒªlvalueûÓи±×÷ÓÃ)¡£--ÔËËã·ûÓÃÓÚ±íʾ¼õ1²Ù×÷¡£
++ºÍ--ÔËËã·û¼È¿ÉÒÔ×÷Ϊǰ׺ÔËËã·û£¬Ò²¿ÉÒÔ×öΪºó׺ÔËËã·û¡£Ë«Ä¿Õû
ÊýÔËËã·ûÊÇ£º
ÔËËã·û ²Ù×÷**
+ ¼Ó
- ¼õ
* ³Ë
/ ³ý
% È¡Ä£
& λÓë
£ü λ»ò
^ λÒì»ò
<< ×óÒÆ
>> ÓÒÒÆ(´ø·ûºÅ)
>>> ÌíÁãÓÒÒÆ
** integer op integer=>integer
ÕûÊý³ý·¨°´ÁãÉáÈë¡£³ý·¨ºÍÈ¡Ä£×ñÊØÒÔϵÈʽ£º
( a/b ) * b + ( a%b ) == a
ÕûÊýËãÊõÔËËãµÄÒì³£ÊÇÓÉÓÚ³ýÁã»ò°´ÁãÈ¡Ä£Ôì³ÉµÄ¡£Ëü½«Òý·¢Ò»¸ö
ArithmeticExceptionËãÊõÒì³£¡£ÏÂÒç²úÉúÁ㣬ÉÏÒçµ¼ÖÂÔ½½ç¡£ÀýÈ磺 ¼Ó1
³¬¹ýÕûÊý×î´óÖµ£¬È¡Ä£ºó£¬±ä³É×îСֵ¡£
Ò»¸öop=¸³ÖµÔËËã·û£¬ºÍÉϱíÖеĸ÷Ë«Ä¿ÕûÊýÔËËã·ûÁªÓ㬹¹³ÉÒ»
¸ö±í´ïʽ¡£
ÕûÊý¹ØϵÔËËã·û<£¬ >£¬<=£¬>=£¬==ºÍ!=²úÉúbooleanÀàÐ͵ÄÊý¾Ý¡£
7.1.2 ²¼¶ûÔËËã·û
²¼¶û(boolean)±äÁ¿»ò±í´ïʽµÄ×éºÏÔËËã¿ÉÒÔ²úÉúеÄbooleanÖµ¡£µ¥
Ä¿ÔËËã·û! ÊDz¼¶û·Ç¡£Ë«Ä¿ÔËËã·û£¦£¬£üºÍ^ÊÇÂß¼AND£¬ORºÍXOR
ÔËËã·û£¬ËüÃÇÇ¿ÖÆÁ½¸ö²Ù×÷ÊýÇ󲼶ûÖµ¡£Îª±ÜÃâÓÒ²à²Ù×÷ÊýÈßÓàÇóÖµ£¬
Óû§¿ÉÒÔʹÓöÌ·ÇóÖµÔËËã·û£¦£¦ºÍ£ü£ü¡£Óû§¿ÉÒÔʹÓÃ==ºÍ!=£¬¸³Öµ
ÔËËã·ûÒ²¿ÉÒÔÓà &=¡¢|=¡¢^=¡£ÈýÔªÌõ¼þ²Ù×÷·û? : ºÍCÓïÑÔÖеÄÒ»Ñù¡£
7.1.3 ¸¡µãÔËËã·û
¸¡µãÔËËã·û¿ÉÒÔʹÓó£¹æÔËËã·ûµÄ×éºÏ£º È絥ĿµÄÔËËã·û++¡¢--£¬
Ë«Ä¿µÄÔËËã·û+¡¢-¡¢* ºÍ/£¬ÒÔ¼°¸³ÖµÔËËã·û+=£¬-=£¬*=£¬ºÍ/=¡£´ËÍ⣬
»¹ÓÐÈ¡Ä£ÔËË㣺 %ºÍ%=Ò²¿ÉÒÔ×÷ÓÃÓÚ¸¡µãÊý£¬ÀýÈ磺
a%b
ºÍa-((int) (a/b)*b)µÄÓïÒåÏàͬ¡£Õâ±íʾa%bµÄ½á¹ûÊdzýÍêºóʣϵÄ
¸¡µãÊý²¿·Ö¡£Ö»Óе¥¾«¶È²Ù×÷ÊýµÄ¸¡µã±í´ïʽ°´ÕÕµ¥¾«¶ÈÔËËãÇóÖµ£¬²ú
Éúµ¥¾«¶È½á¹û¡£Èç¹û¸¡µã±í´ïʽÖк¬ÓÐÒ»¸ö»òÒ»¸öÒÔÉϵÄË«¾«¶È²Ù×÷Êý£¬
Ôò°´Ë«¾«¶ÈÔËË㣬½á¹ûÊÇË«¾«¶È¸¡µãÊý¡£
JavaÏÖÔÚÉÐÎÞ¸¡µãËãÊõÔËËãµÄÒì³£´¦Àí¡£°´ÕÕIEEE754¸¡µã¹æ¸ñ˵
Ã÷£¬Á½¸öÌØÊâµÄÖµ InfºÍNa NÊÇ¿ÉÌ滻ʹÓõģ¬ÉÏÒç²úÉúInf£¬ÏÂÒç²ú
Éú0£¬³ýÁã²úÉúInf¡£
¿ÉÒÔʹÓùØϵÔËËã·û£¬²úÉúbooleanÖµ£º >£¬< , >=£¬<=£¬==£¬!=¡£
°´NaNµÄÐÔÖÊ£¬¸¡µãÖµ²»ÊÇÍêÈ«ÓÐÐòµÄ£¬ËùÒÔÔÚ¸¡µãÊý±È½ÏʱҪСÐÄ¡£
ÀýÈ磺 Èç¹ûa<b·ÇÕ棬Ôò²¢²»Òâζ×Åa>=b; ÔÙÀýÈ磬a!=b£¬²¢²»±íʾ
a>b | a<b¡£Êµ¼ÊÉÏ£¬¿ÉÄܾÍûÓдóС֮·Ö¡£
¸¡µãËãÊõÔËËãºÍÊý¾Ý¸ñʽ¶¨Òå°´IEEE754µÄ¡°¸¡µãËãÊõÔËËã±ê×¼¡±¡£
²Î¼û¸½Â¼ÖеĹØÓÚJava¸¡µãÔËËãʵÏÖµÄϸ½Ú¡£
7.1.4 Êý×éÔËËã·û
Êý×éÔËËã·ûÐÎʽÈçÏ£º
<expression> [ <expression>]
¿É¸ø³öÊý×éÖÐij¸öÔªËصÄÖµ¡£ºÏ·¨µÄÈ¡Öµ·¶Î§ÊÇ´Ó0µ½Êý×éµÄ³¤¶È¼õ
1¡£È¡Öµ·¶Î§µÄ¼ì²éÖ»ÔÚÔËÐÐʱ¿Ìʵʩ¡£
7.1.5 ´®ÔËËã·û
´®ÒÔString¶ÔÏóʵÏÖ£¨¼û¡°´®×ÖÃæÁ¿¡±½Ú£©¡£ÔËËã·û"+"Íê³É²¢´®²Ù
×÷£¬Èç¹û±ØÒªÔò×Ô¶¯°Ñ²Ù×÷Êýת»»ÎªStringÐÍ¡£Èç¹û²Ù×÷ÊýÊÇÒ»¸ö¶ÔÏó£¬
Ëü¿É¶¨ÒåÒ»¸ö·½·¨toString ( ) ·µ»Ø¸Ã¶ÔÏóµÄString·½Ê½£¬ÀýÈç
float a = 1.0
print (¡°The value of a is¡±+ a +¡°\n¡±);
+ÔËËã·ûÓõ½´®ÉϵÄÀý×Ó
String s=¡°a=¡±+ a;
+=ÔËËã·ûÒ²¿ÉÒÔÓÃÓÚString¡£×¢Ò⣬×ó±ß(ÏÂÀýÖеÄs1)½öÇóÖµÒ»´Î¡£
s1+=a; //s1=s1+a
//Èôa·ÇStringÐÍ£¬×Ô¶¯×ª»»ÎªStringÐÍ¡£
7.1.6 ¶ÔÏóÔËËã·û
Ë«Ä¿ÔËËã·ûistanceof ²âÊÔij¸ö¶ÔÏóÊÇ·ñÊÇÖ¸¶¨Àà»òÆä×ÓÀàµÄʵÀý¡£
ÀýÈ磺
if (thermostat instanceof MeasuringDevice) {
MeasuringDevice dev=(MeasuringDevice) thermostat;
¡
}
ÊÇÅж¨thermostatÊÇ·ñÊÇMeasuringDeviceµÄʵÀý»òÊÇÆä×ÓÀàµÄʵÀý¡£
7.2 Ç¿ÖƺÍת»»
JavaÓïÑԺͽâÊÍÆ÷ÏÞÖÆʹÓÃÇ¿ÖƺÍת»»£¬ÒÔ·ÀÖ¹³ö´íµ¼ÖÂϵͳ±ÀÀ£¡£
ÕûÊýºÍ¸¡µãÊýÖ®¼ä¿ÉÒÔÀ´»ØÇ¿ÖÆת»»£¬µ«ÕûÊý²»ÄÜÇ¿ÖƳÉÊý×é»ò¶ÔÏó¡£
Object²»Äܱ»Ç¿ÖÆΪ»ù±¾ÀàÐÍ¡£Ò»¸öʵÀý¿ÉÒÔ±»Ç¿ÖƳɳ¬ÀàµÄÊÇ¿ÖƳÉ×ÓÀàµÄʵÀý»áµ¼ÖÂÔËÐÐʱ¿ÌµÄ¼ì²é¡£Èç¹ûÒ»¸ö±»Ç¿ÖƳÉ×ÓÀàʵÀý
µÄ¶ÔÏó²»ÊǸÃ×ÓÀàµÄʵÀý(»ò¸Ã×ÓÀàµÄ×ÓÀà¶ÔÏó)£¬½âÊÍÆ÷»á²úÉú
ClassCastException¡£
8. Óï¾ä
8.1 ÉùÃ÷
ÉùÃ÷¿ÉÒÔ³öÏÖÔÚËùÓÐÓï¾ä¿ÉÄܳöÏֵĵط½¡£ÉùÃ÷µÄ×÷ÓÃÓò½áÊøÓÚ·â
±Õ¸Ã¿éµÄ½áβ´¦¡£´ËÍ⣬¿ÉÒÔÔÚforÓï¾äµÄÍ·²¿×÷±äÁ¿ÉùÃ÷£¬ÀýÈ磺
for (int i=0; i<10; i++) {
¡
}
µ«ÒÔÕâÖÖ·½Ê½ÉùÃ÷µÄ±äÁ¿£¬½öÔÚforÓï¾äÑ»·ÌåÄÚÓÐЧ¡£ÀýÈ磬ÏÂÃæ
µÄÒ»¶Î´úÂëºÍÉÏÊö´úÂëµÈ
¼Û¡£
{
int i=0;
for (; i<10; i++) {
¡
}
}
8.2 ±í´ïʽ
±í´ïʽÊÇÓï¾ä£º
a=3£»
print (23);
foo.bar( );
8.3 ¿ØÖÆÁ÷
ÏÂÃæÊǶԿØÖÆÁ÷µÄ×ܽ᣺
if (boolean) statement
else statement
switch(e1) {
case e2: statement
default: statement
}
break [label]
continue [label]
return e1;
for ([e1]; [e2]; [e3]) statement
while (boolean) statement
do statement while (boolean);
label: statement
JavaÖ§³ÖÓбêºÅµÄÑ»·ºÍÓбêºÅµÄ¶Ï¿ª£¬ÀýÈ磺
outer£º // ÊDZêºÅ
for (int i=0; i<10; i++) {
for (int j=0; <10; j++) {
if (¡) {
break outer;
}
if (¡) {
}
}
}
ÔÚÑ»·ºÍ¶Ï¿ªÖÐʹÓñêºÅÒª×ñÊØÒÔÏÂ×¼Ôò£º
* ÈκÎÓï¾ä¿ÉÒÔÓÐÒ»¸ö±êºÅ¡£
* Èç¹ûÒ»¸öbreakÓï¾äÓÐÒ»¸ö±êºÅ£¬Õâ¸ö±êºÅ±ØÐëÔÚÒ»¸ö·â±ÕÓï¾ä
µÄÇ°¶Ë¡£
* Èç¹ûÒ»¸öcontinueÓï¾äÓÐÒ»¸ö±êºÅ£¬ÔòËü±ØÐëÊÇÒ»¸ö·â±ÕÑ»·µÄ
±êºÅ¡£
8.4 Òì³£
µ±ÔÚJava³ÌÐòÖз¢ÉúÒ»¸ö´íÎóʱ£¬ÀýÈ磺һ¸ö±äÔªµÄÖµ·Ç·¨£¬´úÂë
»á·¢ÏÖÕâ¸ö´íÎ󣬲¢Òý·¢Ò»¸öÒì³£(exception)¡£ÔÚȱʡµÄÇé¿öÏ£¬Òì³£»á
Êä³öÒ»¸ö´íÎóÏûÏ¢£¬È»ºóÖÐÖ¹Ï̵߳ÄÖ´ÐС£µ«ÊÇ£¬³ÌÐò×Ô¼º¿ÉÒÔ¶¨ÒåÒì
³£´¦Àí¶Î(exception handler)À´½Ø»ñ(catch)Òì³££¬²¢´Ó´íÎóÖлָ´¡£
ÓÐһЩÒì³£ÊÇÓÉJava½âÊÍÆ÷ÔÚÔËÐÐʱ¿ÌÒý·¢µÄ¡£Êµ¼ÊÉÏ£¬ÈκÎÀ඼
¿ÉÒÔ¶¨ÒåÊôÓÚ×Ô¼ºµÄÒì³££¬
²¢Ê¹ÓÃthrowÓï¾äÒý·¢ËüÃÇ¡£
Ò»¸öthrow£¨Òý·¢£©Óï¾äÊÇÓÉthrow¹Ø¼ü×ÖºÍÒ»¸ö¶ÔÏ󹹳ɡ£°´³£¹æ£¬
¸Ã¶ÔÏóÓ¦¸ÃÊÇExceptionÀàµÄʵÀý»òÆä×ÓÀàµÄʵÀý¡£throwÓï¾ä»áÒýÆðÖ´
ÐÐתÏòÏàÓ¦µÄÒì³£´¦Àí¶Î¡£µ±Ò»¸öthrowÓï¾äÖ´ÐÐʱ£¬ËüÏÂÃæµÄËùÓдú
Âë²»ÔÙÖ´ÐÐÁË£¬ËüËùÔڵķ½·¨Ò²²»ÔÙ·µ»ØÖµ¡£ÏÂÃæµÄÀý×Ó½«ÑÝʾÈçºÎ´´
½¨Ò»¸öExceptionµÄ×ÓÀ࣬ȻºóÒý·¢Ò»¸öÒì³£¡£
class MyException extends Exception {
}
class MyClass {
void oops() {
if ( /* ²»³öÏÖ´íÎó */) {
¡
} else { /* ³ö´í */
throw new MyException( );
}
}
}
ΪÁ˶¨ÒåÒ»¸öÒì³£´¦Àí¶Î£¬³ÌÐò±ØÐëÓÃtryÓï¾ä°Ñ¿ÉÄܲúÉúÒì³£µÄ´ú
Âë³É×é¡£ÔÚtryÓï¾äºóÃæ¸úÉÏÒ»¸ö»ò¶à¸öcatch(½Ø»ñ£©Óï¾ä£¬Ã¿¸öÒì³£¶Ô
Ó¦Ò»¸öcatchÓï¾ä¡£Ã¿¸öcatchÓï¾äÖаüº¬×ÅÒì³£´¦Àí
¶Î¡£ÀýÈ磺
try {
p.a=10;
} catch ( NullPointerException e) {
println(¡°p was null¡±);
} catch ( Exception e) {
println (¡°other errors occured¡±);
} catch ( Object obj) {
println(¡°Who threw that object?¡±);
}
catchÓï¾äºÍÒ»¸ö·½·¨¶¨ÒåÀàËÆ£¬Ö»²»¹ý¸Ã·½·¨Ö»ÓÐÒ»¸ö²ÎÊý£¬ÇÒÎÞ
·µ»ØÀàÐÍ¡£²ÎÊý¿ÉÒÔÊÇÒ»¸öÀà»òÒ»¸ö½çÃæ¡£µ±Ò»¸öÒì³£·¢Éúʱ£¬Ç¶Ì×µÄ
try/catchÓï¾ä»áÑ°ÕÒ³öÓë¸ÃÒì³£ÀàÏàÆ¥ÅäµÄ²ÎÊý¡£Èç¹ûÒ»¸ö²ÎÊýºÍÖ¸¶¨
Ò쳣ƥÅäÔò£º
* ¸Ã²ÎÊýºÍÖ¸¶¨µÄÒì³£ÊÇͬһ¸öÀ࣬»ò
* ¸Ã²ÎÊýÊÇÖ¸¶¨Òì³£µÄ×ÓÀ࣬»ò
* Èç¹û²ÎÊýÊÇÒ»¸ö½çÃ棬ָ¶¨Òì³£ÀàʵÏÖÁËÕâ¸ö½çÃæ¡£
µÚÒ»¸ö²ÎÊýºÍÒ쳣ƥÅäµÄtry/catchÓï¾ä£¬ÔòÓëÆäÆ¥ÅäµÄcatchÓï¾äÖ´
ÐС£ÔÚcatchÓï¾äÖ´ÐÐÍêºó£¬³ÌÐòµÄÖ´Ðб»»Ö¸´¡£µ«ÒѲ»¿ÉÄָܻ´µ½Òì³£
·¢Éú´¦ÔÙ´ÎÖ´ÐС£ÀýÈ磺
print ( "now");
try {
print ("is");
throw new MyException( );
print ("a");
} catch (MyException e) {
print ("the ");
}
print ("time\n");
´òӡΪ¡°now is the time¡±¡£ÕýÈçÕâ¸öÀý×ÓËùʾ£¬Òì³£Ó¦¸ÃÖ÷ÒªÓÃÓÚ
´íÎó´¦Àí£¬ÈôÓÃÓÚÆäËü·½Ãæ»áʹ´úÂë»ÞɬÄѶ®¡£
Òì³£´¦Àí¶ÎÊÇ¿ÉÒÔǶÌ׵ģ¬ÔÊÐíÒì³£´¦Àí¿ÉÒÔ·¢ÉúÔÚ¶à¸öµØ·½¡£Ç¶
Ì×Òì³£´¦Àíͨ³£ÓÃÓÚµ±µÚÒ»¸ö´¦Àí³ÌÐòÎÞ·¨ÍêÈ«´Ó´íÎóÖлָ´¹ýÀ´µÄʱ
ºò£¬¶ø²»µÃ²»Ö´ÐÐһЩÇå³ý´úÂ롣ΪÁË°ÑÒì³£´¦Àí¿ØÖÆ´«µÝ¸ø¸ü¸ß²ãµÄ
´¦Àí¶Î£¬¿ÉÒÔÔÙÒ»´Î¶Ô½Ø»ñ¶ÔÏóʵʩthrow²Ù×÷¡£×¢ÒªÔÙ´ÎʵʩthrowÒì
³£µÄ·½·¨£¬
throwÓï¾äÖ´ÐÐÍêºó£¬»áÖÕÖ¹Ö´ÐС£
try {
f. open ( );
} catch(Exception e) {
f. close( );
throw e;
}
8.4.1 ¶¨¾ÖÓï¾ä
finally£¨¶¨¾Ö£©Óï¾äÊÇÓÃÓÚ±£Ö¤ÎÞÂÛÔÚÒì³£ÊÇ·ñ·¢ÉúµÄÇé¿öÏ£¬Ä³
Щ´úÂë±»Ö´ÐС£ÏÂÀý˵Ã÷finallyÓï¾äµÄÓ÷¨£º
try {
//×öijЩ¶¯×÷£»
£ý finally {
//´ËºóÇå³ý£»
}
ºÍÒÔÏ´úÂëÀàËÆ
try {
//×öijЩ¶¯×÷
} catch (Object e) {
//´ËºóÇå³ý£»
throw e;
}
//´ËºóÇå³ý£»
¼´Ê¹try¿éÖаüº¬return£¬break£¬continue£¬throwÓï¾ä£¬finally
Óï¾äÒ²»á±»Ö´ÐС£ÀýÈ磺 ÏÂÃæµÄ´úÂë¡°finally¡±×ÜÊDZ»Êä³ö£¬¶ø¡°after
try¡±½öÔÚa!=10ʱ±»Êä³ö¡£
try {
if (a==10) {
return ;
}
} finally {
print ("finally\n");
£ý
print ("after try \n");
8.4.2 ÔËÐÐʱ¿ÌÒì³£
±¾½ÚÁгöµÄÇåµ¥ÊÇJava½âÊÍÆ÷Òý·¢µÄ¸÷ÖÖÒì³£¡£µ±ÔËÐÐʱ¿Ì·¢ÏÖ¸÷
ÖÖ´íÎó£¬ÓɽâÊÍÆ÷Òý·¢Òì³£¡£
ArithmeticException
Èç¹û³ÌÐòÊÔͼ³ý0£¬»òÓÃ0È¡Ä££¬»á²úÉúArithmeticException£¨Ëã
ÊõÒì³££©£¬ÆäËüËãÊõ²Ù×÷²»»á²úÉúÒì³£¡£ÓйØJavaÈçºÎ´¦ÀíÆäËüËãÊõ´í
ÎóµÄÐÅÏ¢£¬¼û¡°ÕûÊýÔËËã·û¡±ºÍ¡°¸¡µãÔËËã·û¡±Á½½Ú¡£
ÀýÈ磺 ÏÂÃæµÄ´úÂ뽫»áÒý·¢ArithmeticExceptionÒì³££º
class Arith {
public static void main (String args [ ] ) {
int j = 0;
j = j/j;
}
}
NullPointerException
µ±³ÌÐòÊÔͼ·ÃÎÊÒ»¸ö¿Õ¶ÔÏóÖеıäÁ¿»ò·½·¨£¬»òÒ»¸ö¿ÕÊý×éÖеÄÔª
ËØʱÔòÒý·¢NullPointerException£¨¿ÕÖ¸ÕëÒì³££©¡£ÀýÈ磬·ÃÎʳ¤¶ÈΪ0
µÄÊý×éa[0]¡£ÓÐÒÔÏÂÀàÉùÃ÷£¬ÔËÐÐʱ»áÒý·¢NullPointerExceptionÒì³££º
class Null {
public static void main(String args [ ]) {
String o = null;
int a [ ] = null;
o.length( );
a[0] = 0;
}
}
ÓÐȤµÄÊÇ£¬Èç¹ûÎÒÃÇÒý·¢Ò»¸ö¿Õ¶ÔÏó£¬Ò²»á²úÒ»
NullPointerExceptionÒì³£¡£
IncompatibleClassChangeException
µ±Ò»¸öÀàµÄ¶¨Òå±»¸Ä±ä£¬¶øÒýÓøÃÀàµÄÆäËüÀàûÓб»ÖØбàÒëʱ£¬
»á²úÉúÕâÒ»Òì³£¡£ÓÐËÄÖÖÀà¸ü¸Ä»áµ¼ÖÂÔËÐÐʱ¿ÌÒý·¢
IncompatibleClassChangExceptionÒì³£¡£
* Ò»¸öÀàÖеıäÁ¿ÉùÃ÷ÓÉstatic±ä³É·Çstatic£¬¶øÆäËü·ÃÎʸÃÀàÕâÒ»
±äÁ¿µÄÀàûÓб»ÖØбàÒë¡£
* Ò»¸öÀàÖеıäÁ¿ÉùÃ÷ÓÉ·Çstatic±ä³Éstatic£¬¶øÆäËü·ÃÎʸÃÀàÕâÒ»
±äÁ¿µÄÀàûÓб»ÖØбàÒë¡£
* ÀàÖÐÉùÃ÷µÄij¸öÓò±»É¾³ý£¬¶øÆäËü·ÃÎʸÃÓòµÄÀàûÓб»ÖØбàÒë¡£
* ÀàÖÐÉùÃ÷µÄij¸ö·½·¨±»É¾³ý£¬¶øÆäËü·ÃÎʸ÷½·¨µÄÀàûÓб»ÖØÐÂ
±àÒë¡£
ClassCastException
Èç¹ûÊÔͼ°Ñ¶ÔÏóoÇ¿ÖƳÉClass C£¬¶øo¼È²»ÊÇClass CµÄʵÀý£¬Ò²
²»ÊÇClass C×ÓÀàµÄʵÀý£¬Õâʱ±ã»á²úÉúClassCastException¡£
class ClassCast {
public static void main (String args [ ] ) {
Object o = new Object( );
String s = (string) o;
s.length( );
}
}
NagativeArraySizeException
Èç¹ûÒ»¸öÊý×éµÄ³¤¶ÈÊǸºÊý£¬Ôò»áÒý·¢NagativeArraySizeException
£¨Êý×鸺ϱ꣩Òì³£¡£ÀýÈçÏÂÃæÀඨÒåµÄ´úÂëÔÚÔËÐÐʱÒý·¢ÕâÒ»Òì³££º
class NegArray {
public static void main(String args [ ]) {
int a [ ] = new int [-1];
a[0] = 0;
}
}
OutOfMemoryException
µ±ÏµÍ³ÎÞ·¨ÔÙÏòÓ¦ÓóÌÐòÌṩÄÚ´æʱ£¬»áÒý·¢
OutOfMemoryException£¨ÄÚ´æÒç³ö£©Òì³£¡£ÕâÖÖÒì³£Ö»ÄܳöÏÖÔÚ´´½¨ÐÂ
¶ÔÏóµÄʱºò£¬¼´new±»µ÷ÓõÄʱºò¡£ÀýÈ磬ÏÂÃæÒ»¶Î´úÂëÔÚÔËÐÐʱ¿Ì»á
Òý·¢OutOfMemoryExceptionÒì³££º
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
Èç¹ûÒ»¸öÀà±»ÒýÓ㬵«ÔÚÔËÐÐʱ¿Ì£¬ÏµÍ³Ã»ÓÐÕÒµ½±»ÒýÓõÄÀ࣬Õâ
ʱ»áÒý·¢NoClassDefFoundException£¨Î´ÕÒµ½ÀඨÒ壩Òì³£¡£ÀýÈ磬
NoClassÀàµÄÉùÃ÷ÈçÏ£º
class NoClass {
public static void main(String args [ ]) {
C c = new C ( );
}
}
µ±NoClassÔËÐÐʱ£¬Èç¹û½âÊÍÆ÷ÕÒ²»µ½CÀ࣬Ôò»á²úÉú
NoClassDefFoundException¡£
×¢Ò⣬ÔÚNoClass±»±àÒëʱCÀàÒ»¶¨Òª´æÔÚ¡£
IncompatibleType Exception
Èç¹ûÊÔͼΪһ½çÃæ×÷ʵÀý£¬Ôò»áÒý·¢IncompatibleTypeException£¨Àà
ÐͲ»¼æÈÝ£©Òì³£¡£ÀýÈ磬
ÏÂÃæµÄ´úÂë»áÒý·¢Ò»¸öIncompatibleTypeException¡£
Interface I {
}
class IncompType {
public static void main(String args [ ]) {
I r = (I) new ("I");
}
}
ArrayIndexOutOfBoundsException
ÊÔͼ·ÃÎÊÊý×éÖеÄÒ»¸ö·Ç·¨ÔªËØʱ£¬»áÒý·¢
ArrayIndexOutOfBoundsException£¨Êý×éË÷ÒýÔ½½ç£©Òì³£¡£ÀýÈ磺
Class ArrayOut {
public static void main(String args [ ]) {
int a [ ]=new int[0];
a[0]=0;
}
}
UnsatisfiedLinkException
Èç¹ûÒ»¸ö·½·¨±»ÉùÃ÷Ϊ±¾»ú£¬µ«¸Ã·½·¨ÔÚÔËÐÐʱ¿ÌÈ´²»ÄÜÁ¬½Óµ½Ò»
¸öÀý³ÌÌåÉÏȥʱ£¬»á²úÉúUnsatisfiedLinkException£¨ÎÞ·¨Á¬½Ó£©Òì³£¡£
ÀýÈ磺
Class NoLink {
static native void foo( );
public static void main(String args [ ]) {
foo( );
}
}
InternalException
InternalException£¨ÄÚ²¿£©Òì³£ÊDz»Äܱ»Òý·¢µÄ¡£Ö»ÓÐÔÚÔËÐÐʧ°Ü×÷
Ò»ÖÂÐÔ¼ì²éʱ£¬²Å»áÒý·¢Õâ¸öÒì³£¡£
--
¡ù À´Ô´:£®×϶¡Ïã pclinux.hit.edu.cn£®[FROM: zzz.hit.edu.cn]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
Ò³ÃæÖ´ÐÐʱ¼ä£º208.691ºÁÃë