Java鍒濈骇闈㈣瘯棰�

1. Java鍩虹閮ㄥ垎

鍩虹閮ㄥ垎鐨勯『搴忥細鍩烘湰璇硶锛岀被鐩稿叧鐨勮娉曪紝鍐呴儴绫荤殑璇硶锛岀户鎵跨浉鍏崇殑璇硶锛屽紓甯哥殑璇硶锛岀嚎绋嬬殑璇硶锛岄泦鍚堢殑璇硶锛宨o鐨勮娉曪紝铏氭嫙鏈烘柟闈㈢殑璇硶銆�

1銆佷竴涓�".java"婧愭枃浠朵腑鏄惁鍙互鍖呮嫭澶氫釜绫伙紙涓嶆槸鍐呴儴绫伙級锛熸湁浠�涔堥檺鍒讹紵

鍙互鏈夊涓被锛屼絾鍙兘鏈変竴涓猵ublic鐨勭被锛屽苟涓攑ublic鐨勭被鍚嶅繀椤讳笌鏂囦欢鍚嶇浉涓�鑷淬��

2銆丣ava鏈夋病鏈塯oto?

java涓殑淇濈暀瀛楋紝鐜板湪娌℃湁鍦╦ava涓娇鐢ㄣ��

3銆佽璇�&鍜�&&鐨勫尯鍒��

&鍜�&&閮藉彲浠ョ敤浣滈�昏緫涓庣殑杩愮畻绗︼紝琛ㄧず閫昏緫涓庯紙and锛夛紝褰撹繍绠楃涓よ竟鐨勮〃杈惧紡鐨勭粨鏋滈兘涓簍rue鏃讹紝鏁翠釜杩愮畻缁撴灉鎵嶄负true锛屽惁鍒欙紝鍙鏈変竴鏂逛负false锛屽垯缁撴灉涓篺alse銆�

&&杩樺叿鏈夌煭璺殑鍔熻兘锛屽嵆濡傛灉绗竴涓〃杈惧紡涓篺alse锛屽垯涓嶅啀璁$畻绗簩涓〃杈惧紡锛屼緥濡傦紝瀵逛簬if(str != null&& !str.equals(鈥溾��))琛ㄨ揪寮忥紝褰搒tr涓簄ull鏃讹紝鍚庨潰鐨勮〃杈惧紡涓嶄細鎵ц锛屾墍浠ヤ笉浼氬嚭鐜癗ullPointerException濡傛灉灏�&&鏀逛负&锛屽垯浼氭姏鍑篘ullPointerException寮傚父銆侷f(x==33 &++y>0) y浼氬闀匡紝If(x==33 && ++y>0)涓嶄細澧為暱

&杩樺彲浠ョ敤浣滀綅杩愮畻绗︼紝褰�&鎿嶄綔绗︿袱杈圭殑琛ㄨ揪寮忎笉鏄痓oolean绫诲瀷鏃讹紝&琛ㄧず鎸変綅涓庢搷浣滐紝鎴戜滑閫氬父浣跨敤0x0f鏉ヤ笌涓�涓暣鏁拌繘琛�&杩愮畻锛屾潵鑾峰彇璇ユ暣鏁扮殑鏈�浣�4涓猙it浣嶏紝渚嬪锛�0x31 & 0x0f鐨勭粨鏋滀负0x01銆�

澶囨敞锛氳繖閬撻鍏堣涓よ�呯殑鍏卞悓鐐癸紝鍐嶈鍑�&&鍜�&鐨勭壒娈婁箣澶勶紝骞跺垪涓句竴浜涚粡鍏哥殑渚嬪瓙鏉ヨ〃鏄庤嚜宸辩悊瑙i�忓交娣卞叆銆佸疄闄呯粡楠屼赴瀵屻�� f

4銆佸湪JAVA涓浣曡烦鍑哄綋鍓嶇殑澶氶噸宓屽寰幆锛�

鍦↗ava涓紝瑕佹兂璺冲嚭澶氶噸寰幆锛屽彲浠ュ湪澶栭潰鐨勫惊鐜鍙ュ墠瀹氫箟涓�涓爣鍙凤紝鐒跺悗鍦ㄩ噷灞傚惊鐜綋鐨勪唬鐮佷腑浣跨敤甯︽湁鏍囧彿鐨刡reak璇彞锛屽嵆鍙烦鍑哄灞傚惊鐜�備緥濡傦紝

ok:

for(int i=0;i<10;i++) 聽 聽 {

for(int j=0;j<10;j++) 聽 聽 聽 聽 聽 聽{

System.out.println(鈥渋=鈥� + i + 鈥�,j=鈥� + j);

if(j == 5) break ok;

}

}

鍙﹀锛屾垜涓汉閫氬父骞朵笉浣跨敤鏍囧彿杩欑鏂瑰紡锛岃�屾槸璁╁灞傜殑寰幆鏉′欢琛ㄨ揪寮忕殑缁撴灉鍙互鍙楀埌閲屽眰寰幆浣撲唬鐮佺殑鎺у埗锛屼緥濡傦紝瑕佸湪浜岀淮鏁扮粍涓煡鎵惧埌鏌愪釜鏁板瓧銆�

int arr[][] ={{1,2,3},{4,5,6,7},{9}};

boolean found = false;

for(int i=0;i

for(int j=0;j

System.out.println(鈥渋=鈥� + i + 鈥�,j=鈥� + j);

if(arr[i][j] 聽==5) {

found = true;

break;

}

}

}

5銆乻witch璇彞鑳藉惁浣滅敤鍦╞yte涓婏紝鑳藉惁浣滅敤鍦╨ong涓婏紝鑳藉惁浣滅敤鍦⊿tring涓�?

鍦╯witch锛坋xpr1锛変腑锛宔xpr1鍙兘鏄竴涓暣鏁拌〃杈惧紡鎴栬�呮灇涓惧父閲忥紙鏇村ぇ瀛椾綋锛夛紝鏁存暟琛ㄨ揪寮忓彲浠ユ槸int鍩烘湰绫诲瀷鎴朓nteger鍖呰绫诲瀷锛岀敱浜庯紝byte,short,char閮藉彲浠ラ殣鍚浆鎹负int锛屾墍浠ワ紝杩欎簺绫诲瀷浠ュ強杩欎簺绫诲瀷鐨勫寘瑁呯被鍨嬩篃鏄彲浠ョ殑銆傛樉鐒讹紝long鍜孲tring绫诲瀷閮戒笉绗﹀悎switch鐨勮娉曡瀹氾紝骞朵笖涓嶈兘琚殣寮忚浆鎹㈡垚int绫诲瀷锛屾墍浠ワ紝瀹冧滑涓嶈兘浣滅敤浜巗wtich璇彞涓��

6銆乻hort s1 = 1; s1 = s1 + 1;鏈変粈涔堥敊? short s1 = 1; s1 += 1;鏈変粈涔堥敊?

瀵逛簬short s1 = 1; s1 = s1 + 1;鐢变簬s1+1杩愮畻鏃朵細鑷姩鎻愬崌琛ㄨ揪寮忕殑绫诲瀷锛屾墍浠ョ粨鏋滄槸int鍨嬶紝鍐嶈祴鍊肩粰short绫诲瀷s1鏃讹紝缂栬瘧鍣ㄥ皢鎶ュ憡闇�瑕佸己鍒惰浆鎹㈢被鍨嬬殑閿欒銆�

瀵逛簬short s1 = 1; s1 += 1;鐢变簬 +=鏄痡ava璇█瑙勫畾鐨勮繍绠楃锛宩ava缂栬瘧鍣ㄤ細瀵瑰畠杩涜鐗规畩澶勭悊锛屽洜姝ゅ彲浠ユ纭紪璇戙��

7銆乧har鍨嬪彉閲忎腑鑳戒笉鑳藉瓨璐竴涓腑鏂囨眽瀛�?涓轰粈涔�?

char鍨嬪彉閲忔槸鐢ㄦ潵瀛樺偍Unicode缂栫爜鐨勫瓧绗︾殑锛寀nicode缂栫爜瀛楃闆嗕腑鍖呭惈浜嗘眽瀛楋紝鎵�浠ワ紝char鍨嬪彉閲忎腑褰撶劧鍙互瀛樺偍姹夊瓧鍟︺�備笉杩囷紝濡傛灉鏌愪釜鐗规畩鐨勬眽瀛楁病鏈夎鍖呭惈鍦╱nicode缂栫爜瀛楃闆嗕腑锛岄偅涔堬紝杩欎釜char鍨嬪彉閲忎腑灏变笉鑳藉瓨鍌ㄨ繖涓壒娈婃眽瀛椼�傝ˉ鍏呰鏄庯細unicode缂栫爜鍗犵敤涓や釜瀛楄妭锛屾墍浠ワ紝char绫诲瀷鐨勫彉閲忎篃鏄崰鐢ㄤ袱涓瓧鑺傘��

澶囨敞锛氬悗闈竴閮ㄥ垎鍥炵瓟铏界劧涓嶆槸鍦ㄦ闈㈠洖绛旈鐩紝浣嗘槸锛屼负浜嗗睍鐜拌嚜宸辩殑瀛﹁瘑鍜岃〃鐜拌嚜宸卞闂鐞嗚В鐨勯�忓交娣卞叆锛屽彲浠ュ洖绛斾竴浜涚浉鍏崇殑鐭ヨ瘑锛屽仛鍒扮煡鏃犱笉瑷�锛岃█鏃犱笉灏姐��

8銆佺敤鏈�鏈夋晥鐜囩殑鏂规硶绠楀嚭2涔樹互8绛夋柤鍑�?

2 << 3锛�

鍥犱负灏嗕竴涓暟宸︾Щn浣嶏紝灏辩浉褰撲簬涔樹互浜�2鐨刵娆℃柟锛岄偅涔堬紝涓�涓暟涔樹互8鍙灏嗗叾宸︾Щ3浣嶅嵆鍙紝鑰屼綅杩愮畻cpu鐩存帴鏀寔鐨勶紝鏁堢巼鏈�楂橈紝鎵�浠ワ紝2涔樹互8绛夋柤鍑犵殑鏈�鏁堢巼鐨勬柟娉曟槸2 << 3銆�

9銆佽璁捐涓�涓竴鐧句嚎鐨勮绠楀櫒

棣栧厛瑕佹槑鐧借繖閬撻鐩殑鑰冩煡鐐规槸浠�涔堬紝涓�鏄ぇ瀹堕鍏堣瀵硅绠楁満鍘熺悊鐨勫簳灞傜粏鑺傝娓呮銆佽鐭ラ亾鍔犲噺娉曠殑浣嶈繍绠楀師鐞嗗拰鐭ラ亾璁$畻鏈轰腑鐨勭畻鏈繍绠椾細鍙戠敓瓒婄晫鐨勬儏鍐碉紝浜屾槸瑕佸叿澶囦竴瀹氱殑闈㈠悜瀵硅薄鐨勮璁℃�濇兂銆�

棣栧厛锛岃绠楁満涓敤鍥哄畾鏁伴噺鐨勫嚑涓瓧鑺傛潵瀛樺偍鐨勬暟鍊硷紝鎵�浠ヨ绠楁満涓兘澶熻〃绀虹殑鏁板�兼槸鏈変竴瀹氱殑鑼冨洿鐨勶紝涓轰簡渚夸簬璁茶В鍜岀悊瑙o紝鎴戜滑鍏堜互byte绫诲瀷鐨勬暣鏁颁负渚嬶紝瀹冪敤1涓瓧鑺傝繘琛屽瓨鍌紝琛ㄧず鐨勬渶澶ф暟鍊艰寖鍥翠负-128鍒�+127銆�-1鍦ㄥ唴瀛樹腑瀵瑰簲鐨勪簩杩涘埗鏁版嵁涓�11111111锛屽鏋滀袱涓�-1鐩稿姞锛屼笉鑰冭檻Java杩愮畻鏃剁殑绫诲瀷鎻愬崌锛岃繍绠楀悗浼氫骇鐢熻繘浣嶏紝浜岃繘鍒剁粨鏋滀负1,11111110锛岀敱浜庤繘浣嶅悗瓒呰繃浜哹yte绫诲瀷鐨勫瓨鍌ㄧ┖闂达紝鎵�浠ヨ繘浣嶉儴鍒嗚鑸嶅純锛屽嵆鏈�缁堢殑缁撴灉涓�11111110锛屼篃灏辨槸-2锛岃繖姝eソ鍒╃敤婧綅鐨勬柟寮忓疄鐜颁簡璐熸暟鐨勮繍绠椼��-128鍦ㄥ唴瀛樹腑瀵瑰簲鐨勪簩杩涘埗鏁版嵁涓�10000000锛屽鏋滀袱涓�-128鐩稿姞锛屼笉鑰冭檻Java杩愮畻鏃剁殑绫诲瀷鎻愬崌锛岃繍绠楀悗浼氫骇鐢熻繘浣嶏紝浜岃繘鍒剁粨鏋滀负1,00000000锛岀敱浜庤繘浣嶅悗瓒呰繃浜哹yte绫诲瀷鐨勫瓨鍌ㄧ┖闂达紝鎵�浠ヨ繘浣嶉儴鍒嗚鑸嶅純锛屽嵆鏈�缁堢殑缁撴灉涓�00000000锛屼篃灏辨槸0锛岃繖鏍风殑缁撴灉鏄剧劧涓嶆槸鎴戜滑鏈熸湜鐨勶紝杩欒鏄�璁$畻鏈轰腑鐨勭畻鏈繍绠楁槸浼氬彂鐢熻秺鐣屾儏鍐电殑锛屼袱涓暟鍊肩殑杩愮畻缁撴灉涓嶈兘瓒呰繃璁$畻鏈轰腑鐨勮绫诲瀷鐨勬暟鍊艰寖鍥淬��鐢变簬Java涓秹鍙婅〃杈惧紡杩愮畻鏃剁殑绫诲瀷鑷姩鎻愬崌锛屾垜浠棤娉曠敤byte绫诲瀷鏉ュ仛婕旂ず杩欑闂鍜岀幇璞$殑瀹為獙锛屽ぇ瀹跺彲浠ョ敤涓嬮潰涓�涓娇鐢ㄦ暣鏁板仛瀹為獙鐨勪緥瀛愮▼搴忎綋楠屼竴涓嬶細

int a = Integer.MAX_VALUE;

int b = Integer.MAX_VALUE;

int sum = a + b;

System.out.println(鈥渁=鈥�+a+鈥�,b=鈥�+b+鈥�,sum=鈥�+sum);

鍏堜笉鑰冭檻long绫诲瀷锛岀敱浜巌nt鐨勬鏁拌寖鍥翠负2鐨�31娆℃柟锛岃〃绀虹殑鏈�澶ф暟鍊肩害绛変簬2*1000*1000*1000锛屼篃灏辨槸20浜跨殑澶у皬锛屾墍浠ワ紝瑕佸疄鐜颁竴涓竴鐧句嚎鐨勮绠楀櫒锛屾垜浠緱鑷繁璁捐涓�涓被鍙互鐢ㄤ簬琛ㄧず寰堝ぇ鐨勬暣鏁帮紝骞朵笖鎻愪緵浜嗕笌鍙﹀涓�涓暣鏁拌繘琛屽姞鍑忎箻闄ょ殑鍔熻兘锛屽ぇ姒傚姛鑳藉涓嬶細

锛堬級杩欎釜绫诲唴閮ㄦ湁涓や釜鎴愬憳鍙橀噺锛屼竴涓〃绀虹鍙凤紝鍙︿竴涓敤瀛楄妭鏁扮粍琛ㄧず鏁板�肩殑浜岃繘鍒舵暟

锛堬級鏈変竴涓瀯閫犳柟娉曪紝鎶婁竴涓寘鍚湁澶氫綅鏁板�肩殑瀛楃涓茶浆鎹㈠埌鍐呴儴鐨勭鍙峰拰瀛楄妭鏁扮粍涓�

锛堬級鎻愪緵鍔犲噺涔橀櫎鐨勫姛鑳�

public class BigInteger{

int sign;

byte[] val;

public Biginteger(String val) {

sign = ;

val = ;

}

public BigInteger add(BigInteger other) 聽 聽 聽 {

}

public BigInteger subtract(BigInteger other) {

}

public BigInteger multiply(BigInteger other){

}

public BigInteger divide(BigInteger other){

}

}

澶囨敞锛氳鎯冲啓鍑鸿繖涓被鐨勫畬鏁翠唬鐮侊紝鏄潪甯稿鏉傜殑锛屽鏋滄湁鍏磋叮鐨勮瘽锛屽彲浠ュ弬鐪媕dk涓嚜甯︾殑java.math.BigInteger绫荤殑婧愮爜銆傞潰璇曠殑浜轰篃鐭ラ亾璋侀兘涓嶅彲鑳藉湪鐭椂闂村唴鍐欏嚭杩欎釜绫荤殑瀹屾暣浠g爜鐨勶紝浠栬鐨勬槸浣犳槸鍚︽湁杩欐柟闈㈢殑姒傚康鍜屾剰璇嗭紝浠栨渶閲嶈鐨勮繕鏄�冩煡浣犵殑鑳藉姏锛屾墍浠ワ紝浣犱笉瑕佸洜涓鸿嚜宸辨棤娉曞啓鍑哄畬鏁寸殑鏈�缁堢粨鏋滃氨鏀惧純绛旇繖閬撻锛屼綘瑕佸仛鐨勫氨鏄綘姣斿埆浜哄啓寰楀锛岃瘉鏄庝綘姣斿埆浜哄己锛屼綘鏈夎繖鏂归潰鐨勬�濇兂鎰忚瘑灏卞彲浠ヤ簡锛屾瘯绔熷埆浜哄彲鑳借繛棰樼洰鐨勬剰鎬濋兘鐪嬩笉鎳傦紝浠�涔堥兘娌″啓锛屼綘瑕佹暍浜庣瓟杩欓亾棰橈紝鍗充娇鍙瓟浜嗕竴閮ㄥ垎锛岄偅涔熶笌閭d簺浠�涔堥兘涓嶆噦鐨勪汉鍖哄埆鍑烘潵锛屾媺寮�浜嗚窛绂伙紝绠楁槸鐭瓙涓殑楂樹釜锛屾満浼氬綋鐒跺氨灞炰簬浣犱簡銆傚彟澶栵紝绛旀涓殑妗嗘灦浠g爜涔熷緢閲嶈锛屼綋鐜颁簡涓�浜涢潰鍚戝璞¤璁$殑鍔熷簳锛岀壒鍒槸鍏朵腑鐨勬柟娉曞懡鍚嶅緢涓撲笟锛岀敤鐨勮嫳鏂囧崟璇嶅緢绮惧噯锛岃繖涔熸槸鑳藉姏銆佺粡楠屻�佷笓涓氭�с�佽嫳璇按骞崇瓑澶氫釜鏂归潰鐨勪綋鐜帮紝浼氱粰浜虹暀涓嬪緢濂界殑鍗拌薄锛屽湪缂栫▼鑳藉姏鍜屽叾浠栨柟闈㈡潯浠跺樊涓嶅鐨勬儏鍐典笅锛岃嫳璇ソ闄や簡鍙互浣夸綘鑾峰緱鏇村鏈轰細澶栵紝钖按鍙互楂樺嚭涓�鍗冨厓銆�

10銆佷娇鐢╢inal鍏抽敭瀛椾慨楗颁竴涓彉閲忔椂锛屾槸寮曠敤涓嶈兘鍙橈紝杩樻槸寮曠敤鐨勫璞′笉鑳藉彉锛�

浣跨敤final鍏抽敭瀛椾慨楗颁竴涓彉閲忔椂锛屾槸鎸囧紩鐢ㄥ彉閲忎笉鑳藉彉锛屽紩鐢ㄥ彉閲忔墍鎸囧悜鐨勫璞′腑鐨勫唴瀹硅繕鏄彲浠ユ敼鍙樼殑銆備緥濡傦紝瀵逛簬濡備笅璇彞锛�

final StringBuffer a=new StringBuffer("immutable");

鎵ц濡備笅璇彞灏嗘姤鍛婄紪璇戞湡閿欒锛�

a=new StringBuffer("");

浣嗘槸锛屾墽琛屽涓嬭鍙ュ垯鍙互閫氳繃缂栬瘧锛�

a.append(" broken!");

鏈変汉鍦ㄥ畾涔夋柟娉曠殑鍙傛暟鏃讹紝鍙兘鎯抽噰鐢ㄥ涓嬪舰寮忔潵闃绘鏂规硶鍐呴儴淇敼浼犺繘鏉ョ殑鍙傛暟瀵硅薄锛�

public void method(final 聽StringBuffer param){

}

瀹為檯涓婏紝杩欐槸鍔炰笉鍒扮殑锛屽湪璇ユ柟娉曞唴閮ㄤ粛鐒跺彲浠ュ鍔犲涓嬩唬鐮佹潵淇敼鍙傛暟瀵硅薄锛�

param.append("a");

11銆�"=="鍜宔quals鏂规硶绌剁珶鏈変粈涔堝尯鍒紵

锛堝崟鐙妸涓�涓笢瑗胯娓呮锛岀劧鍚庡啀璇存竻妤氬彟涓�涓紝杩欐牱锛屽畠浠殑鍖哄埆鑷劧灏卞嚭鏉ヤ簡锛屾贩鍦ㄤ竴璧疯锛屽垯寰堥毦璇存竻妤氾級

==鎿嶄綔绗︿笓闂ㄧ敤鏉ユ瘮杈冧袱涓彉閲忕殑鍊兼槸鍚︾浉绛夛紝涔熷氨鏄敤浜庢瘮杈冨彉閲忔墍瀵瑰簲鐨勫唴瀛樹腑鎵�瀛樺偍鐨勬暟鍊兼槸鍚︾浉鍚岋紝瑕佹瘮杈冧袱涓熀鏈被鍨嬬殑鏁版嵁鎴栦袱涓紩鐢ㄥ彉閲忔槸鍚︾浉绛夛紝鍙兘鐢�==鎿嶄綔绗︺��

濡傛灉涓�涓彉閲忔寚鍚戠殑鏁版嵁鏄璞$被鍨嬬殑锛岄偅涔堬紝杩欐椂鍊欐秹鍙婁簡涓ゅ潡鍐呭瓨锛屽璞℃湰韬崰鐢ㄤ竴鍧楀唴瀛橈紙鍫嗗唴瀛橈級锛屽彉閲忎篃鍗犵敤涓�鍧楀唴瀛橈紝渚嬪Objet obj = new Object();鍙橀噺obj鏄竴涓唴瀛橈紝new Object()鏄彟涓�涓唴瀛橈紝姝ゆ椂锛屽彉閲弌bj鎵�瀵瑰簲鐨勫唴瀛樹腑瀛樺偍鐨勬暟鍊煎氨鏄璞″崰鐢ㄧ殑閭e潡鍐呭瓨鐨勯鍦板潃銆傚浜庢寚鍚戝璞$被鍨嬬殑鍙橀噺锛屽鏋滆姣旇緝涓や釜鍙橀噺鏄惁鎸囧悜鍚屼竴涓璞★紝鍗宠鐪嬭繖涓や釜鍙橀噺鎵�瀵瑰簲鐨勫唴瀛樹腑鐨勬暟鍊兼槸鍚︾浉绛夛紝杩欐椂鍊欏氨闇�瑕佺敤==鎿嶄綔绗﹁繘琛屾瘮杈冦��

equals鏂规硶鏄敤浜庢瘮杈冧袱涓嫭绔嬪璞$殑鍐呭鏄惁鐩稿悓锛屽氨濂芥瘮鍘绘瘮杈冧袱涓汉鐨勯暱鐩告槸鍚︾浉鍚岋紝瀹冩瘮杈冪殑涓や釜瀵硅薄鏄嫭绔嬬殑銆備緥濡傦紝瀵逛簬涓嬮潰鐨勪唬鐮侊細

String a=new String("foo");

String b=new String("foo");

涓ゆ潯new璇彞鍒涘缓浜嗕袱涓璞★紝鐒跺悗鐢╝/b杩欎袱涓彉閲忓垎鍒寚鍚戜簡鍏朵腑涓�涓璞★紝杩欐槸涓や釜涓嶅悓鐨勫璞★紝瀹冧滑鐨勯鍦板潃鏄笉鍚岀殑锛屽嵆a鍜宐涓瓨鍌ㄧ殑鏁板�兼槸涓嶇浉鍚岀殑锛屾墍浠ワ紝琛ㄨ揪寮廰==b灏嗚繑鍥瀎alse锛岃�岃繖涓や釜瀵硅薄涓殑鍐呭鏄浉鍚岀殑锛屾墍浠ワ紝琛ㄨ揪寮廰.equals(b)灏嗚繑鍥瀟rue銆�

鍦ㄥ疄闄呭紑鍙戜腑锛屾垜浠粡甯歌姣旇緝浼犻�掕繘琛屾潵鐨勫瓧绗︿覆鍐呭鏄惁绛夛紝渚嬪锛孲tring input = 鈥�;input.equals(鈥渜uit鈥�)锛岃澶氫汉绋嶄笉娉ㄦ剰灏变娇鐢�==杩涜姣旇緝浜嗭紝杩欐槸閿欒鐨勶紝闅忎究浠庣綉涓婃壘鍑犱釜椤圭洰瀹炴垬鐨勬暀瀛﹁棰戠湅鐪嬶紝閲岄潰灏辨湁澶ч噺杩欐牱鐨勯敊璇�傝浣忥紝瀛楃涓茬殑姣旇緝鍩烘湰涓婇兘鏄娇鐢╡quals鏂规硶銆�

濡傛灉涓�涓被娌℃湁鑷繁瀹氫箟equals鏂规硶锛岄偅涔堝畠灏嗙户鎵縊bject绫荤殑equals鏂规硶锛孫bject绫荤殑equals鏂规硶鐨勫疄鐜颁唬鐮佸涓嬶細

boolean equals(Object o){

return this==o;

}

杩欒鏄庯紝濡傛灉涓�涓被娌℃湁鑷繁瀹氫箟equals鏂规硶锛屽畠榛樿鐨別quals鏂规硶锛堜粠Object绫荤户鎵跨殑锛夊氨鏄娇鐢�==鎿嶄綔绗︼紝涔熸槸鍦ㄦ瘮杈冧袱涓彉閲忔寚鍚戠殑瀵硅薄鏄惁鏄悓涓�瀵硅薄锛岃繖鏃跺�欎娇鐢╡quals鍜屼娇鐢�==浼氬緱鍒板悓鏍风殑缁撴灉锛屽鏋滄瘮杈冪殑鏄袱涓嫭绔嬬殑瀵硅薄鍒欐�昏繑鍥瀎alse銆傚鏋滀綘缂栧啓鐨勭被甯屾湜鑳藉姣旇緝璇ョ被鍒涘缓鐨勪袱涓疄渚嬪璞$殑鍐呭鏄惁鐩稿悓锛岄偅涔堜綘蹇呴』瑕嗙洊equals鏂规硶锛岀敱浣犺嚜宸卞啓浠g爜鏉ュ喅瀹氬湪浠�涔堟儏鍐靛嵆鍙涓轰袱涓璞$殑鍐呭鏄浉鍚岀殑銆�

12銆侀潤鎬佸彉閲忓拰瀹炰緥鍙橀噺鐨勫尯鍒紵

鍦ㄨ娉曞畾涔変笂鐨勫尯鍒細闈欐�佸彉閲忓墠瑕佸姞static鍏抽敭瀛楋紝鑰屽疄渚嬪彉閲忓墠鍒欎笉鍔犮��

鍦ㄧ▼搴忚繍琛屾椂鐨勫尯鍒細瀹炰緥鍙橀噺灞炰簬鏌愪釜瀵硅薄鐨勫睘鎬э紝蹇呴』鍒涘缓浜嗗疄渚嬪璞★紝鍏朵腑鐨勫疄渚嬪彉閲忔墠浼氳鍒嗛厤绌洪棿锛屾墠鑳戒娇鐢ㄨ繖涓疄渚嬪彉閲忋�傞潤鎬佸彉閲忎笉灞炰簬鏌愪釜瀹炰緥瀵硅薄锛岃�屾槸灞炰簬绫伙紝鎵�浠ヤ篃绉颁负绫诲彉閲忥紝鍙绋嬪簭鍔犺浇浜嗙被鐨勫瓧鑺傜爜锛屼笉鐢ㄥ垱寤轰换浣曞疄渚嬪璞★紝闈欐�佸彉閲忓氨浼氳鍒嗛厤绌洪棿锛岄潤鎬佸彉閲忓氨鍙互琚娇鐢ㄤ簡銆傛�讳箣锛屽疄渚嬪彉閲忓繀椤诲垱寤哄璞″悗鎵嶅彲浠ラ�氳繃杩欎釜瀵硅薄鏉ヤ娇鐢紝闈欐�佸彉閲忓垯鍙互鐩存帴浣跨敤绫诲悕鏉ュ紩鐢ㄣ��

渚嬪锛屽浜庝笅闈㈢殑绋嬪簭锛屾棤璁哄垱寤哄灏戜釜瀹炰緥瀵硅薄锛屾案杩滈兘鍙垎閰嶄簡涓�涓猻taticVar鍙橀噺锛屽苟涓旀瘡鍒涘缓涓�涓疄渚嬪璞★紝杩欎釜staticVar灏变細鍔�1锛涗絾鏄紝姣忓垱寤轰竴涓疄渚嬪璞★紝灏变細鍒嗛厤涓�涓猧nstanceVar锛屽嵆鍙兘鍒嗛厤澶氫釜instanceVar锛屽苟涓旀瘡涓猧nstanceVar鐨勫�奸兘鍙嚜鍔犱簡1娆°��

public class VariantTest{

public static int staticVar = 0;

public int instanceVar = 0;

public VariantTest(){

staticVar++;

instanceVar++;

System.out.println(鈥渟taticVar=鈥� + staticVar + 鈥�,instanceVar=鈥�+ instanceVar);

}

}

澶囨敞锛氳繖涓В绛旈櫎浜嗚娓呮涓よ�呯殑鍖哄埆澶栵紝鏈�鍚庤繕鐢ㄤ竴涓叿浣撶殑搴旂敤渚嬪瓙鏉ヨ鏄庝袱鑰呯殑宸紓锛屼綋鐜颁簡鑷繁鏈夊緢濂界殑瑙h闂鍜岃璁℃渚嬬殑鑳藉姏锛屾�濈淮鏁忔嵎锛岃秴杩囦竴鑸▼搴忓憳锛屾湁鍐欎綔鑳藉姏锛�

13銆佹槸鍚﹀彲浠ヤ粠涓�涓猻tatic鏂规硶鍐呴儴鍙戝嚭瀵归潪static鏂规硶鐨勮皟鐢紵

涓嶅彲浠ャ�傚洜涓洪潪static鏂规硶鏄涓庡璞″叧鑱斿湪涓�璧风殑锛屽繀椤诲垱寤轰竴涓璞″悗锛屾墠鍙互鍦ㄨ瀵硅薄涓婅繘琛屾柟娉曡皟鐢紝鑰宻tatic鏂规硶璋冪敤鏃朵笉闇�瑕佸垱寤哄璞★紝鍙互鐩存帴璋冪敤銆備篃灏辨槸璇达紝褰撲竴涓猻tatic鏂规硶琚皟鐢ㄦ椂锛屽彲鑳借繕娌℃湁鍒涘缓浠讳綍瀹炰緥瀵硅薄锛屽鏋滀粠涓�涓猻tatic鏂规硶涓彂鍑哄闈瀞tatic鏂规硶鐨勮皟鐢紝閭d釜闈瀞tatic鏂规硶鏄叧鑱斿埌鍝釜瀵硅薄涓婄殑鍛紵杩欎釜閫昏緫鏃犳硶鎴愮珛锛屾墍浠ワ紝涓�涓猻tatic鏂规硶鍐呴儴鍙戝嚭瀵归潪static鏂规硶鐨勮皟鐢ㄣ��

14銆両nteger涓巌nt鐨勫尯鍒�

int鏄痡ava鎻愪緵鐨�8绉嶅師濮嬫暟鎹被鍨嬩箣涓�銆侸ava涓烘瘡涓師濮嬬被鍨嬫彁渚涗簡灏佽绫伙紝Integer鏄痡ava涓篿nt鎻愪緵鐨勫皝瑁呯被銆俰nt鐨勯粯璁ゅ�间负0锛岃�孖nteger鐨勯粯璁ゅ�间负null锛屽嵆Integer鍙互鍖哄垎鍑烘湭璧嬪�煎拰鍊间负0鐨勫尯鍒紝int鍒欐棤娉曡〃杈惧嚭鏈祴鍊肩殑鎯呭喌锛屼緥濡傦紝瑕佹兂琛ㄨ揪鍑烘病鏈夊弬鍔犺�冭瘯鍜岃�冭瘯鎴愮哗涓�0鐨勫尯鍒紝鍒欏彧鑳戒娇鐢↖nteger銆傚湪JSP寮�鍙戜腑锛孖nteger鐨勯粯璁や负null锛屾墍浠ョ敤el琛ㄨ揪寮忓湪鏂囨湰妗嗕腑鏄剧ず鏃讹紝鍊间负绌虹櫧瀛楃涓诧紝鑰宨nt榛樿鐨勯粯璁ゅ�间负0锛屾墍浠ョ敤el琛ㄨ揪寮忓湪鏂囨湰妗嗕腑鏄剧ず鏃讹紝缁撴灉涓�0锛屾墍浠ワ紝int涓嶉�傚悎浣滀负web灞傜殑琛ㄥ崟鏁版嵁鐨勭被鍨嬨��

鍦℉ibernate涓紝濡傛灉灏哋ID瀹氫箟涓篒nteger绫诲瀷锛岄偅涔圚ibernate灏卞彲浠ユ牴鎹叾鍊兼槸鍚︿负null鑰屽垽鏂竴涓璞℃槸鍚︽槸涓存椂鐨勶紝濡傛灉灏哋ID瀹氫箟涓轰簡int绫诲瀷锛岃繕闇�瑕佸湪hbm鏄犲皠鏂囦欢涓缃叾unsaved-value灞炴�т负0銆�

鍙﹀锛孖nteger鎻愪緵浜嗗涓笌鏁存暟鐩稿叧鐨勬搷浣滄柟娉曪紝渚嬪锛屽皢涓�涓瓧绗︿覆杞崲鎴愭暣鏁帮紝Integer涓繕瀹氫箟浜嗚〃绀烘暣鏁扮殑鏈�澶у�煎拰鏈�灏忓�肩殑甯搁噺銆�

15銆丮ath.round(11.5)绛夋柤澶氬皯? Math.round(-11.5)绛夋柤澶氬皯?

Math绫讳腑鎻愪緵浜嗕笁涓笌鍙栨暣鏈夊叧鐨勬柟娉曪細ceil銆乫loor銆乺ound锛岃繖浜涙柟娉曠殑浣滅敤涓庡畠浠殑鑻辨枃鍚嶇О鐨勫惈涔夌浉瀵瑰簲锛屼緥濡傦紝ceil鐨勮嫳鏂囨剰涔夋槸澶╄姳鏉匡紝璇ユ柟娉曞氨琛ㄧず鍚戜笂鍙栨暣锛孧ath.ceil(11.3)鐨勭粨鏋滀负12,Math.ceil(-11.3)鐨勭粨鏋滄槸-11锛沠loor鐨勮嫳鏂囨剰涔夋槸鍦版澘锛岃鏂规硶灏辫〃绀哄悜涓嬪彇鏁达紝Math.ceil(11.6)鐨勭粨鏋滀负11,Math.ceil(-11.6)鐨勭粨鏋滄槸-12锛涙渶闅炬帉鎻$殑鏄痳ound鏂规硶锛屽畠琛ㄧず鈥滃洓鑸嶄簲鍏モ�濓紝绠楁硶涓篗ath.floor(x+0.5)锛屽嵆灏嗗師鏉ョ殑鏁板瓧鍔犱笂0.5鍚庡啀鍚戜笅鍙栨暣锛屾墍浠ワ紝Math.round(11.5)鐨勭粨鏋滀负12锛孧ath.round(-11.5)鐨勭粨鏋滀负-11銆�

16銆佷笅闈㈢殑浠g爜鏈変粈涔堜笉濡ヤ箣澶�?

1. if(username.equals(鈥渮xx鈥�){}

username鍙兘涓篘ULL,浼氭姤绌烘寚閽堥敊璇紱鏀逛负"zxx".equals(username)

2.intx = 1;

return x==1?true:false; 聽杩欎釜鏀规垚return x==1;灏卞彲浠�!

17銆佽璇村嚭浣滅敤鍩焢ublic锛宲rivate锛宲rotected锛屼互鍙婁笉鍐欐椂鐨勫尯鍒�

杩欏洓涓綔鐢ㄥ煙鐨勫彲瑙佽寖鍥村涓嬭〃鎵�绀恒��

璇存槑锛氬鏋滃湪淇グ鐨勫厓绱犱笂闈㈡病鏈夊啓浠讳綍璁块棶淇グ绗︼紝鍒欒〃绀篺riendly銆�

浣滅敤鍩� 聽 聽褰撳墠绫诲悓涓�package瀛愬瓩绫诲叾浠杙ackage

public 聽 聽鈭� 聽 聽鈭� 聽 聽 聽 聽 鈭� 聽 聽 聽鈭�

protected 聽鈭� 聽 聽鈭� 聽 聽 聽 聽 鈭� 聽 聽 脳

friendly 聽 鈭� 聽 聽鈭� 聽 聽 聽 聽 脳 聽 聽 脳

private 聽 聽鈭� 聽 聽脳 聽 聽 聽 聽 脳 聽 聽 脳

澶囨敞锛氬彧瑕佽浣忎簡鏈�4绉嶈闂潈闄愶紝4涓闂寖鍥达紝鐒跺悗灏嗗叏閫夊拰鑼冨洿鍦ㄦ按骞冲拰鍨傜洿鏂瑰悜涓婂垎鍒寜鎺掍粠灏忓埌澶ф垨浠庡ぇ鍒板皬鐨勯『搴忔帓鍒楋紝灏卞緢瀹规槗鐢诲嚭涓婇潰鐨勫浘浜嗐��

18銆丱verload鍜孫verride鐨勫尯鍒�侽verloaded鐨勬柟娉曟槸鍚﹀彲浠ユ敼鍙樿繑鍥炲�肩殑绫诲瀷?

Overload鏄噸杞界殑鎰忔�濓紝Override鏄鐩栫殑鎰忔�濓紝涔熷氨鏄噸鍐欍��

閲嶈浇Overload琛ㄧず鍚屼竴涓被涓彲浠ユ湁澶氫釜鍚嶇О鐩稿悓鐨勬柟娉曪紝浣嗚繖浜涙柟娉曠殑鍙傛暟鍒楄〃鍚勪笉鐩稿悓锛堝嵆鍙傛暟涓暟鎴栫被鍨嬩笉鍚岋級銆�

閲嶅啓Override琛ㄧず瀛愮被涓殑鏂规硶鍙互涓庣埗绫讳腑鐨勬煇涓柟娉曠殑鍚嶇О鍜屽弬鏁板畬鍏ㄧ浉鍚岋紝閫氳繃瀛愮被鍒涘缓鐨勫疄渚嬪璞¤皟鐢ㄨ繖涓柟娉曟椂锛屽皢璋冪敤瀛愮被涓殑瀹氫箟鏂规硶锛岃繖鐩稿綋浜庢妸鐖剁被涓畾涔夌殑閭d釜瀹屽叏鐩稿悓鐨勬柟娉曠粰瑕嗙洊浜嗭紝杩欎篃鏄潰鍚戝璞$紪绋嬬殑澶氭�佹�х殑涓�绉嶈〃鐜般�傚瓙绫昏鐩栫埗绫荤殑鏂规硶鏃讹紝鍙兘姣旂埗绫绘姏鍑烘洿灏戠殑寮傚父锛屾垨鑰呮槸鎶涘嚭鐖剁被鎶涘嚭鐨勫紓甯哥殑瀛愬紓甯革紝鍥犱负瀛愮被鍙互瑙e喅鐖剁被鐨勪竴浜涢棶棰橈紝涓嶈兘姣旂埗绫绘湁鏇村鐨勯棶棰樸�傚瓙绫绘柟娉曠殑璁块棶鏉冮檺鍙兘姣旂埗绫荤殑鏇村ぇ锛屼笉鑳芥洿灏忋�傚鏋滅埗绫荤殑鏂规硶鏄痯rivate绫诲瀷锛岄偅涔堬紝瀛愮被鍒欎笉瀛樺湪瑕嗙洊鐨勯檺鍒讹紝鐩稿綋浜庡瓙绫讳腑澧炲姞浜嗕竴涓叏鏂扮殑鏂规硶銆�

鑷充簬Overloaded鐨勬柟娉曟槸鍚﹀彲浠ユ敼鍙樿繑鍥炲�肩殑绫诲瀷杩欎釜闂锛岃鐪嬩綘鍊掑簳鎯抽棶浠�涔堝憿锛熻繖涓鐩緢妯$硦銆傚鏋滃嚑涓狾verloaded鐨勬柟娉曠殑鍙傛暟鍒楄〃涓嶄竴鏍凤紝瀹冧滑鐨勮繑鍥炶�呯被鍨嬪綋鐒朵篃鍙互涓嶄竴鏍枫�備絾鎴戜及璁′綘鎯抽棶鐨勯棶棰樻槸锛氬鏋滀袱涓柟娉曠殑鍙傛暟鍒楄〃瀹屽叏涓�鏍凤紝鏄惁鍙互璁╁畠浠殑杩斿洖鍊间笉鍚屾潵瀹炵幇閲嶈浇Overload銆傝繖鏄笉琛岀殑锛屾垜浠彲浠ョ敤鍙嶈瘉娉曟潵璇存槑杩欎釜闂锛屽洜涓烘垜浠湁鏃跺�欒皟鐢ㄤ竴涓柟娉曟椂涔熷彲浠ヤ笉瀹氫箟杩斿洖缁撴灉鍙橀噺锛屽嵆涓嶈鍏冲績鍏惰繑鍥炵粨鏋滐紝渚嬪锛屾垜浠皟鐢╩ap.remove(key)鏂规硶鏃讹紝铏界劧remove鏂规硶鏈夎繑鍥炲�硷紝浣嗘槸鎴戜滑閫氬父閮戒笉浼氬畾涔夋帴鏀惰繑鍥炵粨鏋滅殑鍙橀噺锛岃繖鏃跺�欏亣璁捐绫讳腑鏈変袱涓悕绉板拰鍙傛暟鍒楄〃瀹屽叏鐩稿悓鐨勬柟娉曪紝浠呬粎鏄繑鍥炵被鍨嬩笉鍚岋紝java灏辨棤娉曠‘瀹氱紪绋嬭�呭�掑簳鏄兂璋冪敤鍝釜鏂规硶浜嗭紝鍥犱负瀹冩棤娉曢�氳繃杩斿洖缁撴灉绫诲瀷鏉ュ垽鏂��

override鍙互缈昏瘧涓鸿鐩栵紝浠庡瓧闈㈠氨鍙互鐭ラ亾锛屽畠鏄鐩栦簡涓�涓柟娉曞苟涓斿鍏堕噸鍐欙紝浠ユ眰杈惧埌涓嶅悓鐨勪綔鐢ㄣ�傚鎴戜滑鏉ヨ鏈�鐔熸倝鐨勮鐩栧氨鏄鎺ュ彛鏂规硶鐨勫疄鐜帮紝鍦ㄦ帴鍙d腑涓�鑸彧鏄鏂规硶杩涜浜嗗0鏄庯紝鑰屾垜浠湪瀹炵幇鏃讹紝灏遍渶瑕佸疄鐜版帴鍙e0鏄庣殑鎵�鏈夋柟娉曘�傞櫎浜嗚繖涓吀鍨嬬殑鐢ㄦ硶浠ュ锛屾垜浠湪缁ф壙涓篃鍙兘浼氬湪瀛愮被瑕嗙洊鐖剁被涓殑鏂规硶銆傚湪瑕嗙洊瑕佹敞鎰忎互涓嬬殑鍑犵偣锛�

1銆佽鐩栫殑鏂规硶鐨勬爣蹇楀繀椤昏鍜岃瑕嗙洊鐨勬柟娉曠殑鏍囧織瀹屽叏鍖归厤锛屾墠鑳借揪鍒拌鐩栫殑鏁堟灉锛�

2銆佽鐩栫殑鏂规硶鐨勮繑鍥炲�煎繀椤诲拰琚鐩栫殑鏂规硶鐨勮繑鍥炰竴鑷达紱

3銆佽鐩栫殑鏂规硶鎵�鎶涘嚭鐨勫紓甯稿繀椤诲拰琚鐩栨柟娉曠殑鎵�鎶涘嚭鐨勫紓甯镐竴鑷达紝鎴栬�呮槸鍏跺瓙绫伙紱

4銆佽瑕嗙洊鐨勬柟娉曚笉鑳戒负private锛屽惁鍒欏湪鍏跺瓙绫讳腑鍙槸鏂板畾涔変簡涓�涓柟娉曪紝骞舵病鏈夊鍏惰繘琛岃鐩栥��

overload瀵规垜浠潵璇村彲鑳芥瘮杈冪啛鎮夛紝鍙互缈昏瘧涓洪噸杞斤紝瀹冩槸鎸囨垜浠彲浠ュ畾涔変竴浜涘悕绉扮浉鍚岀殑鏂规硶锛岄�氳繃瀹氫箟涓嶅悓鐨勮緭鍏ュ弬鏁版潵鍖哄垎杩欎簺鏂规硶锛岀劧鍚庡啀璋冪敤鏃讹紝VM灏变細鏍规嵁涓嶅悓鐨勫弬鏁版牱寮忥紝鏉ラ�夋嫨鍚堥�傜殑鏂规硶鎵ц銆傚湪浣跨敤閲嶈浇瑕佹敞鎰忎互涓嬬殑鍑犵偣锛�

1銆佸湪浣跨敤閲嶈浇鏃跺彧鑳介�氳繃涓嶅悓鐨勫弬鏁版牱寮忋�備緥濡傦紝涓嶅悓鐨勫弬鏁扮被鍨嬶紝涓嶅悓鐨勫弬鏁颁釜鏁帮紝涓嶅悓鐨勫弬鏁伴『搴忥紙褰撶劧锛屽悓涓�鏂规硶鍐呯殑鍑犱釜鍙傛暟绫诲瀷蹇呴』涓嶄竴鏍凤紝渚嬪鍙互鏄痜un(int,float)锛屼絾鏄笉鑳戒负fun(int,int)锛夛紱

2銆佷笉鑳介�氳繃璁块棶鏉冮檺銆佽繑鍥炵被鍨嬨�佹姏鍑虹殑寮傚父杩涜閲嶈浇锛�

3銆佹柟娉曠殑寮傚父绫诲瀷鍜屾暟鐩笉浼氬閲嶈浇閫犳垚褰卞搷锛�

4銆佸浜庣户鎵挎潵璇达紝濡傛灉鏌愪竴鏂规硶鍦ㄧ埗绫讳腑鏄闂潈闄愭槸priavte锛岄偅涔堝氨涓嶈兘鍦ㄥ瓙绫诲鍏惰繘琛岄噸杞斤紝濡傛灉瀹氫箟鐨勮瘽锛屼篃鍙槸瀹氫箟浜嗕竴涓柊鏂规硶锛岃�屼笉浼氳揪鍒伴噸杞界殑鏁堟灉銆�

19銆佹瀯閫犲櫒Constructor鏄惁鍙override?

鏋勯�犲櫒Constructor涓嶈兘琚户鎵匡紝鍥犳涓嶈兘閲嶅啓Override锛屼絾鍙互琚噸杞絆verload銆�

20銆佹帴鍙f槸鍚﹀彲缁ф壙鎺ュ彛?鎶借薄绫绘槸鍚﹀彲瀹炵幇(implements)鎺ュ彛?鎶借薄绫绘槸鍚﹀彲缁ф壙鍏蜂綋绫�(concrete class)?鎶借薄绫讳腑鏄惁鍙互鏈夐潤鎬佺殑main鏂规硶锛�

鎺ュ彛鍙互缁ф壙鎺ュ彛銆傛娊璞$被鍙互瀹炵幇(implements)鎺ュ彛锛屾娊璞$被鍙互缁ф壙鍏蜂綋绫汇�傛娊璞$被涓彲浠ユ湁闈欐�佺殑main鏂规硶銆�

澶囨敞锛氬彧瑕佹槑鐧戒簡鎺ュ彛鍜屾娊璞$被鐨勬湰璐ㄥ拰浣滅敤锛岃繖浜涢棶棰橀兘寰堝ソ鍥炵瓟锛屼綘鎯虫兂锛屽鏋滀綘鏄痡ava璇█鐨勮璁¤�咃紝浣犳槸鍚︿細鎻愪緵杩欐牱鐨勬敮鎸侊紝濡傛灉涓嶆彁渚涚殑璇濓紝鏈変粈涔堢悊鐢卞悧锛熷鏋滀綘娌℃湁閬撶悊涓嶆彁渚涳紝閭g瓟妗堝氨鏄偗瀹氱殑浜嗐��

鍙湁璁颁綇鎶借薄绫讳笌鏅�氱被鐨勫敮涓�鍖哄埆灏辨槸涓嶈兘鍒涘缓瀹炰緥瀵硅薄鍜屽厑璁告湁abstract鏂规硶銆�

21銆佸啓clone()鏂规硶鏃讹紝閫氬父閮芥湁涓�琛屼唬鐮侊紝鏄粈涔堬紵

clone 鏈夌己鐪佽涓猴紝super.clone();鍥犱负棣栧厛瑕佹妸鐖剁被涓殑鎴愬憳澶嶅埗鍒颁綅锛岀劧鍚庢墠鏄鍒惰嚜宸辩殑鎴愬憳銆�

22銆侀潰鍚戝璞$殑鐗瑰緛鏈夊摢浜涙柟闈�

璁$畻鏈鸿蒋浠剁郴缁熸槸鐜板疄鐢熸椿涓殑涓氬姟鍦ㄨ绠楁満涓殑鏄犲皠锛岃�岀幇瀹炵敓娲讳腑鐨勪笟鍔″叾瀹炲氨鏄竴涓釜瀵硅薄鍗忎綔鐨勮繃绋嬨�傞潰鍚戝璞$紪绋嬪氨鏄寜鐜板疄涓氬姟涓�鏍风殑鏂瑰紡灏嗙▼搴忎唬鐮佹寜涓�涓釜瀵硅薄杩涜缁勭粐鍜岀紪鍐欙紝璁╄绠楁満绯荤粺鑳藉璇嗗埆鍜岀悊瑙g敤瀵硅薄鏂瑰紡缁勭粐鍜岀紪鍐欑殑绋嬪簭浠g爜锛岃繖鏍峰氨鍙互鎶婄幇瀹炵敓娲讳腑鐨勪笟鍔″璞℃槧灏勫埌璁$畻鏈虹郴缁熶腑銆�

闈㈠悜瀵硅薄鐨勭紪绋嬭瑷�鏈�4涓富瑕佺殑鐗瑰緛銆�

1灏佽锛�

灏佽鏄繚璇佽蒋浠堕儴浠跺叿鏈変紭鑹殑妯″潡鎬х殑鍩虹锛屽皝瑁呯殑鐩爣灏辨槸瑕佸疄鐜拌蒋浠堕儴浠剁殑鈥滈珮鍐呰仛銆佷綆鑰﹀悎鈥濓紝闃叉绋嬪簭鐩镐簰渚濊禆鎬ц�屽甫鏉ョ殑鍙樺姩褰卞搷銆傚湪闈㈠悜瀵硅薄鐨勭紪绋嬭瑷�涓紝瀵硅薄鏄皝瑁呯殑鏈�鍩烘湰鍗曚綅锛岄潰鍚戝璞$殑灏佽姣斾紶缁熻瑷�鐨勫皝瑁呮洿涓烘竻鏅般�佹洿涓烘湁鍔涖�傞潰鍚戝璞$殑灏佽灏辨槸鎶婃弿杩颁竴涓璞$殑灞炴�у拰琛屼负鐨勪唬鐮佸皝瑁呭湪涓�涓�滄ā鍧椻�濅腑锛屼篃灏辨槸涓�涓被涓紝灞炴�х敤鍙橀噺瀹氫箟锛岃涓虹敤鏂规硶杩涜瀹氫箟锛屾柟娉曞彲浠ョ洿鎺ヨ闂悓涓�涓璞′腑鐨勫睘鎬с�傞�氬父鎯呭喌涓嬶紝鍙璁颁綇璁╁彉閲忓拰璁块棶杩欎釜鍙橀噺鐨勬柟娉曟斁鍦ㄤ竴璧凤紝灏嗕竴涓被涓殑鎴愬憳鍙橀噺鍏ㄩ儴瀹氫箟鎴愮鏈夌殑锛屽彧鏈夎繖涓被鑷繁鐨勬柟娉曟墠鍙互璁块棶鍒拌繖浜涙垚鍛樺彉閲忥紝杩欏氨鍩烘湰涓婂疄鐜板璞$殑灏佽锛屽氨寰堝鏄撴壘鍑鸿鍒嗛厤鍒拌繖涓被涓婄殑鏂规硶浜嗭紝灏卞熀鏈笂绠楁槸浼氶潰鍚戝璞$殑缂栫▼浜嗐�傛妸鎻′竴涓師鍒欙細鎶婂鍚屼竴浜嬬墿杩涜鎿嶄綔鐨勬柟娉曞拰鐩稿叧鐨勬柟娉曟斁鍦ㄥ悓涓�涓被涓紝鎶婃柟娉曞拰瀹冩搷浣滅殑鏁版嵁鏀惧湪鍚屼竴涓被涓��

渚嬪锛屼汉瑕佸湪榛戞澘涓婄敾鍦嗭紝杩欎竴鍏辨秹鍙婁笁涓璞★細浜恒�侀粦鏉裤�佸渾锛岀敾鍦嗙殑鏂规硶瑕佸垎閰嶇粰鍝釜瀵硅薄鍛紵鐢变簬鐢诲渾闇�瑕佷娇鐢ㄥ埌鍦嗗績鍜屽崐寰勶紝鍦嗗績鍜屽崐寰勬樉鐒舵槸鍦嗙殑灞炴�э紝濡傛灉灏嗗畠浠湪绫讳腑瀹氫箟鎴愪簡绉佹湁鐨勬垚鍛樺彉閲忥紝閭d箞锛岀敾鍦嗙殑鏂规硶蹇呴』鍒嗛厤缁欏渾锛屽畠鎵嶈兘璁块棶鍒板渾蹇冨拰鍗婂緞杩欎袱涓睘鎬э紝浜轰互鍚庡彧鏄皟鐢ㄥ渾鐨勭敾鍦嗘柟娉曘�佽〃绀虹粰鍦嗗彂缁欐秷鎭�屽凡锛岀敾鍦嗚繖涓柟娉曚笉搴旇鍒嗛厤鍦ㄤ汉杩欎釜瀵硅薄涓婏紝杩欏氨鏄潰鍚戝璞$殑灏佽鎬э紝鍗冲皢瀵硅薄灏佽鎴愪竴涓珮搴﹁嚜娌诲拰鐩稿灏侀棴鐨勪釜浣擄紝瀵硅薄鐘舵�侊紙灞炴�э級鐢辫繖涓璞¤嚜宸辩殑琛屼负锛堟柟娉曪級鏉ヨ鍙栧拰鏀瑰彉銆�涓�涓洿渚夸簬鐞嗚В鐨勪緥瀛愬氨鏄紝鍙告満灏嗙伀杞﹀埞浣忎簡锛屽埞杞︾殑鍔ㄤ綔鏄垎閰嶇粰鍙告満锛岃繕鏄垎閰嶇粰鐏溅锛屾樉鐒讹紝搴旇鍒嗛厤缁欑伀杞︼紝鍥犱负鍙告満鑷韩鏄笉鍙兘鏈夐偅涔堝ぇ鐨勫姏姘斿皢涓�涓伀杞︾粰鍋滀笅鏉ョ殑锛屽彧鏈夌伀杞﹁嚜宸辨墠鑳藉畬鎴愯繖涓�鍔ㄤ綔锛岀伀杞﹂渶瑕佽皟鐢ㄥ唴閮ㄧ殑绂诲悎鍣ㄥ拰鍒硅溅鐗囩瓑澶氫釜鍣ㄤ欢鍗忎綔鎵嶈兘瀹屾垚鍒硅溅杩欎釜鍔ㄤ綔锛屽徃鏈哄埞杞︾殑杩囩▼鍙槸缁欑伀杞﹀彂浜嗕竴涓秷鎭紝閫氱煡鐏溅瑕佹墽琛屽埞杞﹀姩浣滆�屽凡銆�

鎶借薄锛�

鎶借薄灏辨槸鎵惧嚭涓�浜涗簨鐗╃殑鐩镐技鍜屽叡鎬т箣澶勶紝鐒跺悗灏嗚繖浜涗簨鐗╁綊涓轰竴涓被锛岃繖涓被鍙�冭檻杩欎簺浜嬬墿鐨勭浉浼煎拰鍏辨�т箣澶勶紝骞朵笖浼氬拷鐣ヤ笌褰撳墠涓婚鍜岀洰鏍囨棤鍏崇殑閭d簺鏂归潰锛屽皢娉ㄦ剰鍔涢泦涓湪涓庡綋鍓嶇洰鏍囨湁鍏崇殑鏂归潰銆備緥濡傦紝鐪嬪埌涓�鍙殏铓佸拰澶ц薄锛屼綘鑳藉鎯宠薄鍑哄畠浠殑鐩稿悓涔嬪锛岄偅灏辨槸鎶借薄銆傛娊璞″寘鎷涓烘娊璞″拰鐘舵�佹娊璞′袱涓柟闈€�備緥濡傦紝瀹氫箟涓�涓狿erson绫伙紝濡備笅锛�

Class Person{

String name;

int age;

}

浜烘湰鏉ユ槸寰堝鏉傜殑浜嬬墿锛屾湁寰堝鏂归潰锛屼絾鍥犱负褰撳墠绯荤粺鍙渶瑕佷簡瑙d汉鐨勫鍚嶅拰骞撮緞锛屾墍浠ヤ笂闈㈠畾涔夌殑绫讳腑鍙寘鍚鍚嶅拰骞撮緞杩欎袱涓睘鎬э紝杩欏氨鏄竴绉嶆娊鍍忥紝浣跨敤鎶借薄鍙互閬垮厤鑰冭檻涓�浜涗笌鐩爣鏃犲叧鐨勭粏鑺傘�傛垜瀵规娊璞$殑鐞嗚В灏辨槸涓嶈鐢ㄦ樉寰暅鍘荤湅涓�涓簨鐗╃殑鎵�鏈夋柟闈紝杩欐牱娑夊強鐨勫唴瀹瑰氨澶浜嗭紝鑰屾槸瑕佸杽浜庡垝鍒嗛棶棰樼殑杈圭晫锛屽綋鍓嶇郴缁熼渶瑕佷粈涔堬紝灏卞彧鑰冭檻浠�涔堛��

缁ф壙锛�

鍦ㄥ畾涔夊拰瀹炵幇涓�涓被鐨勬椂鍊欙紝鍙互鍦ㄤ竴涓凡缁忓瓨鍦ㄧ殑绫荤殑鍩虹涔嬩笂鏉ヨ繘琛岋紝鎶婅繖涓凡缁忓瓨鍦ㄧ殑绫绘墍瀹氫箟鐨勫唴瀹逛綔涓鸿嚜宸辩殑鍐呭锛屽苟鍙互鍔犲叆鑻ュ共鏂扮殑鍐呭锛屾垨淇敼鍘熸潵鐨勬柟娉曚娇涔嬫洿閫傚悎鐗规畩鐨勯渶瑕侊紝杩欏氨鏄户鎵裤�傜户鎵挎槸瀛愮被鑷姩鍏变韩鐖剁被鏁版嵁鍜屾柟娉曠殑鏈哄埗锛岃繖鏄被涔嬮棿鐨勪竴绉嶅叧绯伙紝鎻愰珮浜嗚蒋浠剁殑鍙噸鐢ㄦ�у拰鍙墿灞曟�с��

澶氭�侊細

澶氭�佹槸鎸囩▼搴忎腑瀹氫箟鐨勫紩鐢ㄥ彉閲忔墍鎸囧悜鐨勫叿浣撶被鍨嬪拰閫氳繃璇ュ紩鐢ㄥ彉閲忓彂鍑虹殑鏂规硶璋冪敤鍦ㄧ紪绋嬫椂骞朵笉纭畾锛岃�屾槸鍦ㄧ▼搴忚繍琛屾湡闂存墠纭畾锛屽嵆涓�涓紩鐢ㄥ彉閲忓�掑簳浼氭寚鍚戝摢涓被鐨勫疄渚嬪璞★紝璇ュ紩鐢ㄥ彉閲忓彂鍑虹殑鏂规硶璋冪敤鍒板簳鏄摢涓被涓疄鐜扮殑鏂规硶锛屽繀椤诲湪鐢辩▼搴忚繍琛屾湡闂存墠鑳藉喅瀹氥�傚洜涓哄湪绋嬪簭杩愯鏃舵墠纭畾鍏蜂綋鐨勭被锛岃繖鏍凤紝涓嶇敤淇敼婧愮▼搴忎唬鐮侊紝灏卞彲浠ヨ寮曠敤鍙橀噺缁戝畾鍒板悇绉嶄笉鍚岀殑绫诲疄鐜颁笂锛屼粠鑰屽鑷磋寮曠敤璋冪敤鐨勫叿浣撴柟娉曢殢涔嬫敼鍙橈紝鍗充笉淇敼绋嬪簭浠g爜灏卞彲浠ユ敼鍙樼▼搴忚繍琛屾椂鎵�缁戝畾鐨勫叿浣撲唬鐮侊紝璁╃▼搴忓彲浠ラ�夋嫨澶氫釜杩愯鐘舵�侊紝杩欏氨鏄鎬佹�с�傚鎬佹�у寮轰簡杞欢鐨勭伒娲绘�у拰鎵╁睍鎬с�備緥濡傦紝涓嬮潰浠g爜涓殑UserDao鏄竴涓帴鍙o紝瀹冨畾涔夊紩鐢ㄥ彉閲弖serDao鎸囧悜鐨勫疄渚嬪璞$敱daofactory.getDao()鍦ㄦ墽琛岀殑鏃跺�欒繑鍥烇紝鏈夋椂鍊欐寚鍚戠殑鏄疷serJdbcDao杩欎釜瀹炵幇锛屾湁鏃跺�欐寚鍚戠殑鏄疷serHibernateDao杩欎釜瀹炵幇锛岃繖鏍凤紝涓嶇敤淇敼婧愪唬鐮侊紝灏卞彲浠ユ敼鍙榰serDao鎸囧悜鐨勫叿浣撶被瀹炵幇锛屼粠鑰屽鑷磚serDao.insertUser()鏂规硶璋冪敤鐨勫叿浣撲唬鐮佷篃闅忎箣鏀瑰彉锛屽嵆鏈夋椂鍊欒皟鐢ㄧ殑鏄疷serJdbcDao鐨刬nsertUser鏂规硶锛屾湁鏃跺�欒皟鐢ㄧ殑鏄疷serHibernateDao鐨刬nsertUser鏂规硶锛�

UserDao userDao =daofactory.getDao();

userDao.insertUser(user);

姣斿柣锛氫汉鍚冮キ锛屼綘鐪嬪埌鐨勬槸宸︽墜锛岃繕鏄彸鎵嬶紵

23銆乯ava涓疄鐜板鎬佺殑鏈哄埗鏄粈涔堬紵

闈犵殑鏄埗绫绘垨鎺ュ彛瀹氫箟鐨勫紩鐢ㄥ彉閲忓彲浠ユ寚鍚戝瓙绫绘垨鍏蜂綋瀹炵幇绫荤殑瀹炰緥瀵硅薄锛岃�岀▼搴忚皟鐢ㄧ殑鏂规硶鍦ㄨ繍琛屾湡鎵嶅姩鎬佺粦瀹氾紝灏辨槸寮曠敤鍙橀噺鎵�鎸囧悜鐨勫叿浣撳疄渚嬪璞$殑鏂规硶锛屼篃灏辨槸鍐呭瓨閲屾鍦ㄨ繍琛岀殑閭d釜瀵硅薄鐨勬柟娉曪紝鑰屼笉鏄紩鐢ㄥ彉閲忕殑绫诲瀷涓畾涔夌殑鏂规硶銆�

24銆乤bstract class鍜宨nterface鏈変粈涔堝尯鍒�?

鍚湁abstract淇グ绗︾殑class鍗充负鎶借薄绫伙紝abstract绫讳笉鑳藉垱寤哄疄渚嬪璞°�傚惈鏈塧bstract鏂规硶鐨勭被蹇呴』瀹氫箟涓篴bstract class锛宎bstract class绫讳腑鐨勬柟娉曚笉蹇呮槸鎶借薄鐨勩�俛bstract class绫讳腑瀹氫箟鎶借薄鏂规硶蹇呴』鍦ㄥ叿浣�(Concrete)瀛愮被涓疄鐜帮紝鎵�浠ワ紝涓嶈兘鏈夋娊璞℃瀯閫犳柟娉曟垨鎶借薄闈欐�佹柟娉曘�傚鏋滅殑瀛愮被娌℃湁瀹炵幇鎶借薄鐖剁被涓殑鎵�鏈夋娊璞℃柟娉曪紝閭d箞瀛愮被涔熷繀椤诲畾涔変负abstract绫诲瀷銆�

鎺ュ彛锛坕nterface锛夊彲浠ヨ鎴愭槸鎶借薄绫荤殑涓�绉嶇壒渚嬶紝鎺ュ彛涓殑鎵�鏈夋柟娉曢兘蹇呴』鏄娊璞$殑銆傛帴鍙d腑鐨勬柟娉曞畾涔夐粯璁や负public abstract绫诲瀷锛屾帴鍙d腑鐨勬垚鍛樺彉閲忕被鍨嬮粯璁や负public static final銆�

涓嬮潰姣旇緝涓�涓嬩袱鑰呯殑璇硶鍖哄埆锛�

1.鎶借薄绫诲彲浠ユ湁鏋勯�犳柟娉曪紝鎺ュ彛涓笉鑳芥湁鏋勯�犳柟娉曘��

2.鎶借薄绫讳腑鍙互鏈夋櫘閫氭垚鍛樺彉閲忥紝鎺ュ彛涓病鏈夋櫘閫氭垚鍛樺彉閲�

3.鎶借薄绫讳腑鍙互鍖呭惈闈炴娊璞$殑鏅�氭柟娉曪紝鎺ュ彛涓殑鎵�鏈夋柟娉曞繀椤婚兘鏄娊璞$殑锛屼笉鑳芥湁闈炴娊璞$殑鏅�氭柟娉曘��

4.鎶借薄绫讳腑鐨勬娊璞℃柟娉曠殑璁块棶绫诲瀷鍙互鏄痯ublic锛宲rotected鍜岋紙榛樿绫诲瀷,铏界劧

eclipse涓嬩笉鎶ラ敊锛屼絾搴旇涔熶笉琛岋級锛屼絾鎺ュ彛涓殑鎶借薄鏂规硶鍙兘鏄痯ublic绫诲瀷鐨勶紝骞朵笖榛樿鍗充负public abstract绫诲瀷銆�

5.鎶借薄绫讳腑鍙互鍖呭惈闈欐�佹柟娉曪紝鎺ュ彛涓笉鑳藉寘鍚潤鎬佹柟娉�

6.鎶借薄绫诲拰鎺ュ彛涓兘鍙互鍖呭惈闈欐�佹垚鍛樺彉閲忥紝鎶借薄绫讳腑鐨勯潤鎬佹垚鍛樺彉閲忕殑璁块棶绫诲瀷鍙互浠绘剰锛屼絾鎺ュ彛涓畾涔夌殑鍙橀噺鍙兘鏄痯ublic static final绫诲瀷锛屽苟涓旈粯璁ゅ嵆涓簆ublic static final绫诲瀷銆�

7. 涓�涓被鍙互瀹炵幇澶氫釜鎺ュ彛锛屼絾鍙兘缁ф壙涓�涓娊璞$被銆�

涓嬮潰鎺ョ潃鍐嶈璇翠袱鑰呭湪搴旂敤涓婄殑鍖哄埆锛�

鎺ュ彛鏇村鐨勬槸鍦ㄧ郴缁熸灦鏋勮璁℃柟娉曞彂鎸ヤ綔鐢紝涓昏鐢ㄤ簬瀹氫箟妯″潡涔嬮棿鐨勯�氫俊濂戠害銆傝�屾娊璞$被鍦ㄤ唬鐮佸疄鐜版柟闈㈠彂鎸ヤ綔鐢紝鍙互瀹炵幇浠g爜鐨勯噸鐢紝渚嬪锛屾ā鏉挎柟娉曡璁℃ā寮忔槸鎶借薄绫荤殑涓�涓吀鍨嬪簲鐢紝鍋囪鏌愪釜椤圭洰鐨勬墍鏈塖ervlet绫婚兘瑕佺敤鐩稿悓鐨勬柟寮忚繘琛屾潈闄愬垽鏂�佽褰曡闂棩蹇楀拰澶勭悊寮傚父锛岄偅涔堝氨鍙互瀹氫箟涓�涓娊璞$殑鍩虹被锛岃鎵�鏈夌殑Servlet閮界户鎵胯繖涓娊璞″熀绫伙紝鍦ㄦ娊璞″熀绫荤殑service鏂规硶涓畬鎴愭潈闄愬垽鏂�佽褰曡闂棩蹇楀拰澶勭悊寮傚父鐨勪唬鐮侊紝鍦ㄥ悇涓瓙绫讳腑鍙槸瀹屾垚鍚勮嚜鐨勪笟鍔¢�昏緫浠g爜锛屼吉浠g爜濡備笅锛�

public abstract classBaseServlet extends HttpServlet{

public final void service(HttpServletRequest request,HttpServletResponse response) throws IOExcetion,ServletException 聽 聽 聽 {

璁板綍璁块棶鏃ュ織

杩涜鏉冮檺鍒ゆ柇

if(鍏锋湁鏉冮檺){

try{

doService(request,response);

}

catch(Excetpion e) 聽{

璁板綍寮傚父淇℃伅

}

}

}

protected abstract void doService(HttpServletRequest request,HttpServletResponse response) throws IOExcetion,ServletException;

//娉ㄦ剰璁块棶鏉冮檺瀹氫箟鎴恜rotected锛屾樉寰楁棦涓撲笟锛屽張涓ヨ皑锛屽洜涓哄畠鏄笓闂ㄧ粰瀛愮被鐢ㄧ殑

}

public class MyServlet1 extendsBaseServlet

{

protected voiddoService(HttpServletRequest request, HttpServletResponse response) throwsIOExcetion,ServletException

{

鏈琒ervlet鍙鐞嗙殑鍏蜂綋涓氬姟閫昏緫浠g爜

}

}

鐖剁被鏂规硶涓棿鐨勬煇娈典唬鐮佷笉纭畾锛岀暀缁欏瓙绫诲共锛屽氨鐢ㄦā鏉挎柟娉曡璁℃ā寮忋��

澶囨敞锛氳繖閬撻鐨勬�濊矾鏄厛浠庢�讳綋瑙i噴鎶借薄绫诲拰鎺ュ彛鐨勫熀鏈蹇碉紝鐒跺悗鍐嶆瘮杈冧袱鑰呯殑璇硶缁嗚妭锛屾渶鍚庡啀璇翠袱鑰呯殑搴旂敤鍖哄埆銆傛瘮杈冧袱鑰呰娉曠粏鑺傚尯鍒殑鏉$悊鏄細鍏堜粠涓�涓被涓殑鏋勯�犳柟娉曘�佹櫘閫氭垚鍛樺彉閲忓拰鏂规硶锛堝寘鎷娊璞℃柟娉曪級锛岄潤鎬佸彉閲忓拰鏂规硶锛岀户鎵挎�х瓑6涓柟闈㈤�愪竴鍘绘瘮杈冨洖绛旓紝鎺ョ潃浠庣涓夎�呯户鎵跨殑瑙掑害鐨勫洖绛旓紝鐗瑰埆鏄渶鍚庣敤浜嗕竴涓吀鍨嬬殑渚嬪瓙鏉ュ睍鐜拌嚜宸辨繁鍘氱殑鎶�鏈姛搴曘��

25銆乤bstract鐨刴ethod鏄惁鍙悓鏃舵槸static,鏄惁鍙悓鏃舵槸native锛屾槸鍚﹀彲鍚屾椂鏄痵ynchronized?

abstract鐨刴ethod涓嶅彲浠ユ槸static鐨勶紝鍥犱负鎶借薄鐨勬柟娉曟槸瑕佽瀛愮被瀹炵幇鐨勶紝鑰宻tatic涓庡瓙绫绘壇涓嶄笂鍏崇郴锛�

native鏂规硶琛ㄧず璇ユ柟娉曡鐢ㄥ彟澶栦竴绉嶄緷璧栧钩鍙扮殑缂栫▼璇█瀹炵幇鐨勶紝涓嶅瓨鍦ㄧ潃琚瓙绫诲疄鐜扮殑闂锛屾墍浠ワ紝瀹冧篃涓嶈兘鏄娊璞$殑锛屼笉鑳戒笌abstract娣风敤銆備緥濡傦紝FileOutputSteam绫昏纭欢鎵撲氦閬擄紝搴曞眰鐨勫疄鐜扮敤鐨勬槸鎿嶄綔绯荤粺鐩稿叧鐨刟pi瀹炵幇锛屼緥濡傦紝鍦╳indows鐢╟璇█瀹炵幇鐨勶紝鎵�浠ワ紝鏌ョ湅jdk鐨勬簮浠g爜锛屽彲浠ュ彂鐜癋ileOutputStream鐨刼pen鏂规硶鐨勫畾涔夊涓嬶細

private native void open(Stringname) throws FileNotFoundException;

濡傛灉鎴戜滑瑕佺敤java璋冪敤鍒汉鍐欑殑c璇█鍑芥暟锛屾垜浠槸鏃犳硶鐩存帴璋冪敤鐨勶紝鎴戜滑闇�瑕佹寜鐓ava鐨勮姹傚啓涓�涓猚璇█鐨勫嚱鏁帮紝鍙堟垜浠殑杩欎釜c璇█鍑芥暟鍘昏皟鐢ㄥ埆浜虹殑c璇█鍑芥暟銆傜敱浜庢垜浠殑c璇█鍑芥暟鏄寜java鐨勮姹傛潵鍐欑殑锛屾垜浠繖涓猚璇█鍑芥暟灏卞彲浠ヤ笌java瀵规帴涓婏紝java閭h竟鐨勫鎺ユ柟寮忓氨鏄畾涔夊嚭涓庢垜浠繖涓猚鍑芥暟鐩稿搴旂殑鏂规硶锛宩ava涓搴旂殑鏂规硶涓嶉渶瑕佸啓鍏蜂綋鐨勪唬鐮侊紝浣嗛渶瑕佸湪鍓嶉潰澹版槑native銆�

鍏充簬synchronized涓巃bstract鍚堢敤鐨勯棶棰橈紝鎴戣寰椾篃涓嶈锛屽洜涓哄湪鎴戝嚑骞寸殑瀛︿範鍜屽紑鍙戜腑锛屼粠鏉ユ病瑙佸埌杩囪繖绉嶆儏鍐碉紝骞朵笖鎴戣寰梥ynchronized搴旇鏄綔鐢ㄥ湪涓�涓叿浣撶殑鏂规硶涓婃墠鏈夋剰涔夈�傝�屼笖锛屾柟娉曚笂鐨剆ynchronized鍚屾鎵�浣跨敤鐨勫悓姝ラ攣瀵硅薄鏄痶his锛岃�屾娊璞℃柟娉曚笂鏃犳硶纭畾this鏄粈涔堛��

26銆佷粈涔堟槸鍐呴儴绫伙紵Static Nested Class鍜孖nner Class鐨勪笉鍚屻��

鍐呴儴绫诲氨鏄湪涓�涓被鐨勫唴閮ㄥ畾涔夌殑绫伙紝鍐呴儴绫讳腑涓嶈兘瀹氫箟闈欐�佹垚鍛橈紙闈欐�佹垚鍛樹笉鏄璞$殑鐗规�э紝鍙槸涓轰簡鎵句竴涓韬箣澶勶紝鎵�浠ラ渶瑕佹斁鍒颁竴涓被涓�屽凡锛岃繖涔堜竴鐐瑰皬浜嬶紝浣犺繕瑕佹妸瀹冩斁鍒扮被鍐呴儴鐨勪竴涓被涓紝杩囧垎浜嗗晩锛佹彁渚涘唴閮ㄧ被锛屼笉鏄负璁╀綘骞茶繖绉嶄簨鎯咃紝鏃犺亰锛屼笉璁╀綘骞层�傛垜鎯冲彲鑳芥槸鏃㈢劧闈欐�佹垚鍛樼被浼糲璇█鐨勫叏灞�鍙橀噺锛岃�屽唴閮ㄧ被閫氬父鏄敤浜庡垱寤哄唴閮ㄥ璞$敤鐨勶紝鎵�浠ワ紝鎶娾�滃叏灞�鍙橀噺鈥濇斁鍦ㄥ唴閮ㄧ被涓氨鏄鏃犳剰涔夌殑浜嬫儏锛屾棦鐒舵槸姣棤鎰忎箟鐨勪簨鎯咃紝灏卞簲璇ヨ绂佹锛夛紝鍐呴儴绫诲彲浠ョ洿鎺ヨ闂閮ㄧ被涓殑鎴愬憳鍙橀噺锛屽唴閮ㄧ被鍙互瀹氫箟鍦ㄥ閮ㄧ被鐨勬柟娉曞闈紝涔熷彲浠ュ畾涔夊湪澶栭儴绫荤殑鏂规硶浣撲腑锛屽涓嬫墍绀猴細

public class Outer

{

int out_x 聽= 0;

public void method()

{

Inner1 inner1 = new Inner1();

public class Inner2 聽//鍦ㄦ柟娉曚綋鍐呴儴瀹氫箟鐨勫唴閮ㄧ被

{

public method()

{

out_x = 3;

}

}

Inner2 inner2 = new Inner2();

}

public class Inner1 聽//鍦ㄦ柟娉曚綋澶栭潰瀹氫箟鐨勫唴閮ㄧ被

{

}

}

鍦ㄦ柟娉曚綋澶栭潰瀹氫箟鐨勫唴閮ㄧ被鐨勮闂被鍨嬪彲浠ユ槸public,protecte,榛樿鐨勶紝private绛�4绉嶇被鍨嬶紝杩欏氨濂藉儚绫讳腑瀹氫箟鐨勬垚鍛樺彉閲忔湁4绉嶈闂被鍨嬩竴鏍凤紝瀹冧滑鍐冲畾杩欎釜鍐呴儴绫荤殑瀹氫箟瀵瑰叾浠栫被鏄惁鍙锛涘浜庤繖绉嶆儏鍐碉紝鎴戜滑涔熷彲浠ュ湪澶栭潰鍒涘缓鍐呴儴绫荤殑瀹炰緥瀵硅薄锛屽垱寤哄唴閮ㄧ被鐨勫疄渚嬪璞℃椂锛屼竴瀹氳鍏堝垱寤哄閮ㄧ被鐨勫疄渚嬪璞★紝鐒跺悗鐢ㄨ繖涓閮ㄧ被鐨勫疄渚嬪璞″幓鍒涘缓鍐呴儴绫荤殑瀹炰緥瀵硅薄锛屼唬鐮佸涓嬶細

Outer outer = new Outer();

Outer.Inner1 inner1 = outer.new Innner1();

鍦ㄦ柟娉曞唴閮ㄥ畾涔夌殑鍐呴儴绫诲墠闈笉鑳芥湁璁块棶绫诲瀷淇グ绗︼紝灏卞ソ鍍忔柟娉曚腑瀹氫箟鐨勫眬閮ㄥ彉閲忎竴鏍凤紝浣嗚繖绉嶅唴閮ㄧ被鐨勫墠闈㈠彲浠ヤ娇鐢╢inal鎴朼bstract淇グ绗︺�傝繖绉嶅唴閮ㄧ被瀵瑰叾浠栫被鏄笉鍙鐨勫叾浠栫被鏃犳硶寮曠敤杩欑鍐呴儴绫伙紝浣嗘槸杩欑鍐呴儴绫诲垱寤虹殑瀹炰緥瀵硅薄鍙互浼犻�掔粰鍏朵粬绫昏闂�傝繖绉嶅唴閮ㄧ被蹇呴』鏄厛瀹氫箟锛屽悗浣跨敤锛屽嵆鍐呴儴绫荤殑瀹氫箟浠g爜蹇呴』鍑虹幇鍦ㄤ娇鐢ㄨ绫讳箣鍓嶏紝杩欎笌鏂规硶涓殑灞�閮ㄥ彉閲忓繀椤诲厛瀹氫箟鍚庝娇鐢ㄧ殑閬撶悊涔熸槸涓�鏍风殑銆傝繖绉嶅唴閮ㄧ被鍙互璁块棶鏂规硶浣撲腑鐨勫眬閮ㄥ彉閲忥紝浣嗘槸锛岃灞�閮ㄥ彉閲忓墠蹇呴』鍔爁inal淇グ绗︺��

瀵逛簬杩欎簺缁嗚妭锛屽彧瑕佸湪eclipse鍐欎唬鐮佽瘯璇曪紝鏍规嵁寮�鍙戝伐鍏锋彁绀虹殑鍚勭被閿欒淇℃伅灏卞彲浠ラ┈涓婁簡瑙e埌銆�

鍦ㄦ柟娉曚綋鍐呴儴杩樺彲浠ラ噰鐢ㄥ涓嬭娉曟潵鍒涘缓涓�绉嶅尶鍚嶅唴閮ㄧ被锛屽嵆瀹氫箟鏌愪竴鎺ュ彛鎴栫被鐨勫瓙绫荤殑鍚屾椂锛岃繕鍒涘缓浜嗚瀛愮被鐨勫疄渚嬪璞★紝鏃犻渶涓鸿瀛愮被瀹氫箟鍚嶇О锛�

public class Outer

{

public void start()

{

new Thread(

new Runable(){

public void run(){};

}

).start();

}

}

鏈�鍚庯紝鍦ㄦ柟娉曞閮ㄥ畾涔夌殑鍐呴儴绫诲墠闈㈠彲浠ュ姞涓妔tatic鍏抽敭瀛楋紝浠庤�屾垚涓篠tatic Nested Class锛屽畠涓嶅啀鍏锋湁鍐呴儴绫荤殑鐗规�э紝鎵�鏈夛紝浠庣嫮涔変笂璁诧紝瀹冧笉鏄唴閮ㄧ被銆係tatic Nested Class涓庢櫘閫氱被鍦ㄨ繍琛屾椂鐨勮涓哄拰鍔熻兘涓婃病鏈変粈涔堝尯鍒紝鍙槸鍦ㄧ紪绋嬪紩鐢ㄦ椂鐨勮娉曚笂鏈変竴浜涘樊鍒紝瀹冨彲浠ュ畾涔夋垚public銆乸rotected銆侀粯璁ょ殑銆乸rivate绛夊绉嶇被鍨嬶紝鑰屾櫘閫氱被鍙兘瀹氫箟鎴恜ublic鍜岄粯璁ょ殑杩欎袱绉嶇被鍨嬨�傚湪澶栭潰寮曠敤Static Nested Class绫荤殑鍚嶇О涓衡�滃閮ㄧ被鍚�.鍐呴儴绫诲悕鈥濄�傚湪澶栭潰涓嶉渶瑕佸垱寤哄閮ㄧ被鐨勫疄渚嬪璞★紝灏卞彲浠ョ洿鎺ュ垱寤篠tatic Nested Class锛屼緥濡傦紝鍋囪Inner鏄畾涔夊湪Outer绫讳腑鐨凷tatic Nested Class锛岄偅涔堝彲浠ヤ娇鐢ㄥ涓嬭鍙ュ垱寤篒nner绫伙細

Outer.Inner inner = new Outer.Inner();

鐢变簬static Nested Class涓嶄緷璧栦簬澶栭儴绫荤殑瀹炰緥瀵硅薄锛屾墍浠ワ紝static Nested Class鑳借闂閮ㄧ被鐨勯潪static鎴愬憳鍙橀噺(涓嶈兘鐩存帴璁块棶锛岄渶瑕佸垱寤哄閮ㄧ被瀹炰緥鎵嶈兘璁块棶闈為潤鎬佸彉閲�)銆�褰撳湪澶栭儴绫讳腑璁块棶Static Nested Class鏃讹紝鍙互鐩存帴浣跨敤Static Nested Class鐨勫悕瀛楋紝鑰屼笉闇�瑕佸姞涓婂閮ㄧ被鐨勫悕瀛椾簡锛屽湪Static Nested Class涓篃鍙互鐩存帴寮曠敤澶栭儴绫荤殑static鐨勬垚鍛樺彉閲忥紝涓嶉渶瑕佸姞涓婂閮ㄧ被鐨勫悕瀛椼��

鍦ㄩ潤鎬佹柟娉曚腑瀹氫箟鐨勫唴閮ㄧ被涔熸槸Static Nested Class锛岃繖鏃跺�欎笉鑳藉湪绫诲墠闈㈠姞static鍏抽敭瀛楋紝闈欐�佹柟娉曚腑鐨凷tatic Nested Class涓庢櫘閫氭柟娉曚腑鐨勫唴閮ㄧ被鐨勫簲鐢ㄦ柟寮忓緢鐩镐技锛屽畠闄や簡鍙互鐩存帴璁块棶澶栭儴绫讳腑鐨剆tatic鐨勬垚鍛樺彉閲忥紝杩樺彲浠ヨ闂潤鎬佹柟娉曚腑鐨勫眬閮ㄥ彉閲忥紝浣嗘槸锛岃灞�閮ㄥ彉閲忓墠蹇呴』鍔爁inal淇グ绗︺��

澶囨敞锛氶鍏堟牴鎹綘鐨勫嵃璞¤鍑轰綘瀵瑰唴閮ㄧ被鐨勬�讳綋鏂归潰鐨勭壒鐐癸細渚嬪锛屽湪涓や釜鍦版柟鍙互瀹氫箟锛屽彲浠ヨ闂閮ㄧ被鐨勬垚鍛樺彉閲忥紝涓嶈兘瀹氫箟闈欐�佹垚鍛橈紝杩欐槸澶х殑鐗圭偣銆傜劧鍚庡啀璇翠竴浜涚粏鑺傛柟闈㈢殑鐭ヨ瘑锛屼緥濡傦紝鍑犵瀹氫箟鏂瑰紡鐨勮娉曞尯鍒紝闈欐�佸唴閮ㄧ被锛屼互鍙婂尶鍚嶅唴閮ㄧ被銆�

27銆佸唴閮ㄧ被鍙互寮曠敤瀹冪殑鍖呭惈绫荤殑鎴愬憳鍚楋紵鏈夋病鏈変粈涔堥檺鍒讹紵

瀹屽叏鍙互銆傚鏋滀笉鏄潤鎬佸唴閮ㄧ被锛岄偅娌℃湁浠�涔堥檺鍒讹紒

濡傛灉浣犳妸闈欐�佸祵濂楃被褰撲綔鍐呴儴绫荤殑涓�绉嶇壒渚嬶紝閭e湪杩欑鎯呭喌涓嬩笉鍙互璁块棶澶栭儴绫荤殑鏅�氭垚鍛樺彉閲忥紝鑰屽彧鑳借闂閮ㄧ被涓殑闈欐�佹垚鍛橈紝渚嬪锛屼笅闈㈢殑浠g爜锛�

class Outer

{

static int x;

static class Inner

{

void test()

{

syso(x);

}

}

}

绛旈鏃讹紝涔熻鑳藉療瑷�瑙傝壊锛屾彛鎽╂彁闂�呯殑蹇冩�濓紝鏄剧劧浜哄甯屾湜浣犺鐨勬槸闈欐�佸唴閮ㄧ被涓嶈兘璁块棶澶栭儴绫荤殑鎴愬憳锛屼絾浣犱竴涓婃潵灏遍《鐗涳紝杩欎笉濂斤紝瑕佸厛椤虹潃浜哄锛岃浜哄婊℃剰锛岀劧鍚庡啀璇寸壒娈婃儏鍐碉紝璁╀汉瀹跺悆鎯娿��

28銆丄nonymous Inner Class (鍖垮悕鍐呴儴绫�)鏄惁鍙互extends(缁ф壙)鍏跺畠绫伙紝鏄惁鍙互implements(瀹炵幇)interface(鎺ュ彛)?

鍙互缁ф壙鍏朵粬绫绘垨瀹炵幇鍏朵粬鎺ュ彛銆備笉浠呮槸鍙互锛岃�屾槸蹇呴』!

29銆乻uper.getClass()鏂规硶璋冪敤

涓嬮潰绋嬪簭鐨勮緭鍑虹粨鏋滄槸澶氬皯锛�

Import java.util.Date;

publicclassTestextendsDate{

public static voidmain(String[] args) {

newTest().test();

}

public voidtest(){

System.out.println(super.getClass().getName());

}

}

寰堝鎬紝缁撴灉鏄疶est

杩欏睘浜庤剳绛嬫�ヨ浆寮殑棰樼洰锛屽湪涓�涓猶q缇ゆ湁涓綉鍙嬫濂介棶杩囪繖涓棶棰橈紝鎴戣寰楁尯鏈夎叮锛屽氨鐮旂┒浜嗕竴涓嬶紝娌℃兂鍒颁粖澶╄繕琚綘闈㈠埌浜嗭紝鍝堝搱銆�

鍦╰est鏂规硶涓紝鐩存帴璋冪敤getClass().getName()鏂规硶锛岃繑鍥炵殑鏄疶est绫诲悕

鐢变簬getClass()鍦∣bject绫讳腑瀹氫箟鎴愪簡final锛屽瓙绫讳笉鑳借鐩栬鏂规硶锛屾墍浠ワ紝鍦�

test鏂规硶涓皟鐢╣etClass().getName()鏂规硶锛屽叾瀹炲氨鏄湪璋冪敤浠庣埗绫荤户鎵跨殑getClass()鏂规硶锛岀瓑鏁堜簬璋冪敤super.getClass().getName()鏂规硶锛屾墍浠ワ紝super.getClass().getName()鏂规硶杩斿洖鐨勪篃搴旇鏄疶est銆�

濡傛灉鎯冲緱鍒扮埗绫荤殑鍚嶇О锛屽簲璇ョ敤濡備笅浠g爜锛�

getClass().getSuperClass().getName();

30銆丼tring鏄渶鍩烘湰鐨勬暟鎹被鍨嬪悧?

鍩烘湰鏁版嵁绫诲瀷鍖呮嫭byte銆乮nt銆乧har銆乴ong銆乫loat銆乨ouble銆乥oolean鍜宻hort銆�

java.lang.String绫绘槸final绫诲瀷鐨勶紝鍥犳涓嶅彲浠ョ户鎵胯繖涓被銆佷笉鑳戒慨鏀硅繖涓被銆備负浜嗘彁楂樻晥鐜囪妭鐪佺┖闂达紝鎴戜滑搴旇鐢⊿tringBuffer绫�

31銆丼tring s = "Hello";s = s + " world!";杩欎袱琛屼唬鐮佹墽琛屽悗锛屽師濮嬬殑String瀵硅薄涓殑鍐呭鍒板簳鍙樹簡娌℃湁锛�

娌℃湁銆傚洜涓篠tring琚璁℃垚涓嶅彲鍙�(immutable)绫伙紝鎵�浠ュ畠鐨勬墍鏈夊璞¢兘鏄笉鍙彉瀵硅薄銆傚湪杩欐浠g爜涓紝s鍘熷厛鎸囧悜涓�涓猄tring瀵硅薄锛屽唴瀹规槸 "Hello"锛岀劧鍚庢垜浠s杩涜浜�+鎿嶄綔锛岄偅涔坰鎵�鎸囧悜鐨勯偅涓璞℃槸鍚﹀彂鐢熶簡鏀瑰彉鍛紵绛旀鏄病鏈夈�傝繖鏃讹紝s涓嶆寚鍚戝師鏉ラ偅涓璞′簡锛岃�屾寚鍚戜簡鍙︿竴涓� String瀵硅薄锛屽唴瀹逛负"Hello world!"锛屽師鏉ラ偅涓璞¤繕瀛樺湪浜庡唴瀛樹箣涓紝鍙槸s杩欎釜寮曠敤鍙橀噺涓嶅啀鎸囧悜瀹冧簡銆�

閫氳繃涓婇潰鐨勮鏄庯紝鎴戜滑寰堝鏄撳鍑哄彟涓�涓粨璁猴紝濡傛灉缁忓父瀵瑰瓧绗︿覆杩涜鍚勭鍚勬牱鐨勪慨鏀癸紝鎴栬�呰锛屼笉鍙瑙佺殑淇敼锛岄偅涔堜娇鐢⊿tring鏉ヤ唬琛ㄥ瓧绗︿覆鐨勮瘽浼氬紩璧峰緢澶х殑鍐呭瓨寮�閿�銆傚洜涓� String瀵硅薄寤虹珛涔嬪悗涓嶈兘鍐嶆敼鍙橈紝鎵�浠ュ浜庢瘡涓�涓笉鍚岀殑瀛楃涓诧紝閮介渶瑕佷竴涓猄tring瀵硅薄鏉ヨ〃绀恒�傝繖鏃讹紝搴旇鑰冭檻浣跨敤StringBuffer绫伙紝瀹冨厑璁镐慨鏀癸紝鑰屼笉鏄瘡涓笉鍚岀殑瀛楃涓查兘瑕佺敓鎴愪竴涓柊鐨勫璞°�傚苟涓旓紝杩欎袱绉嶇被鐨勫璞¤浆鎹㈠崄鍒嗗鏄撱��

鍚屾椂锛屾垜浠繕鍙互鐭ラ亾锛屽鏋滆浣跨敤鍐呭鐩稿悓鐨勫瓧绗︿覆锛屼笉蹇呮瘡娆¢兘new涓�涓猄tring銆備緥濡傛垜浠鍦ㄦ瀯閫犲櫒涓涓�涓悕鍙玸鐨凷tring寮曠敤鍙橀噺杩涜鍒濆鍖栵紝鎶婂畠璁剧疆涓哄垵濮嬪�硷紝搴斿綋杩欐牱鍋氾細

public class Demo {

private String s;

...

public Demo {

s = "Initial Value";

}

...

}

鑰岄潪

s = new String("Initial Value");

鍚庤�呮瘡娆¢兘浼氳皟鐢ㄦ瀯閫犲櫒锛岀敓鎴愭柊瀵硅薄锛屾�ц兘浣庝笅涓斿唴瀛樺紑閿�澶э紝骞朵笖娌℃湁鎰忎箟锛屽洜涓篠tring瀵硅薄涓嶅彲鏀瑰彉锛屾墍浠ュ浜庡唴瀹圭浉鍚岀殑瀛楃涓诧紝鍙涓�涓猄tring瀵硅薄鏉ヨ〃绀哄氨鍙互浜嗐�備篃灏辫锛屽娆¤皟鐢ㄤ笂闈㈢殑鏋勯�犲櫒鍒涘缓澶氫釜瀵硅薄锛屼粬浠殑String绫诲瀷灞炴�閮芥寚鍚戝悓涓�涓璞°��

涓婇潰鐨勭粨璁鸿繕鍩轰簬杩欐牱涓�涓簨瀹烇細瀵逛簬瀛楃涓插父閲忥紝濡傛灉鍐呭鐩稿悓锛孞ava璁や负瀹冧滑浠h〃鍚屼竴涓猄tring瀵硅薄銆傝�岀敤鍏抽敭瀛梟ew璋冪敤鏋勯�犲櫒锛屾�绘槸浼氬垱寤轰竴涓柊鐨勫璞★紝鏃犺鍐呭鏄惁鐩稿悓銆�

鑷充簬涓轰粈涔堣鎶奡tring绫昏璁℃垚涓嶅彲鍙樼被锛屾槸瀹冪殑鐢ㄩ�斿喅瀹氱殑銆傚叾瀹炰笉鍙猄tring锛屽緢澶欽ava鏍囧噯绫诲簱涓殑绫婚兘鏄笉鍙彉鐨勩�傚湪寮�鍙戜竴涓郴缁熺殑鏃跺�欙紝鎴戜滑鏈夋椂鍊欎篃闇�瑕佽璁′笉鍙彉绫伙紝鏉ヤ紶閫掍竴缁勭浉鍏崇殑鍊硷紝杩欎篃鏄潰鍚戝璞℃�濇兂鐨勪綋鐜般�備笉鍙彉绫绘湁涓�浜涗紭鐐癸紝姣斿鍥犱负瀹冪殑瀵硅薄鏄彧璇荤殑锛屾墍浠ュ绾跨▼骞跺彂璁块棶涔熶笉浼氭湁浠讳綍闂銆傚綋鐒朵篃鏈変竴浜涚己鐐癸紝姣斿姣忎釜涓嶅悓鐨勭姸鎬侀兘瑕佷竴涓璞℃潵浠h〃锛屽彲鑳戒細閫犳垚鎬ц兘涓婄殑闂銆傛墍浠ava鏍囧噯绫诲簱杩樻彁渚涗簡涓�涓彲鍙樼増鏈紝鍗� StringBuffer銆�

32銆佹槸鍚﹀彲浠ョ户鎵縎tring绫�?

String绫绘槸final绫绘晠涓嶅彲浠ョ户鎵裤��

33銆丼tring s = new String("xyz");鍒涘缓浜嗗嚑涓猄tring Object?浜岃�呬箣闂存湁浠�涔堝尯鍒紵

涓や釜鎴栦竴涓紝鈥漻yz鈥濆搴斾竴涓璞★紝杩欎釜瀵硅薄鏀惧湪瀛楃涓插父閲忕紦鍐插尯锛屽父閲忊�漻yz鈥濅笉绠″嚭鐜板灏戦亶锛岄兘鏄紦鍐插尯涓殑閭d竴涓�侼ew String姣忓啓涓�閬嶏紝灏卞垱寤轰竴涓柊鐨勫璞★紝瀹冧竴鍙ラ偅涓父閲忊�漻yz鈥濆璞$殑鍐呭鏉ュ垱寤哄嚭涓�涓柊String瀵硅薄銆傚鏋滀互鍓嶅氨鐢ㄨ繃鈥檟yz鈥欙紝杩欏彞浠h〃灏变笉浼氬垱寤衡�漻yz鈥濊嚜宸变簡锛岀洿鎺ヤ粠缂撳啿鍖烘嬁銆�

34銆丼tring鍜孲tringBuffer鐨勫尯鍒�

JAVA骞冲彴鎻愪緵浜嗕袱涓被锛歋tring鍜孲tringBuffer锛屽畠浠彲浠ュ偍瀛樺拰鎿嶄綔瀛楃涓诧紝鍗冲寘鍚涓瓧绗︾殑瀛楃鏁版嵁銆傝繖涓猄tring绫绘彁渚涗簡鏁板�间笉鍙敼鍙樼殑瀛楃涓层�傝�岃繖涓猄tringBuffer绫绘彁渚涚殑瀛楃涓茶繘琛屼慨鏀广�傚綋浣犵煡閬撳瓧绗︽暟鎹鏀瑰彉鐨勬椂鍊欎綘灏卞彲浠ヤ娇鐢⊿tringBuffer銆傚吀鍨嬪湴锛屼綘鍙互浣跨敤StringBuffers鏉ュ姩鎬佹瀯閫犲瓧绗︽暟鎹�傚彟澶栵紝String瀹炵幇浜唀quals鏂规硶锛宯ew String(鈥渁bc鈥�).equals(newString(鈥渁bc鈥�)鐨勭粨鏋滀负true,鑰孲tringBuffer娌℃湁瀹炵幇equals鏂规硶锛屾墍浠ワ紝new StringBuffer(鈥渁bc鈥�).equals(newStringBuffer(鈥渁bc鈥�)鐨勭粨鏋滀负false銆�

鎺ョ潃瑕佷妇涓�涓叿浣撶殑渚嬪瓙鏉ヨ鏄庯紝鎴戜滑瑕佹妸1鍒�100鐨勬墍鏈夋暟瀛楁嫾璧锋潵锛岀粍鎴愪竴涓覆銆�

StringBuffer sbf = new StringBuffer();

for(int i=0;i<100;i++)

{

sbf.append(i);

}

涓婇潰鐨勪唬鐮佹晥鐜囧緢楂橈紝鍥犱负鍙垱寤轰簡涓�涓猄tringBuffer瀵硅薄锛岃�屼笅闈㈢殑浠g爜鏁堢巼寰堜綆锛屽洜涓哄垱寤轰簡101涓璞°��

String str = new String();

for(int i=0;i<100;i++)

{

str = str + i;

}

鍦ㄨ涓よ�呭尯鍒椂锛屽簲鎶婂惊鐜殑娆℃暟鎼炴垚10000锛岀劧鍚庣敤endTime-beginTime鏉ユ瘮杈冧袱鑰呮墽琛岀殑鏃堕棿宸紓锛屾渶鍚庤繕瑕佽璁睸tringBuilder涓嶴tringBuffer鐨勫尯鍒��

String瑕嗙洊浜唀quals鏂规硶鍜宧ashCode鏂规硶锛岃�孲tringBuffer娌℃湁瑕嗙洊equals鏂规硶鍜宧ashCode鏂规硶锛屾墍浠ワ紝灏�StringBuffer瀵硅薄瀛樺偍杩汮ava闆嗗悎绫讳腑鏃朵細鍑虹幇闂銆�

35銆佸浣曟妸涓�娈甸�楀彿鍒嗗壊鐨勫瓧绗︿覆杞崲鎴愪竴涓暟缁�?

濡傛灉涓嶆煡jdk api锛屾垜寰堥毦鍐欏嚭鏉ワ紒鎴戝彲浠ヨ璇存垜鐨勬�濊矾锛�

1鐢ㄦ鍒欒〃杈惧紡锛屼唬鐮佸ぇ姒備负锛歋tring [] result = orgStr.split(鈥�,鈥�);

2鐢� StingTokenizer ,浠g爜涓猴細StringTokenizer 聽tokener = StringTokenizer(orgStr,鈥�,鈥�);

String [] result =new String[tokener .countTokens()];

Int i=0;

while(tokener.hasNext(){

result[i++]=toker.nextToken();

}

36銆佹暟缁勬湁娌℃湁length()杩欎釜鏂规硶? String鏈夋病鏈塴ength()杩欎釜鏂规硶锛�

鏁扮粍娌℃湁length()杩欎釜鏂规硶锛屾湁length鐨勫睘鎬с�係tring鏈夋湁length()杩欎釜鏂规硶銆�

37銆佷笅闈㈣繖鏉¤鍙ヤ竴鍏卞垱寤轰簡澶氬皯涓璞★細String s="a"+"b"+"c"+"d";

绛旓細瀵逛簬濡備笅浠g爜锛�

String s1 = "a";

String s2 = s1 + "b";

String s3 = "a" + "b";

System.out.println(s2 == "ab");

System.out.println(s3 == "ab");

绗竴鏉¤鍙ユ墦鍗扮殑缁撴灉涓篺alse锛岀浜屾潯璇彞鎵撳嵃鐨勭粨鏋滀负true锛岃繖璇存槑javac缂栬瘧鍙互瀵瑰瓧绗︿覆甯搁噺鐩存帴鐩稿姞鐨勮〃杈惧紡杩涜浼樺寲锛屼笉蹇呰绛夊埌杩愯鏈熷幓杩涜鍔犳硶杩愮畻澶勭悊锛岃�屾槸鍦ㄧ紪璇戞椂鍘绘帀鍏朵腑鐨勫姞鍙凤紝鐩存帴灏嗗叾缂栬瘧鎴愪竴涓繖浜涘父閲忕浉杩炵殑缁撴灉銆�

棰樼洰涓殑绗竴琛屼唬鐮佽缂栬瘧鍣ㄥ湪缂栬瘧鏃朵紭鍖栧悗锛岀浉褰撲簬鐩存帴瀹氫箟浜嗕竴涓�漚bcd鈥濈殑瀛楃涓诧紝鎵�浠ワ紝涓婇潰鐨勪唬鐮佸簲璇ュ彧鍒涘缓浜嗕竴涓猄tring瀵硅薄銆傚啓濡備笅涓よ浠g爜锛�

String s ="a" + "b" + "c" + "d";

System.out.println(s== "abcd");

鏈�缁堟墦鍗扮殑缁撴灉搴旇涓簍rue銆�

38銆乼ry {}閲屾湁涓�涓猺eturn璇彞锛岄偅涔堢揣璺熷湪杩欎釜try鍚庣殑finally {}閲岀殑code浼氫笉浼氳鎵ц锛屼粈涔堟椂鍊欒鎵ц锛屽湪return鍓嶈繕鏄悗?

涔熻浣犵殑绛旀鏄湪return涔嬪墠锛屼絾寰�鏇寸粏鍦拌锛屾垜鐨勭瓟妗堟槸鍦╮eturn涓棿鎵ц锛岃鐪嬩笅闈㈢▼搴忎唬鐮佺殑杩愯缁撴灉锛�

publicclassTest {

/**

*@paramargs add by zxx ,Dec 9, 2008

*/

public static voidmain(String[] args) {

//TODOAuto-generated method stub

System.out.println(newTest().test());;

}

staticinttest()

{

intx = 1;

try

{

returnx;

}

finally

{

++x;

}

}

}

---------鎵ц缁撴灉 ---------

1

杩愯缁撴灉鏄�1锛屼负浠�涔堝憿锛熶富鍑芥暟璋冪敤瀛愬嚱鏁板苟寰楀埌缁撴灉鐨勮繃绋嬶紝濂芥瘮涓诲嚱鏁板噯澶囦竴涓┖缃愬瓙锛屽綋瀛愬嚱鏁拌杩斿洖缁撴灉鏃讹紝鍏堟妸缁撴灉鏀惧湪缃愬瓙閲岋紝鐒跺悗鍐嶅皢绋嬪簭閫昏緫杩斿洖鍒颁富鍑芥暟銆傛墍璋撹繑鍥烇紝灏辨槸瀛愬嚱鏁拌锛屾垜涓嶈繍琛屼簡锛屼綘涓诲嚱鏁扮户缁繍琛屽惂锛岃繖娌′粈涔堢粨鏋滃彲瑷�锛岀粨鏋滄槸鍦ㄨ杩欒瘽涔嬪墠鏀捐繘缃愬瓙閲岀殑銆�

39銆佷笅闈㈢殑绋嬪簭浠g爜杈撳嚭鐨勭粨鏋滄槸澶氬皯锛�

public classsmallT

{

public staticvoid 聽main(String args[])

{

smallT t 聽= new smallT();

int 聽b = 聽t.get();

System.out.println(b);

}

public int 聽get()

{

try

{

return1 ;

}

finally

{

return2 ;

}

}

}

杩斿洖鐨勭粨鏋滄槸2銆�

鎴戝彲浠ラ�氳繃涓嬮潰涓�涓緥瀛愮▼搴忔潵甯姪鎴戣В閲婅繖涓瓟妗堬紝浠庝笅闈緥瀛愮殑杩愯缁撴灉涓彲浠ュ彂鐜帮紝try涓殑return璇彞璋冪敤鐨勫嚱鏁板厛浜巉inally涓皟鐢ㄧ殑鍑芥暟鎵ц锛屼篃灏辨槸璇磖eturn璇彞鍏堟墽琛岋紝finally璇彞鍚庢墽琛岋紝鎵�浠ワ紝杩斿洖鐨勭粨鏋滄槸2銆俁eturn骞朵笉鏄鍑芥暟椹笂杩斿洖锛岃�屾槸return璇彞鎵ц鍚庯紝灏嗘妸杩斿洖缁撴灉鏀剧疆杩涘嚱鏁版爤涓紝姝ゆ椂鍑芥暟骞朵笉鏄┈涓婅繑鍥烇紝瀹冭鎵цfinally璇彞鍚庢墠鐪熸寮�濮嬭繑鍥炪��

鍦ㄨ瑙g瓟妗堟椂鍙互鐢ㄤ笅闈㈢殑绋嬪簭鏉ュ府鍔╁垎鏋愶細

publicclassTest {

/**

*@paramargs add by zxx ,Dec 9, 2008

*/

public static voidmain(String[] args) {

//TODOAuto-generated method stub

System.out.println(newTest().test());;

}

inttest()

{

try

{

returnfunc1();

}

finally

{

returnfunc2();

}

}

intfunc1()

{

System.out.println("func1");

return1;

}

intfunc2()

{

System.out.println("func2");

return2;

}

}

-----------鎵ц缁撴灉-----------------

func1

func2

2

缁撹锛歠inally涓殑浠g爜姣攔eturn鍜宐reak璇彞鍚庢墽琛�

40銆乫inal, finally, finalize鐨勫尯鍒��

final 鐢ㄤ簬澹版槑灞炴�э紝鏂规硶鍜岀被锛屽垎鍒〃绀哄睘鎬т笉鍙彉锛屾柟娉曚笉鍙鐩栵紝绫讳笉鍙户鎵裤��

鍐呴儴绫昏璁块棶灞�閮ㄥ彉閲忥紝灞�閮ㄥ彉閲忓繀椤诲畾涔夋垚final绫诲瀷锛屼緥濡傦紝涓�娈典唬鐮佲�︹��

finally鏄紓甯稿鐞嗚鍙ョ粨鏋勭殑涓�閮ㄥ垎锛岃〃绀烘�绘槸鎵ц銆�

finalize鏄疧bject绫荤殑涓�涓柟娉曪紝鍦ㄥ瀮鍦炬敹闆嗗櫒鎵ц鐨勬椂鍊欎細璋冪敤琚洖鏀跺璞$殑姝ゆ柟娉曪紝鍙互瑕嗙洊姝ゆ柟娉曟彁渚涘瀮鍦炬敹闆嗘椂鐨勫叾浠栬祫婧愬洖鏀讹紝渚嬪鍏抽棴鏂囦欢绛夈�侸VM涓嶄繚璇佹鏂规硶鎬昏璋冪敤

41銆佽繍琛屾椂寮傚父涓庝竴鑸紓甯告湁浣曞紓鍚岋紵

寮傚父琛ㄧず绋嬪簭杩愯杩囩▼涓彲鑳藉嚭鐜扮殑闈炴甯哥姸鎬侊紝杩愯鏃跺紓甯歌〃绀鸿櫄鎷熸満鐨勯�氬父鎿嶄綔涓彲鑳介亣鍒扮殑寮傚父锛屾槸涓�绉嶅父瑙佽繍琛岄敊璇�俲ava缂栬瘧鍣ㄨ姹傛柟娉曞繀椤诲0鏄庢姏鍑哄彲鑳藉彂鐢熺殑闈炶繍琛屾椂寮傚父锛屼絾鏄苟涓嶈姹傚繀椤诲0鏄庢姏鍑烘湭琚崟鑾风殑杩愯鏃跺紓甯搞��

42銆乪rror鍜宔xception鏈変粈涔堝尯鍒�?

error 琛ㄧず鎭㈠涓嶆槸涓嶅彲鑳戒絾寰堝洶闅剧殑鎯呭喌涓嬬殑涓�绉嶄弗閲嶉棶棰樸�傛瘮濡傝鍐呭瓨婧㈠嚭銆備笉鍙兘鎸囨湜绋嬪簭鑳藉鐞嗚繖鏍风殑鎯呭喌銆� exception琛ㄧず涓�绉嶈璁℃垨瀹炵幇闂銆備篃灏辨槸璇达紝瀹冭〃绀哄鏋滅▼搴忚繍琛屾甯革紝浠庝笉浼氬彂鐢熺殑鎯呭喌銆�

43銆丣ava涓殑寮傚父澶勭悊鏈哄埗鐨勭畝鍗曞師鐞嗗拰搴旂敤銆�

寮傚父鏄寚java绋嬪簭杩愯鏃讹紙闈炵紪璇戯級鎵�鍙戠敓鐨勯潪姝e父鎯呭喌鎴栭敊璇紝涓庣幇瀹炵敓娲讳腑鐨勪簨浠跺緢鐩镐技锛岀幇瀹炵敓娲讳腑鐨勪簨浠跺彲浠ュ寘鍚簨浠跺彂鐢熺殑鏃堕棿銆佸湴鐐广�佷汉鐗┿�佹儏鑺傜瓑淇℃伅锛屽彲浠ョ敤涓�涓璞℃潵琛ㄧず锛孞ava浣跨敤闈㈠悜瀵硅薄鐨勬柟寮忔潵澶勭悊寮傚父锛屽畠鎶婄▼搴忎腑鍙戠敓鐨勬瘡涓紓甯镐篃閮藉垎鍒皝瑁呭埌涓�涓璞℃潵琛ㄧず鐨勶紝璇ュ璞′腑鍖呭惈鏈夊紓甯哥殑淇℃伅銆�

Java瀵瑰紓甯歌繘琛屼簡鍒嗙被锛屼笉鍚岀被鍨嬬殑寮傚父鍒嗗埆鐢ㄤ笉鍚岀殑Java绫昏〃绀猴紝鎵�鏈夊紓甯哥殑鏍圭被涓簀ava.lang.Throwable锛孴hrowable涓嬮潰鍙堟淳鐢熶簡涓や釜瀛愮被锛欵rror鍜孍xception锛孍rror琛ㄧず搴旂敤绋嬪簭鏈韩鏃犳硶鍏嬫湇鍜屾仮澶嶇殑涓�绉嶄弗閲嶉棶棰橈紝绋嬪簭鍙湁姝荤殑浠戒簡锛屼緥濡傦紝璇村唴瀛樻孩鍑哄拰绾跨▼姝婚攣绛夌郴缁熼棶棰樸�侲xception琛ㄧず绋嬪簭杩樿兘澶熷厠鏈嶅拰鎭㈠鐨勯棶棰橈紝鍏朵腑鍙堝垎涓虹郴缁熷紓甯稿拰鏅�氬紓甯革紝绯荤粺寮傚父鏄蒋浠舵湰韬己闄锋墍瀵艰嚧鐨勯棶棰橈紝涔熷氨鏄蒋浠跺紑鍙戜汉鍛樿�冭檻涓嶅懆鎵�瀵艰嚧鐨勯棶棰橈紝杞欢浣跨敤鑰呮棤娉曞厠鏈嶅拰鎭㈠杩欑闂锛屼絾鍦ㄨ繖绉嶉棶棰樹笅杩樺彲浠ヨ杞欢绯荤粺缁х画杩愯鎴栬�呰杞欢姝绘帀锛屼緥濡傦紝鏁扮粍鑴氭湰瓒婄晫锛圓rrayIndexOutOfBoundsException锛夛紝绌烘寚閽堝紓甯革紙NullPointerException锛夈�佺被杞崲寮傚父锛圕lassCastException锛夛紱鏅�氬紓甯告槸杩愯鐜鐨勫彉鍖栨垨寮傚父鎵�瀵艰嚧鐨勯棶棰橈紝鏄敤鎴疯兘澶熷厠鏈嶇殑闂锛屼緥濡傦紝缃戠粶鏂嚎锛岀‖鐩樼┖闂翠笉澶燂紝鍙戠敓杩欐牱鐨勫紓甯稿悗锛岀▼搴忎笉搴旇姝绘帀銆�

java涓虹郴缁熷紓甯稿拰鏅�氬紓甯告彁渚涗簡涓嶅悓鐨勮В鍐虫柟妗堬紝缂栬瘧鍣ㄥ己鍒舵櫘閫氬紓甯稿繀椤籺ry..catch澶勭悊鎴栫敤throws澹版槑缁х画鎶涚粰涓婂眰璋冪敤鏂规硶澶勭悊锛屾墍浠ユ櫘閫氬紓甯镐篃绉颁负checked寮傚父锛岃�岀郴缁熷紓甯稿彲浠ュ鐞嗕篃鍙互涓嶅鐞嗭紝鎵�浠ワ紝缂栬瘧鍣ㄤ笉寮哄埗鐢╰ry..catch澶勭悊鎴栫敤throws澹版槑锛屾墍浠ョ郴缁熷紓甯镐篃绉颁负unchecked寮傚父銆�

鎻愮ず绛旈鑰咃細灏辨寜鐓т笁涓骇鍒幓鎬濊�冿細铏氭嫙鏈哄繀椤诲畷鏈虹殑閿欒锛岀▼搴忓彲浠ユ鎺変篃鍙互涓嶆鎺夌殑閿欒锛岀▼搴忎笉搴旇姝绘帀鐨勯敊璇紱

44銆佽鍐欏嚭浣犳渶甯歌鍒扮殑5涓猺untime exception銆�

杩欓亾棰樹富瑕佽�冧綘鐨勪唬鐮侀噺鍒板簳澶氬ぇ锛屽鏋滀綘闀挎湡鍐欎唬鐮佺殑锛屽簲璇ョ粡甯搁兘鐪嬪埌杩囦竴浜涚郴缁熸柟闈㈢殑寮傚父锛屼綘涓嶄竴瀹氱湡瑕佸洖绛斿嚭5涓叿浣撶殑绯荤粺寮傚父锛屼絾浣犺鑳藉璇村嚭浠�涔堟槸绯荤粺寮傚父锛屼互鍙婂嚑涓郴缁熷紓甯稿氨鍙互浜嗭紝褰撶劧锛岃繖浜涘紓甯稿畬鍏ㄧ敤鍏惰嫳鏂囧悕绉版潵鍐欐槸鏈�濂界殑锛屽鏋滃疄鍦ㄥ啓涓嶅嚭锛岄偅灏辩敤涓枃鍚э紝鏈夋�绘瘮娌℃湁寮猴紒

鎵�璋撶郴缁熷紓甯革紝灏辨槸鈥�..锛屽畠浠兘鏄疪untimeException鐨勫瓙绫伙紝鍦╦dk doc涓煡RuntimeException绫伙紝灏卞彲浠ョ湅鍒板叾鎵�鏈夌殑瀛愮被鍒楄〃锛屼篃灏辨槸鐪嬪埌浜嗘墍鏈夌殑绯荤粺寮傚父銆傛垜姣旇緝鏈夊嵃璞$殑绯荤粺寮傚父鏈夛細NullPointerException銆丄rrayIndexOutOfBoundsException銆丆lassCastException銆�

45銆丣AVA璇█濡備綍杩涜寮傚父澶勭悊锛屽叧閿瓧锛歵hrows,throw,try,catch,finally鍒嗗埆浠h〃浠�涔堟剰涔夛紵鍦╰ry鍧椾腑鍙互鎶涘嚭寮傚父鍚楋紵

46銆乯ava涓湁鍑犵鏂规硶鍙互瀹炵幇涓�涓嚎绋嬶紵鐢ㄤ粈涔堝叧閿瓧淇グ鍚屾鏂规硶? stop()鍜宻uspend()鏂规硶涓轰綍涓嶆帹鑽愪娇鐢紵

java5浠ュ墠锛屾湁濡備笅涓ょ锛�

绗竴绉嶏細

new Thread(){}.start();杩欒〃绀鸿皟鐢═hread瀛愮被瀵硅薄鐨剅un鏂规硶锛宯ew Thread(){}琛ㄧず涓�涓猅hread鐨勫尶鍚嶅瓙绫荤殑瀹炰緥瀵硅薄锛屽瓙绫诲姞涓妑un鏂规硶鍚庣殑浠g爜濡備笅锛�

new Thread(){

public void run(){

}

}.start();

绗簩绉嶏細

new Thread(new Runnable(){}).start();杩欒〃绀鸿皟鐢═hread瀵硅薄鎺ュ彈鐨凴unnable瀵硅薄鐨剅un鏂规硶锛宯ew Runnable(){}琛ㄧず涓�涓猂unnable鐨勫尶鍚嶅瓙绫荤殑瀹炰緥瀵硅薄,runnable鐨勫瓙绫诲姞涓妑un鏂规硶鍚庣殑浠g爜濡備笅锛�

new Thread(new Runnable(){

public voidrun(){

}

}

).start();

浠巎ava5寮�濮嬶紝杩樻湁濡備笅涓�浜涚嚎绋嬫睜鍒涘缓澶氱嚎绋嬬殑鏂瑰紡锛�

ExecutorService pool = Executors.newFixedThreadPool(3)

for(int i=0;i<10;i++)

{

pool.execute(newRunable(){public void run(){}});

}

Executors.newCachedThreadPool().execute(new Runable(){publicvoid run(){}});

Executors.newSingleThreadExecutor().execute(new Runable(){publicvoid run(){}});

鏈変袱绉嶅疄鐜版柟娉曪紝鍒嗗埆浣跨敤new Thread()鍜宯ew Thread(runnable)褰㈠紡锛岀涓�绉嶇洿鎺ヨ皟鐢╰hread鐨剅un鏂规硶锛屾墍浠ワ紝鎴戜滑寰�寰�浣跨敤Thread瀛愮被锛屽嵆new SubThread()銆傜浜岀璋冪敤runnable鐨剅un鏂规硶銆�

鏈変袱绉嶅疄鐜版柟娉曪紝鍒嗗埆鏄户鎵縏hread绫讳笌瀹炵幇Runnable鎺ュ彛

鐢╯ynchronized鍏抽敭瀛椾慨楗板悓姝ユ柟娉�

鍙嶅浣跨敤stop()锛屾槸鍥犱负瀹冧笉瀹夊叏銆傚畠浼氳В闄ょ敱绾跨▼鑾峰彇鐨勬墍鏈夐攣瀹氾紝鑰屼笖濡傛灉瀵硅薄澶勪簬涓�绉嶄笉杩炶疮鐘舵�侊紝閭d箞鍏朵粬绾跨▼鑳藉湪閭g鐘舵�佷笅妫�鏌ュ拰淇敼瀹冧滑銆傜粨鏋滃緢闅炬鏌ュ嚭鐪熸鐨勯棶棰樻墍鍦ㄣ�俿uspend()鏂规硶瀹规槗鍙戠敓姝婚攣銆傝皟鐢╯uspend()鐨勬椂鍊欙紝鐩爣绾跨▼浼氬仠涓嬫潵锛屼絾鍗翠粛鐒舵寔鏈夊湪杩欎箣鍓嶈幏寰楃殑閿佸畾銆傛鏃讹紝鍏朵粬浠讳綍绾跨▼閮戒笉鑳借闂攣瀹氱殑璧勬簮锛岄櫎闈炶"鎸傝捣"鐨勭嚎绋嬫仮澶嶈繍琛屻�傚浠讳綍绾跨▼鏉ヨ锛屽鏋滃畠浠兂鎭㈠鐩爣绾跨▼锛屽悓鏃跺張璇曞浘浣跨敤浠讳綍涓�涓攣瀹氱殑璧勬簮锛屽氨浼氶�犳垚姝婚攣銆傛墍浠ヤ笉搴旇浣跨敤suspend()锛岃�屽簲鍦ㄨ嚜宸辩殑Thread绫讳腑缃叆涓�涓爣蹇楋紝鎸囧嚭绾跨▼搴旇娲诲姩杩樻槸鎸傝捣銆傝嫢鏍囧織鎸囧嚭绾跨▼搴旇鎸傝捣锛屼究鐢╳ait()鍛藉叾杩涘叆绛夊緟鐘舵�併�傝嫢鏍囧織鎸囧嚭绾跨▼搴斿綋鎭㈠锛屽垯鐢ㄤ竴涓猲otify()閲嶆柊鍚姩绾跨▼銆�

47銆乻leep()鍜� wait()鏈変粈涔堝尯鍒�?

锛堢綉涓婄殑绛旀锛歴leep鏄嚎绋嬬被锛圱hread锛夌殑鏂规硶锛屽鑷存绾跨▼鏆傚仠鎵ц鎸囧畾鏃堕棿锛岀粰鎵ц鏈轰細缁欏叾浠栫嚎绋嬶紝浣嗘槸鐩戞帶鐘舵�佷緷鐒朵繚鎸侊紝鍒版椂鍚庝細鑷姩鎭㈠銆傝皟鐢╯leep涓嶄細閲婃斁瀵硅薄閿併�� wait鏄疧bject绫荤殑鏂规硶锛屽姝ゅ璞¤皟鐢╳ait鏂规硶瀵艰嚧鏈嚎绋嬫斁寮冨璞¢攣锛岃繘鍏ョ瓑寰呮瀵硅薄鐨勭瓑寰呴攣瀹氭睜锛屽彧鏈夐拡瀵规瀵硅薄鍙戝嚭notify鏂规硶锛堟垨notifyAll锛夊悗鏈嚎绋嬫墠杩涘叆瀵硅薄閿佸畾姹犲噯澶囪幏寰楀璞¢攣杩涘叆杩愯鐘舵�併�傦級

sleep灏辨槸姝e湪鎵ц鐨勭嚎绋嬩富鍔ㄨ鍑篶pu锛宑pu鍘绘墽琛屽叾浠栫嚎绋嬶紝鍦╯leep鎸囧畾鐨勬椂闂磋繃鍚庯紝cpu鎵嶄細鍥炲埌杩欎釜绾跨▼涓婄户缁線涓嬫墽琛岋紝濡傛灉褰撳墠绾跨▼杩涘叆浜嗗悓姝ラ攣锛宻leep鏂规硶骞朵笉浼氶噴鏀鹃攣锛屽嵆浣垮綋鍓嶇嚎绋嬩娇鐢╯leep鏂规硶璁╁嚭浜哻pu锛屼絾鍏朵粬琚悓姝ラ攣鎸′綇浜嗙殑绾跨▼涔熸棤娉曞緱鍒版墽琛屻�倃ait鏄寚鍦ㄤ竴涓凡缁忚繘鍏ヤ簡鍚屾閿佺殑绾跨▼鍐咃紝璁╄嚜宸辨殏鏃惰鍑哄悓姝ラ攣锛屼互渚垮叾浠栨鍦ㄧ瓑寰呮閿佺殑绾跨▼鍙互寰楀埌鍚屾閿佸苟杩愯锛屽彧鏈夊叾浠栫嚎绋嬭皟鐢ㄤ簡notify鏂规硶锛坣otify骞朵笉閲婃斁閿侊紝鍙槸鍛婅瘔璋冪敤杩噖ait鏂规硶鐨勭嚎绋嬪彲浠ュ幓鍙備笌鑾峰緱閿佺殑绔炰簤浜嗭紝浣嗕笉鏄┈涓婂緱鍒伴攣锛屽洜涓洪攣杩樺湪鍒汉鎵嬮噷锛屽埆浜鸿繕娌¢噴鏀俱�傚鏋渘otify鏂规硶鍚庨潰鐨勪唬鐮佽繕鏈夊緢澶氾紝闇�瑕佽繖浜涗唬鐮佹墽琛屽畬鍚庢墠浼氶噴鏀鹃攣锛屽彲浠ュ湪notfiy鏂规硶鍚庡鍔犱竴涓瓑寰呭拰涓�浜涗唬鐮侊紝鐪嬬湅鏁堟灉锛夛紝璋冪敤wait鏂规硶鐨勭嚎绋嬪氨浼氳В闄ait鐘舵�佸拰绋嬪簭鍙互鍐嶆寰楀埌閿佸悗缁х画鍚戜笅杩愯銆傚浜巜ait鐨勮瑙d竴瀹氳閰嶅悎渚嬪瓙浠g爜鏉ヨ鏄庯紝鎵嶆樉寰楄嚜宸辩湡鏄庣櫧銆�

packagecom.huawei.interview;

publicclassMultiThread {

/**

*@paramargs

*/

public static voidmain(String[] args) {

//TODOAuto-generated method stub

newThread(newThread1()).start();

try{

Thread.sleep(10);

}catch(InterruptedException e) {

//TODOAuto-generated catchblock

e.printStackTrace();

}

newThread(newThread2()).start();

}

private static classThread1implementsRunnable

{

@Override

public voidrun() {

//TODOAuto-generated methodstub

//鐢变簬杩欓噷鐨凾hread1鍜屼笅闈㈢殑Thread2鍐呴儴run鏂规硶瑕佺敤鍚屼竴瀵硅薄浣滀负鐩戣鍣紝鎴戜滑杩欓噷涓嶈兘鐢╰his锛屽洜涓哄湪Thread2閲岄潰鐨則his鍜岃繖涓猅hread1鐨則his涓嶆槸鍚屼竴涓璞°�傛垜浠敤MultiThread.class杩欎釜瀛楄妭鐮佸璞★紝褰撳墠铏氭嫙鏈洪噷寮曠敤杩欎釜鍙橀噺鏃讹紝鎸囧悜鐨勯兘鏄悓涓�涓璞°��

synchronized(MultiThread.class){

System.out.println("enterthread1...");

System.out.println("thread1is waiting");

try{

//閲婃斁閿佹湁涓ょ鏂瑰紡锛岀涓�绉嶆柟寮忔槸绋嬪簭鑷劧绂诲紑鐩戣鍣ㄧ殑鑼冨洿锛屼篃灏辨槸绂诲紑浜唖ynchronized鍏抽敭瀛楃杈栫殑浠g爜鑼冨洿锛屽彟涓�绉嶆柟寮忓氨鏄湪synchronized鍏抽敭瀛楃杈栫殑浠g爜鍐呴儴璋冪敤鐩戣鍣ㄥ璞$殑wait鏂规硶銆傝繖閲岋紝浣跨敤wait鏂规硶閲婃斁閿併��

MultiThread.class.wait();

}catch(InterruptedException e) {

//TODOAuto-generatedcatch block

e.printStackTrace();

}

System.out.println("thread1is going on...");

System.out.println("thread1is being over!");

}

}

}

private static classThread2implementsRunnable

{

@Override

public voidrun() {

//TODOAuto-generated methodstub

synchronized(MultiThread.class){

System.out.println("enterthread2...");

System.out.println("thread2notify other thread can release wait status..");

//鐢变簬notify鏂规硶骞朵笉閲婃斁閿侊紝鍗充娇thread2璋冪敤涓嬮潰鐨剆leep鏂规硶浼戞伅浜�10姣锛屼絾thread1浠嶇劧涓嶄細鎵ц锛屽洜涓簍hread2娌℃湁閲婃斁閿侊紝鎵�浠hread1鏃犳硶寰椾笉鍒伴攣銆�

MultiThread.class.notify();

System.out.println("thread2is sleeping ten millisecond...");

try{

Thread.sleep(10);

}catch(InterruptedExceptione) {

//TODOAuto-generatedcatch block

e.printStackTrace();

}

System.out.println("thread2is going on...");

System.out.println("thread2is being over!");

}

}

}

}

48銆佸悓姝ュ拰寮傛鏈変綍寮傚悓锛屽湪浠�涔堟儏鍐典笅鍒嗗埆浣跨敤浠栦滑锛熶妇渚嬭鏄庛��

濡傛灉鏁版嵁灏嗗湪绾跨▼闂村叡浜�備緥濡傛鍦ㄥ啓鐨勬暟鎹互鍚庡彲鑳借鍙︿竴涓嚎绋嬭鍒帮紝鎴栬�呮鍦ㄨ鐨勬暟鎹彲鑳藉凡缁忚鍙︿竴涓嚎绋嬪啓杩囦簡锛岄偅涔堣繖浜涙暟鎹氨鏄叡浜暟鎹紝蹇呴』杩涜鍚屾瀛樺彇銆�

褰撳簲鐢ㄧ▼搴忓湪瀵硅薄涓婅皟鐢ㄤ簡涓�涓渶瑕佽姳璐瑰緢闀挎椂闂存潵鎵ц鐨勬柟娉曪紝骞朵笖涓嶅笇鏈涜绋嬪簭绛夊緟鏂规硶鐨勮繑鍥炴椂锛屽氨搴旇浣跨敤寮傛缂栫▼锛屽湪寰堝鎯呭喌涓嬮噰鐢ㄥ紓姝ラ�斿緞寰�寰�鏇存湁鏁堢巼銆�

49. 涓嬮潰涓や釜鏂规硶鍚屾鍚楋紵锛堣嚜宸卞彂鏄庯級

class Test

{

synchronizedstatic voidsayHello3()

{

}

synchronizedvoidgetX(){}

}

50銆佸绾跨▼鏈夊嚑绉嶅疄鐜版柟娉�?鍚屾鏈夊嚑绉嶅疄鐜版柟娉�?

澶氱嚎绋嬫湁涓ょ瀹炵幇鏂规硶锛屽垎鍒槸缁ф壙Thread绫讳笌瀹炵幇Runnable鎺ュ彛

鍚屾鐨勫疄鐜版柟闈㈡湁涓ょ锛屽垎鍒槸synchronized,wait涓巒otify

wait():浣夸竴涓嚎绋嬪浜庣瓑寰呯姸鎬侊紝骞朵笖閲婃斁鎵�鎸佹湁鐨勫璞$殑lock銆�

sleep():浣夸竴涓鍦ㄨ繍琛岀殑绾跨▼澶勪簬鐫$湢鐘舵�侊紝鏄竴涓潤鎬佹柟娉曪紝璋冪敤姝ゆ柟娉曡鎹曟崏InterruptedException寮傚父銆�

notify():鍞ら啋涓�涓浜庣瓑寰呯姸鎬佺殑绾跨▼锛屾敞鎰忕殑鏄湪璋冪敤姝ゆ柟娉曠殑鏃跺�欙紝骞朵笉鑳界‘鍒囩殑鍞ら啋鏌愪竴涓瓑寰呯姸鎬佺殑绾跨▼锛岃�屾槸鐢盝VM纭畾鍞ら啋鍝釜绾跨▼锛岃�屼笖涓嶆槸鎸変紭鍏堢骇銆�

Allnotity():鍞ら啋鎵�鏈夊鍏ョ瓑寰呯姸鎬佺殑绾跨▼锛屾敞鎰忓苟涓嶆槸缁欐墍鏈夊敜閱掔嚎绋嬩竴涓璞$殑閿侊紝鑰屾槸璁╁畠浠珵浜夈��

51銆佸惎鍔ㄤ竴涓嚎绋嬫槸鐢╮un()杩樻槸start()? .

鍚姩涓�涓嚎绋嬫槸璋冪敤start()鏂规硶锛屼娇绾跨▼灏辩华鐘舵�侊紝浠ュ悗鍙互琚皟搴︿负杩愯鐘舵�侊紝涓�涓嚎绋嬪繀椤诲叧鑱斾竴浜涘叿浣撶殑鎵ц浠g爜锛宺un()鏂规硶鏄绾跨▼鎵�鍏宠仈鐨勬墽琛屼唬鐮併��

52銆佸綋涓�涓嚎绋嬭繘鍏ヤ竴涓璞$殑涓�涓猻ynchronized鏂规硶鍚庯紝鍏跺畠绾跨▼鏄惁鍙�杩涘叆姝ゅ璞$殑鍏跺畠鏂规硶?

鍒嗗嚑绉嶆儏鍐碉細

1.鍏朵粬鏂规硶鍓嶆槸鍚﹀姞浜唖ynchronized鍏抽敭瀛楋紝濡傛灉娌″姞锛屽垯鑳姐��

2.濡傛灉杩欎釜鏂规硶鍐呴儴璋冪敤浜唚ait锛屽垯鍙互杩涘叆鍏朵粬synchronized鏂规硶銆�

3.濡傛灉鍏朵粬涓柟娉曢兘鍔犱簡synchronized鍏抽敭瀛楋紝骞朵笖鍐呴儴娌℃湁璋冪敤wait锛屽垯涓嶈兘銆�

4.濡傛灉鍏朵粬鏂规硶鏄痵tatic锛屽畠鐢ㄧ殑鍚屾閿佹槸褰撳墠绫荤殑瀛楄妭鐮侊紝涓庨潪闈欐�佺殑鏂规硶涓嶈兘鍚屾锛屽洜涓洪潪闈欐�佺殑鏂规硶鐢ㄧ殑鏄痶his銆�

53銆佺嚎绋嬬殑鍩烘湰姒傚康銆佺嚎绋嬬殑鍩烘湰鐘舵�佷互鍙婄姸鎬佷箣闂寸殑鍏崇郴

涓�涓▼搴忎腑鍙互鏈夊鏉℃墽琛岀嚎绱㈠悓鏃舵墽琛岋紝涓�涓嚎绋嬪氨鏄▼搴忎腑鐨勪竴鏉℃墽琛岀嚎绱紝姣忎釜绾跨▼涓婇兘鍏宠仈鏈夎鎵ц鐨勪唬鐮侊紝鍗冲彲浠ユ湁澶氭绋嬪簭浠g爜鍚屾椂杩愯锛屾瘡涓▼搴忚嚦灏戦兘鏈変竴涓嚎绋嬶紝鍗砿ain鏂规硶鎵ц鐨勯偅涓嚎绋嬨�傚鏋滃彧鏄竴涓猚pu锛屽畠鎬庝箞鑳藉鍚屾椂鎵ц澶氭绋嬪簭鍛紵杩欐槸浠庡畯瑙備笂鏉ョ湅鐨勶紝cpu涓�浼氭墽琛宎绾跨储锛屼竴浼氭墽琛宐绾跨储锛屽垏鎹㈡椂闂村緢蹇紝缁欎汉鐨勬劅瑙夋槸a,b鍦ㄥ悓鏃舵墽琛岋紝濂芥瘮澶у鍦ㄥ悓涓�涓姙鍏涓婄綉锛屽彧鏈変竴鏉¢摼鎺ュ埌澶栭儴缃戠嚎锛屽叾瀹烇紝杩欐潯缃戠嚎涓�浼氫负a浼犳暟鎹紝涓�浼氫负b浼犳暟鎹紝鐢变簬鍒囨崲鏃堕棿寰堢煭鏆傦紝鎵�浠ワ紝澶у鎰熻閮藉湪鍚屾椂涓婄綉銆�

鐘舵�侊細灏辩华锛岃繍琛岋紝synchronize闃诲锛寃ait鍜宻leep鎸傝捣锛岀粨鏉熴�倃ait蹇呴』鍦╯ynchronized鍐呴儴璋冪敤銆�

璋冪敤绾跨▼鐨剆tart鏂规硶鍚庣嚎绋嬭繘鍏ュ氨缁姸鎬侊紝绾跨▼璋冨害绯荤粺灏嗗氨缁姸鎬佺殑绾跨▼杞负杩愯鐘舵�侊紝閬囧埌synchronized璇彞鏃讹紝鐢辫繍琛岀姸鎬佽浆涓洪樆濉烇紝褰搒ynchronized鑾峰緱閿佸悗锛岀敱闃诲杞负杩愯锛屽湪杩欑鎯呭喌鍙互璋冪敤wait鏂规硶杞负鎸傝捣鐘舵�侊紝褰撶嚎绋嬪叧鑱旂殑浠g爜鎵ц瀹屽悗锛岀嚎绋嬪彉涓虹粨鏉熺姸鎬併��

54銆佺畝杩皊ynchronized鍜宩ava.util.concurrent.locks.Lock鐨勫紓鍚岋紵

涓昏鐩稿悓鐐癸細Lock鑳藉畬鎴恠ynchronized鎵�瀹炵幇鐨勬墍鏈夊姛鑳�

涓昏涓嶅悓鐐癸細Lock鏈夋瘮synchronized鏇寸簿纭殑绾跨▼璇箟鍜屾洿濂界殑鎬ц兘銆俿ynchronized浼氳嚜鍔ㄩ噴鏀鹃攣锛岃�孡ock涓�瀹氳姹傜▼搴忓憳鎵嬪伐閲婃斁锛屽苟涓斿繀椤诲湪finally浠庡彞涓噴鏀俱�侺ock杩樻湁鏇村己澶х殑鍔熻兘锛屼緥濡傦紝瀹冪殑tryLock鏂规硶鍙互闈為樆濉炴柟寮忓幓鎷块攣銆�

涓句緥璇存槑锛堝涓嬮潰鐨勯鐢╨ock杩涜浜嗘敼鍐欙級锛�

packagecom.huawei.interview;

importjava.util.concurrent.locks.Lock;

importjava.util.concurrent.locks.ReentrantLock;

publicclassThreadTest {

/**

*@paramargs

*/

private intj;

privateLock lock =newReentrantLock();

public static voidmain(String[] args) {

//TODOAuto-generated method stub

ThreadTest tt =newThreadTest();

for(inti=0;i<2;i++)

{

newThread(tt.newAdder()).start();

newThread(tt.newSubtractor()).start();

}

}

private classSubtractorimplementsRunnable

{

@Override

public voidrun() {

//TODOAuto-generated methodstub

while(true)

{

/*synchronized (ThreadTest.this) {

System.out.println("j--="+ j--);

//杩欓噷鎶涘紓甯镐簡锛岄攣鑳介噴鏀惧悧锛�

}*/

lock.lock();

try

{

System.out.println("j--="+ j--);

}finally

{

lock.unlock();

}

}

}

}

private classAdderimplementsRunnable

{

@Override

public voidrun() {

//TODOAuto-generated methodstub

while(true)

{

/*synchronized (ThreadTest.this) {

System.out.println("j++="+ j++);

}*/

lock.lock();

try

{

System.out.println("j++="+ j++);

}finally

{

lock.unlock();

}

}

}

}

}

55銆佽璁�4涓嚎绋嬶紝鍏朵腑涓や釜绾跨▼姣忔瀵筳澧炲姞1锛屽彟澶栦袱涓嚎绋嬪j姣忔鍑忓皯1銆傚啓鍑虹▼搴忋��

浠ヤ笅绋嬪簭浣跨敤鍐呴儴绫诲疄鐜扮嚎绋嬶紝瀵筳澧炲噺鐨勬椂鍊欐病鏈夎�冭檻椤哄簭闂銆�

public class ThreadTest1

{

private int j;

public static void main(String args[]){

ThreadTest1 tt=newThreadTest1();

Inc inc=tt.new Inc();

Dec dec=tt.new Dec();

for(inti=0;i<2;i++){

Thread t=newThread(inc);

t.start();

t=new Thread(dec);

t.start();

}

}

private synchronized void inc(){

j++;

System.out.println(Thread.currentThread().getName()+"-inc:"+j);

}

private synchronized void dec(){

j--;

System.out.println(Thread.currentThread().getName()+"-dec:"+j);

}

class Inc implements Runnable{

public void run(){

for(inti=0;i<100;i++){

inc();

}

}

}

class Dec implements Runnable{

public void run(){

for(inti=0;i<100;i++){

dec();

}

}

}

}

----------闅忔墜鍐嶅啓鐨勪竴涓�-------------

class A

{

JManger j =new JManager();

main()

{

new A().call();

}

void call

{

for(int i=0;i<2;i++)

{

new Thread(

newRunnable(){ public void run(){while(true){j.accumulate()}}}

).start();

new Thread(newRunnable(){ public void run(){while(true){j.sub()}}}).start();

}

}

}

class JManager

{

private j = 0;

public synchronized voidsubtract()

{

j--

}

public synchronized voidaccumulate()

{

j++;

}

}

56銆佸瓙绾跨▼寰幆10娆★紝鎺ョ潃涓荤嚎绋嬪惊鐜�100锛屾帴鐫�鍙堝洖鍒板瓙绾跨▼寰幆10娆★紝鎺ョ潃鍐嶅洖鍒颁富绾跨▼鍙堝惊鐜�100锛屽姝ゅ惊鐜�50娆★紝璇峰啓鍑虹▼搴忋��

鏈�缁堢殑绋嬪簭浠g爜濡備笅锛�

publicclassThreadTest {

/**

*@paramargs

*/

public static voidmain(String[] args) {

//TODOAuto-generated method stub

newThreadTest().init();

}

public voidinit()

{

finalBusiness business =newBusiness();

newThread(

newRunnable()

{

public voidrun() {

for(inti=0;i<50;i++)

{

business.SubThread(i);

}

}

}

).start();

for(inti=0;i<50;i++)

{

business.MainThread(i);

}

}

private classBusiness

{

booleanbShouldSub =true;//杩欓噷鐩稿綋浜庡畾涔変簡鎺у埗璇ヨ皝鎵ц鐨勪竴涓俊鍙风伅

public synchronized voidMainThread(inti)

{

if(bShouldSub)

try{

this.wait();

}catch(InterruptedException e) {

//TODOAuto-generatedcatch block

e.printStackTrace();

}

for(intj=0;j<5;j++)

{

System.out.println(Thread.currentThread().getName()+ ":i=" + i +",j=" + j);

}

bShouldSub =true;

this.notify();

}

public synchronized voidSubThread(inti)

{

if(!bShouldSub)

try{

this.wait();

}catch(InterruptedExceptione) {

//TODOAuto-generatedcatch block

e.printStackTrace();

}

for(intj=0;j<10;j++)

{

System.out.println(Thread.currentThread().getName()+ ":i=" + i +",j=" + j);

}

bShouldSub =false;

this.notify();

}

}

}

澶囨敞锛氫笉鍙兘涓�涓婃潵灏卞啓鍑轰笂闈㈢殑瀹屾暣浠g爜锛屾渶鍒濆啓鍑烘潵鐨勪唬鐮佸涓嬶紝闂鍦ㄤ簬涓や釜绾跨▼鐨勪唬鐮佽鍙傜収鍚屼竴涓彉閲忥紝鍗宠繖涓や釜绾跨▼鐨勪唬鐮佽鍏变韩鏁版嵁锛屾墍浠ワ紝鎶婅繖涓や釜绾跨▼鐨勬墽琛屼唬鐮佹惉鍒板悓涓�涓被涓幓锛�

packagecom.huawei.interview.lym;

publicclassThreadTest {

private static booleanbShouldMain=false;

public static voidmain(String[]args) {

//TODOAuto-generated method stub

/*new Thread(){

public void run()

{

for(int i=0;i<50;i++)

{

for(int j=0;j<10;j++)

{

System.out.println("i="+ i + ",j=" + j);

}

}

}

}.start();*/

//final String str = newString("");

newThread(

newRunnable()

{

public voidrun()

{

for(inti=0;i<50;i++)

{

synchronized(ThreadTest.class) {

if(bShouldMain)

{

try{

ThreadTest.class.wait();}

catch(InterruptedException e) {

e.printStackTrace();

}

}

for(intj=0;j<10;j++)

{

System.out.println(

Thread.currentThread().getName()+

"i="+ i + ",j=" + j);

}

bShouldMain=true;

ThreadTest.class.notify();

}

}

}

}

).start();

for(inti=0;i<50;i++)

{

synchronized(ThreadTest.class){

if(!bShouldMain)

{

try{

ThreadTest.class.wait();}

catch(InterruptedException e) {

e.printStackTrace();

}

}

for(intj=0;j<5;j++)

{

System.out.println(

Thread.currentThread().getName()+

"i=" + i +",j=" + j);

}

bShouldMain=false;

ThreadTest.class.notify();

}

}

}

}

涓嬮潰浣跨敤jdk5涓殑骞跺彂搴撴潵瀹炵幇鐨勶細

import java.util.concurrent.Executors;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.locks.Lock;

import java.util.concurrent.locks.ReentrantLock;

import java.util.concurrent.locks.Condition;

public class ThreadTest

{

private static Locklock = new ReentrantLock();

private staticCondition subThreadCondition = lock.newCondition();

private staticboolean bBhouldSubThread = false;

public static voidmain(String [] args)

{

ExecutorServicethreadPool = Executors.newFixedThreadPool(3);

threadPool.execute(newRunnable(){

publicvoid run()

{

for(inti=0;i<50;i++)

{

lock.lock();

try

{

if(!bBhouldSubThread)

subThreadCondition.await();

for(intj=0;j<10;j++)

{

System.out.println(Thread.currentThread().getName()+ ",j=" + j);

}

bBhouldSubThread= false;

subThreadCondition.signal();

}catch(Exceptione)

{

}

finally

{

lock.unlock();

}

}

}

});

threadPool.shutdown();

for(inti=0;i<50;i++)

{

lock.lock();

try

{

if(bBhouldSubThread)

subThreadCondition.await();

for(intj=0;j<10;j++)

{

System.out.println(Thread.currentThread().getName()+ ",j=" + j);

}

bBhouldSubThread= true;

subThreadCondition.signal();

}catch(Exceptione)

{

}

finally

{

lock.unlock();

}

}

}

}

57銆佷粙缁岰ollection妗嗘灦鐨勭粨鏋�

绛旓細闅忔剰鍙戞尌棰橈紝澶╁崡娴峰寳璋佷究璋堬紝鍙璁╁埆瑙夊緱浣犵煡璇嗘笂鍗氾紝鐞嗚В閫忓交鍗冲彲銆�

58銆丆ollection妗嗘灦涓疄鐜版瘮杈冭瀹炵幇浠�涔堟帴鍙�

comparable/comparator

59銆丄rrayList鍜孷ector鐨勫尯鍒�

绛旓細

杩欎袱涓被閮藉疄鐜颁簡List鎺ュ彛锛圠ist鎺ュ彛缁ф壙浜咰ollection鎺ュ彛锛夛紝浠栦滑閮芥槸鏈夊簭闆嗗悎锛屽嵆瀛樺偍鍦ㄨ繖涓や釜闆嗗悎涓殑鍏冪礌鐨勪綅缃兘鏄湁椤哄簭鐨勶紝鐩稿綋浜庝竴绉嶅姩鎬佺殑鏁扮粍锛屾垜浠互鍚庡彲浠ユ寜浣嶇疆绱㈠紩鍙峰彇鍑烘煇涓厓绱狅紝锛屽苟涓斿叾涓殑鏁版嵁鏄厑璁搁噸澶嶇殑锛岃繖鏄疕ashSet涔嬬被鐨勯泦鍚堢殑鏈�澶т笉鍚屽锛孒ashSet涔嬬被鐨勯泦鍚堜笉鍙互鎸夌储寮曞彿鍘绘绱㈠叾涓殑鍏冪礌锛屼篃涓嶅厑璁告湁閲嶅鐨勫厓绱狅紙鏈潵棰樼洰闂殑涓巋ashset娌℃湁浠讳綍鍏崇郴锛屼絾涓轰簡璇存竻妤欰rrayList涓嶸ector鐨勫姛鑳斤紝鎴戜滑浣跨敤瀵规瘮鏂瑰紡锛屾洿鏈夊埄浜庤鏄庨棶棰橈級銆�

鎺ョ潃鎵嶈ArrayList涓嶸ector鐨勫尯鍒紝杩欎富瑕佸寘鎷袱涓柟闈細.

锛�1锛夊悓姝ユ�э細

Vector鏄嚎绋嬪畨鍏ㄧ殑锛屼篃灏辨槸璇存槸瀹冪殑鏂规硶涔嬮棿鏄嚎绋嬪悓姝ョ殑锛岃�孉rrayList鏄嚎绋嬪簭涓嶅畨鍏ㄧ殑锛屽畠鐨勬柟娉曚箣闂存槸绾跨▼涓嶅悓姝ョ殑銆傚鏋滃彧鏈変竴涓嚎绋嬩細璁块棶鍒伴泦鍚堬紝閭f渶濂芥槸浣跨敤ArrayList锛屽洜涓哄畠涓嶈�冭檻绾跨▼瀹夊叏锛屾晥鐜囦細楂樹簺锛涘鏋滄湁澶氫釜绾跨▼浼氳闂埌闆嗗悎锛岄偅鏈�濂芥槸浣跨敤Vector锛屽洜涓轰笉闇�瑕佹垜浠嚜宸卞啀鍘昏�冭檻鍜岀紪鍐欑嚎绋嬪畨鍏ㄧ殑浠g爜銆�

澶囨敞锛氬浜嶸ector&ArrayList銆丠ashtable&HashMap锛岃璁颁綇绾跨▼瀹夊叏鐨勯棶棰橈紝璁颁綇Vector涓嶩ashtable鏄棫鐨勶紝鏄痡ava涓�璇炵敓灏辨彁渚涗簡鐨勶紝瀹冧滑鏄嚎绋嬪畨鍏ㄧ殑锛孉rrayList涓嶩ashMap鏄痡ava2鏃舵墠鎻愪緵鐨勶紝瀹冧滑鏄嚎绋嬩笉瀹夊叏鐨勩�傛墍浠ワ紝鎴戜滑璁茶鏃跺厛璁茶�佺殑銆�

锛�2锛夋暟鎹闀匡細

ArrayList涓嶸ector閮芥湁涓�涓垵濮嬬殑瀹归噺澶у皬锛屽綋瀛樺偍杩涘畠浠噷闈㈢殑鍏冪礌鐨勪釜鏁拌秴杩囦簡瀹归噺鏃讹紝灏遍渶瑕佸鍔燗rrayList涓嶸ector鐨勫瓨鍌ㄧ┖闂达紝姣忔瑕佸鍔犲瓨鍌ㄧ┖闂存椂锛屼笉鏄彧澧炲姞涓�涓瓨鍌ㄥ崟鍏冿紝鑰屾槸澧炲姞澶氫釜瀛樺偍鍗曞厓锛屾瘡娆″鍔犵殑瀛樺偍鍗曞厓鐨勪釜鏁板湪鍐呭瓨绌洪棿鍒╃敤涓庣▼搴忔晥鐜囦箣闂磋鍙栧緱涓�瀹氱殑骞宠 銆俈ector榛樿澧為暱涓哄師鏉ヤ袱鍊嶏紝鑰孉rrayList鐨勫闀跨瓥鐣ュ湪鏂囨。涓病鏈夋槑纭瀹氾紙浠庢簮浠g爜鐪嬪埌鐨勬槸澧為暱涓哄師鏉ョ殑1.5鍊嶏級銆侫rrayList涓嶸ector閮藉彲浠ヨ缃垵濮嬬殑绌洪棿澶у皬锛孷ector杩樺彲浠ヨ缃闀跨殑绌洪棿澶у皬锛岃�孉rrayList娌℃湁鎻愪緵璁剧疆澧為暱绌洪棿鐨勬柟娉曘��

鎬荤粨锛氬嵆Vector澧為暱鍘熸潵鐨勪竴鍊嶏紝ArrayList澧炲姞鍘熸潵鐨�0.5鍊嶃��

60銆丠ashMap鍜孒ashtable鐨勫尯鍒�

锛堟潯鐞嗕笂杩橀渶瑕佹暣鐞嗭紝涔熸槸鍏堣鐩稿悓鐐癸紝鍐嶈涓嶅悓鐐癸級

HashMap鏄疕ashtable鐨勮交閲忕骇瀹炵幇锛堥潪绾跨▼瀹夊叏鐨勫疄鐜帮級锛屼粬浠兘瀹屾垚浜哅ap鎺ュ彛锛屼富瑕佸尯鍒湪浜嶩ashMap鍏佽绌猴紙null锛夐敭鍊硷紙key锛�,鐢变簬闈炵嚎绋嬪畨鍏紝鍦ㄥ彧鏈変竴涓嚎绋嬭闂殑鎯呭喌涓嬶紝鏁堢巼瑕侀珮浜嶩ashtable銆�

HashMap鍏佽灏唍ull浣滀负涓�涓猠ntry鐨刱ey鎴栬�卾alue锛岃�孒ashtable涓嶅厑璁搞��

HashMap鎶奌ashtable鐨刢ontains鏂规硶鍘绘帀浜嗭紝鏀规垚containsvalue鍜宑ontainsKey銆傚洜涓篶ontains鏂规硶瀹规槗璁╀汉寮曡捣璇В銆�

Hashtable缁ф壙鑷狣ictionary绫伙紝鑰孒ashMap鏄疛ava1.2寮曡繘鐨凪ap interface鐨勪竴涓疄鐜般��

鏈�澶х殑涓嶅悓鏄紝Hashtable鐨勬柟娉曟槸Synchronize鐨勶紝鑰孒ashMap涓嶆槸锛屽湪澶氫釜绾跨▼璁块棶Hashtable鏃讹紝涓嶉渶瑕佽嚜宸变负瀹冪殑鏂规硶瀹炵幇鍚屾锛岃�孒ashMap灏卞繀椤讳负涔嬫彁渚涘鍚屾銆�

Hashtable鍜孒ashMap閲囩敤鐨刪ash/rehash绠楁硶閮藉ぇ姒備竴鏍凤紝鎵�浠ユ�ц兘涓嶄細鏈夊緢澶х殑宸紓銆�

灏盚ashMap涓嶩ashTable涓昏浠庝笁鏂归潰鏉ヨ銆�

涓�.鍘嗗彶鍘熷洜:Hashtable鏄熀浜庨檲鏃х殑Dictionary绫荤殑锛孒ashMap鏄疛ava 1.2寮曡繘鐨凪ap鎺ュ彛鐨勪竴涓疄鐜�

浜�.鍚屾鎬�:Hashtable鏄嚎绋嬪畨鍏ㄧ殑锛屼篃灏辨槸璇存槸鍚屾鐨勶紝鑰孒ashMap鏄嚎绋嬪簭涓嶅畨鍏ㄧ殑锛屼笉鏄悓姝ョ殑

涓�.鍊硷細鍙湁HashMap鍙互璁╀綘灏嗙┖鍊间綔涓轰竴涓〃鐨勬潯鐩殑key鎴杤alue

61銆丩ist鍜� Map鍖哄埆?

涓�涓槸瀛樺偍鍗曞垪鏁版嵁鐨勯泦鍚堬紝鍙︿竴涓槸瀛樺偍閿拰鍊艰繖鏍风殑鍙屽垪鏁版嵁鐨勯泦鍚堬紝List涓瓨鍌ㄧ殑鏁版嵁鏄湁椤哄簭锛屽苟涓斿厑璁搁噸澶嶏紱Map涓瓨鍌ㄧ殑鏁版嵁鏄病鏈夐『搴忕殑锛屽叾閿槸涓嶈兘閲嶅鐨勶紝瀹冪殑鍊兼槸鍙互鏈夐噸澶嶇殑銆�

62銆丩ist, Set, Map鏄惁缁ф壙鑷狢ollection鎺ュ彛?

List锛孲et鏄紝Map涓嶆槸

63銆丩ist銆丮ap銆丼et涓変釜鎺ュ彛锛屽瓨鍙栧厓绱犳椂锛屽悇鏈変粈涔堢壒鐐癸紵

杩欐牱鐨勯灞炰簬闅忔剰鍙戞尌棰橈細杩欐牱鐨勯姣旇緝鑰冩按骞筹紝涓や釜鏂归潰鐨勬按骞筹細涓�鏄鐪熸鏄庣櫧杩欎簺鍐呭锛屼簩鏄鏈夎緝寮虹殑鎬荤粨鍜岃〃杩拌兘鍔涖�傚鏋滀綘鏄庣櫧锛屼絾琛ㄨ堪涓嶆竻妤氾紝鍦ㄥ埆浜洪偅閲屽垯绛夊悓浜庝笉鏄庣櫧銆�

棣栧厛锛孡ist涓嶴et鍏锋湁鐩镐技鎬э紝瀹冧滑閮芥槸鍗曞垪鍏冪礌鐨勯泦鍚堬紝鎵�浠ワ紝瀹冧滑鏈変竴涓姛鍏卞悓鐨勭埗鎺ュ彛锛屽彨Collection銆係et閲岄潰涓嶅厑璁告湁閲嶅鐨勫厓绱狅紝鎵�璋撻噸澶嶏紝鍗充笉鑳芥湁涓や釜鐩哥瓑锛堟敞鎰忥紝涓嶆槸浠呬粎鏄浉鍚岋級鐨勫璞★紝鍗冲亣璁維et闆嗗悎涓湁浜嗕竴涓狝瀵硅薄锛岀幇鍦ㄦ垜瑕佸悜Set闆嗗悎鍐嶅瓨鍏ヤ竴涓狟瀵硅薄锛屼絾B瀵硅薄涓嶢瀵硅薄equals鐩哥瓑锛屽垯B瀵硅薄瀛樺偍涓嶈繘鍘伙紝鎵�浠ワ紝Set闆嗗悎鐨刟dd鏂规硶鏈変竴涓猙oolean鐨勮繑鍥炲�硷紝褰撻泦鍚堜腑娌℃湁鏌愪釜鍏冪礌锛屾鏃禷dd鏂规硶鍙垚鍔熷姞鍏ヨ鍏冪礌鏃讹紝鍒欒繑鍥瀟rue锛屽綋闆嗗悎鍚湁涓庢煇涓厓绱爀quals鐩哥瓑鐨勫厓绱犳椂锛屾鏃禷dd鏂规硶鏃犳硶鍔犲叆璇ュ厓绱狅紝杩斿洖缁撴灉涓篺alse銆係et鍙栧厓绱犳椂锛屾病娉曡鍙栫鍑犱釜锛屽彧鑳戒互Iterator鎺ュ彛鍙栧緱鎵�鏈夌殑鍏冪礌锛屽啀閫愪竴閬嶅巻鍚勪釜鍏冪礌銆�

List琛ㄧず鏈夊厛鍚庨『搴忕殑闆嗗悎锛屾敞鎰忥紝涓嶆槸閭g鎸夊勾榫勩�佹寜澶у皬銆佹寜浠锋牸涔嬬被鐨勬帓搴忋�傚綋鎴戜滑澶氭璋冪敤add(Obj e)鏂规硶鏃讹紝姣忔鍔犲叆鐨勫璞″氨鍍忕伀杞︾珯涔扮エ鏈夋帓闃熼『搴忎竴鏍凤紝鎸夊厛鏉ュ悗鍒扮殑椤哄簭鎺掑簭銆傛湁鏃跺�欙紝涔熷彲浠ユ彃闃燂紝鍗宠皟鐢╝dd(int index,Obj e)鏂规硶锛屽氨鍙互鎸囧畾褰撳墠瀵硅薄鍦ㄩ泦鍚堜腑鐨勫瓨鏀句綅缃�備竴涓璞″彲浠ヨ鍙嶅瀛樺偍杩汱ist涓紝姣忚皟鐢ㄤ竴娆dd鏂规硶锛岃繖涓璞″氨琚彃鍏ヨ繘闆嗗悎涓竴娆★紝鍏跺疄锛屽苟涓嶆槸鎶婅繖涓璞℃湰韬瓨鍌ㄨ繘浜嗛泦鍚堜腑锛岃�屾槸鍦ㄩ泦鍚堜腑鐢ㄤ竴涓储寮曞彉閲忔寚鍚戣繖涓璞★紝褰撹繖涓璞¤add澶氭鏃讹紝鍗崇浉褰撲簬闆嗗悎涓湁澶氫釜绱㈠紩鎸囧悜浜嗚繖涓璞★紝濡傚浘x鎵�绀恒�侺ist闄や簡鍙互浠terator鎺ュ彛鍙栧緱鎵�鏈夌殑鍏冪礌锛屽啀閫愪竴閬嶅巻鍚勪釜鍏冪礌涔嬪锛岃繕鍙互璋冪敤get(index i)鏉ユ槑纭鏄庡彇绗嚑涓��

Map涓嶭ist鍜孲et涓嶅悓锛屽畠鏄弻鍒楃殑闆嗗悎锛屽叾涓湁put鏂规硶锛屽畾涔夊涓嬶細put(obj key,objvalue)锛屾瘡娆″瓨鍌ㄦ椂锛岃瀛樺偍涓�瀵筴ey/value锛屼笉鑳藉瓨鍌ㄩ噸澶嶇殑key锛岃繖涓噸澶嶇殑瑙勫垯涔熸槸鎸塭quals姣旇緝鐩哥瓑銆傚彇鍒欏彲浠ユ牴鎹甼ey鑾峰緱鐩稿簲鐨剉alue锛屽嵆get(Object key)杩斿洖鍊间负key鎵�瀵瑰簲鐨剉alue銆傚彟澶栵紝涔熷彲浠ヨ幏寰楁墍鏈夌殑key鐨勭粨鍚堬紝杩樺彲浠ヨ幏寰楁墍鏈夌殑value鐨勭粨鍚堬紝杩樺彲浠ヨ幏寰梜ey鍜寁alue缁勫悎鎴愮殑Map.Entry瀵硅薄鐨勯泦鍚堛��

List 浠ョ壒瀹氭搴忔潵鎸佹湁鍏冪礌锛屽彲鏈夐噸澶嶅厓绱犮�係et鏃犳硶鎷ユ湁閲嶅鍏冪礌,鍐呴儴鎺掑簭銆侻ap淇濆瓨key-value鍊硷紝value鍙鍊笺��

HashSet鎸夌収hashcode鍊肩殑鏌愮杩愮畻鏂瑰紡杩涜瀛樺偍锛岃�屼笉鏄洿鎺ユ寜hashCode鍊肩殑澶у皬杩涜瀛樺偍銆備緥濡傦紝"abc"---> 78锛�"def" ---> 62锛�"xyz" ---> 65鍦╤ashSet涓殑瀛樺偍椤哄簭涓嶆槸62,65,78锛岃繖浜涢棶棰樻劅璋互鍓嶄竴涓彨宕斿仴鐨勫鍛樻彁鍑猴紝鏈�鍚庨�氳繃鏌ョ湅婧愪唬鐮佺粰浠栬В閲婃竻妤氾紝鐪嬫湰娆″煿璁鍛樺綋涓湁澶氬皯鑳界湅鎳傛簮鐮併�侺inkedHashSet鎸夋彃鍏ョ殑椤哄簭瀛樺偍锛岄偅琚瓨鍌ㄥ璞$殑hashcode鏂规硶杩樻湁浠�涔堜綔鐢ㄥ憿锛熷鍛樻兂鎯�!hashset闆嗗悎姣旇緝涓や釜瀵硅薄鏄惁鐩哥瓑锛岄鍏堢湅hashcode鏂规硶鏄惁鐩哥瓑锛岀劧鍚庣湅equals鏂规硶鏄惁鐩哥瓑銆俷ew涓や釜Student鎻掑叆鍒癏ashSet涓紝鐪婬ashSet鐨剆ize锛屽疄鐜癶ashcode鍜宔quals鏂规硶鍚庡啀鐪媠ize銆�

鍚屼竴涓璞″彲浠ュ湪Vector涓姞鍏ュ娆°�傚線闆嗗悎閲岄潰鍔犲厓绱狅紝鐩稿綋浜庨泦鍚堥噷鐢ㄤ竴鏍圭怀瀛愯繛鎺ュ埌浜嗙洰鏍囧璞°�傚線HashSet涓嵈鍔犱笉浜嗗娆$殑銆�

64銆佽鍑篈rrayList,Vector, LinkedList鐨勫瓨鍌ㄦ�ц兘鍜岀壒鎬�

ArrayList鍜孷ector閮芥槸浣跨敤鏁扮粍鏂瑰紡瀛樺偍鏁版嵁锛屾鏁扮粍鍏冪礌鏁板ぇ浜庡疄闄呭瓨鍌ㄧ殑鏁版嵁浠ヤ究澧炲姞鍜屾彃鍏ュ厓绱狅紝瀹冧滑閮藉厑璁哥洿鎺ユ寜搴忓彿绱㈠紩鍏冪礌锛屼絾鏄彃鍏ュ厓绱犺娑夊強鏁扮粍鍏冪礌绉诲姩绛夊唴瀛樻搷浣滐紝鎵�浠ョ储寮曟暟鎹揩鑰屾彃鍏ユ暟鎹參锛孷ector鐢变簬浣跨敤浜唖ynchronized鏂规硶锛堢嚎绋嬪畨鍏級锛岄�氬父鎬ц兘涓婅緝ArrayList宸紝鑰孡inkedList浣跨敤鍙屽悜閾捐〃瀹炵幇瀛樺偍锛屾寜搴忓彿绱㈠紩鏁版嵁闇�瑕佽繘琛屽墠鍚戞垨鍚庡悜閬嶅巻锛屼絾鏄彃鍏ユ暟鎹椂鍙渶瑕佽褰曟湰椤圭殑鍓嶅悗椤瑰嵆鍙紝鎵�浠ユ彃鍏ラ�熷害杈冨揩銆�

LinkedList涔熸槸绾跨▼涓嶅畨鍏ㄧ殑锛孡inkedList鎻愪緵浜嗕竴浜涙柟娉曪紝浣垮緱LinkedList鍙互琚綋浣滃爢鏍堝拰闃熷垪鏉ヤ娇鐢ㄣ��

65銆佸幓鎺変竴涓猇ector闆嗗悎涓噸澶嶇殑鍏冪礌

Vector newVector = new Vector();

For (int i=0;i

{

Object obj = vector.get(i);

if(!newVector.contains(obj);

newVector.add(obj);

}

杩樻湁涓�绉嶇畝鍗曠殑鏂瑰紡锛孒ashSet set = new HashSet(vector);

66銆丆ollection鍜� Collections鐨勫尯鍒��

Collection鏄泦鍚堢被鐨勪笂绾ф帴鍙o紝缁ф壙涓庝粬鐨勬帴鍙d富瑕佹湁Set鍜孡ist.

Collections鏄拡瀵归泦鍚堢被鐨勪竴涓府鍔╃被锛屼粬鎻愪緵涓�绯诲垪闈欐�佹柟娉曞疄鐜板鍚勭闆嗗悎鐨勬悳绱€�佹帓搴忋�佺嚎绋嬪畨鍏ㄥ寲绛夋搷浣溿��

67銆丼et閲岀殑鍏冪礌鏄笉鑳介噸澶嶇殑锛岄偅涔堢敤浠�涔堟柟娉曟潵鍖哄垎閲嶅涓庡惁鍛�?鏄敤==杩樻槸equals()?瀹冧滑鏈変綍鍖哄埆?

Set閲岀殑鍏冪礌鏄笉鑳介噸澶嶇殑锛屽厓绱犻噸澶嶄笌鍚︽槸浣跨敤equals()鏂规硶杩涜鍒ゆ柇鐨勩��

equals()鍜�==鏂规硶鍐冲畾寮曠敤鍊兼槸鍚︽寚鍚戝悓涓�瀵硅薄equals()鍦ㄧ被涓瑕嗙洊锛屼负鐨勬槸褰撲袱涓垎绂荤殑瀵硅薄鐨勫唴瀹瑰拰绫诲瀷鐩搁厤鐨勮瘽锛岃繑鍥炵湡鍊笺��

68銆佷綘鎵�鐭ラ亾鐨勯泦鍚堢被閮芥湁鍝簺锛熶富瑕佹柟娉曪紵

鏈�甯哥敤鐨勯泦鍚堢被鏄� List 鍜� Map銆� List鐨勫叿浣撳疄鐜板寘鎷� ArrayList鍜� Vector锛屽畠浠槸鍙彉澶у皬鐨勫垪琛紝姣旇緝閫傚悎鏋勫缓銆佸瓨鍌ㄥ拰鎿嶄綔浠讳綍绫诲瀷瀵硅薄鐨勫厓绱犲垪琛ㄣ�� List閫傜敤浜庢寜鏁板�肩储寮曡闂厓绱犵殑鎯呭舰銆�

Map 鎻愪緵浜嗕竴涓洿閫氱敤鐨勫厓绱犲瓨鍌ㄦ柟娉曘�� Map闆嗗悎绫荤敤浜庡瓨鍌ㄥ厓绱犲锛堢О浣�"閿�"鍜�"鍊�"锛夛紝鍏朵腑姣忎釜閿槧灏勫埌涓�涓�笺��

ArrayList/Vector脿List

脿Collection

HashSet/TreeSet脿Set

Propeties脿HashTable

脿Map

Treemap/HashMap

鎴戣鐨勪笉鏄柟娉曞悕锛岃�屾槸鎬濇兂锛屾垜鐭ラ亾瀹冧滑閮芥湁澧炲垹鏀规煡鐨勬柟娉曪紝浣嗚繖浜涙柟娉曠殑鍏蜂綋鍚嶇О锛屾垜璁板緱涓嶆槸寰堟竻妤氾紝瀵逛簬set锛屽ぇ姒傜殑鏂规硶鏄痑dd,remove, contains锛涘浜巑ap锛屽ぇ姒傜殑鏂规硶灏辨槸put,remove锛宑ontains绛夛紝鍥犱负锛屾垜鍙鍦╡clispe涓嬫寜鐐规搷浣滅锛屽緢鑷劧鐨勮繖浜涙柟娉曞氨鍑烘潵浜嗐�傛垜璁颁綇鐨勪竴浜涙�濇兂灏辨槸List绫讳細鏈塯et(int index)杩欐牱鐨勬柟娉曪紝鍥犱负瀹冨彲浠ユ寜椤哄簭鍙栧厓绱狅紝鑰宻et绫讳腑娌℃湁get(int index)杩欐牱鐨勬柟娉曘�侺ist鍜宻et閮藉彲浠ヨ凯浠e嚭鎵�鏈夊厓绱狅紝杩唬鏃跺厛瑕佸緱鍒颁竴涓猧terator瀵硅薄锛屾墍浠ワ紝set鍜宭ist绫婚兘鏈変竴涓猧terator鏂规硶锛岀敤浜庤繑鍥為偅涓猧terator瀵硅薄銆俶ap鍙互杩斿洖涓変釜闆嗗悎锛屼竴涓槸杩斿洖鎵�鏈夌殑key鐨勯泦鍚堬紝鍙﹀涓�涓繑鍥炵殑鏄墍鏈塿alue鐨勯泦鍚堬紝鍐嶄竴涓繑鍥炵殑key鍜寁alue缁勫悎鎴愮殑EntrySet瀵硅薄鐨勯泦鍚堬紝map涔熸湁get鏂规硶锛屽弬鏁版槸key锛岃繑鍥炲�兼槸key瀵瑰簲鐨剉alue銆�

69銆佷袱涓璞″�肩浉鍚�(x.equals(y) == true)锛屼絾鍗村彲鏈変笉鍚岀殑hash code锛岃繖鍙ヨ瘽瀵逛笉瀵�?

瀵广��

濡傛灉瀵硅薄瑕佷繚瀛樺湪HashSet鎴朒ashMap涓紝瀹冧滑鐨別quals鐩哥瓑锛岄偅涔堬紝瀹冧滑鐨刪ashcode鍊煎氨蹇呴』鐩哥瓑銆�

濡傛灉涓嶆槸瑕佷繚瀛樺湪HashSet鎴朒ashMap锛屽垯涓巋ashcode娌℃湁浠�涔堝叧绯讳簡锛岃繖鏃跺�檋ashcode涓嶇瓑鏄彲浠ョ殑锛屼緥濡俛rrayList瀛樺偍鐨勫璞″氨涓嶇敤瀹炵幇hashcode锛屽綋鐒讹紝鎴戜滑娌℃湁鐞嗙敱涓嶅疄鐜帮紝閫氬父閮戒細鍘诲疄鐜扮殑銆�

70銆乀reeSet閲岄潰鏀惧璞★紝濡傛灉鍚屾椂鏀惧叆浜嗙埗绫诲拰瀛愮被鐨勫疄渚嬪璞★紝閭f瘮杈冩椂浣跨敤鐨勬槸鐖剁被鐨刢ompareTo鏂规硶锛岃繕鏄娇鐢ㄧ殑瀛愮被鐨刢ompareTo鏂规硶锛岃繕鏄姏寮傚父锛�

锛堝簲璇ユ槸娌℃湁閽堝闂鐨勭‘鍒囩殑绛旀锛屽綋鍓嶇殑add鏂规硶鏀惧叆鐨勬槸鍝釜瀵硅薄锛屽氨璋冪敤鍝釜瀵硅薄鐨刢ompareTo鏂规硶锛岃嚦浜庤繖涓猚ompareTo鏂规硶鎬庝箞鍋氾紝灏辩湅褰撳墠杩欎釜瀵硅薄鐨勭被涓槸濡備綍缂栧啓杩欎釜鏂规硶鐨勶級

瀹為獙浠g爜锛�

publicclassParentimplementsComparable {

private intage = 0;

publicParent(intage){

this.age = age;

}

public intcompareTo(Object o){

//TODOAuto-generated method stub

System.out.println("method ofparent");

Parent o1 = (Parent)o;

returnage>o1.age?1:age

}

}

publicclassChildextendsParent {

publicChild(){

super(3);

}

public intcompareTo(Object o){

//TODOAuto-generated methodstub

System.out.println("methodof child");

//Child o1 = (Child)o;

return1;

}

}

publicclassTreeSetTest {

/**

*@paramargs

*/

public static voidmain(String[] args) {

//TODOAuto-generated method stub

TreeSet set =newTreeSet();

set.add(newParent(3));

set.add(newChild());

set.add(newParent(4));

System.out.println(set.size());

}

}

71銆佽鍑轰竴浜涘父鐢ㄧ殑绫伙紝鍖咃紝鎺ュ彛锛岃鍚勪妇5涓�

瑕佽浜哄鎰熻浣犲java ee寮�鍙戝緢鐔燂紝鎵�浠ワ紝涓嶈兘浠呬粎鍙垪core java涓殑閭d簺涓滆タ锛岃澶氬垪浣犲湪鍋歴sh椤圭洰涓秹鍙婄殑閭d簺涓滆タ銆傚氨鍐欎綘鏈�杩戝啓鐨勯偅浜涚▼搴忎腑娑夊強鐨勯偅浜涚被銆�

甯哥敤鐨勭被锛欱ufferedReaderBufferedWriter 聽FileReader FileWirter 聽String 聽Integer

java.util.Date锛孲ystem锛孋lass锛孡ist,HashMap

甯哥敤鐨勫寘锛歫ava.langjava.io java.util 聽java.sql,javax.servlet,org.apache.strtuts.action,org.hibernate

甯哥敤鐨勬帴鍙o細RemoteList Map 聽Document 聽NodeList,Servlet,HttpServletRequest,HttpServletResponse,Transaction(Hibernate)銆丼ession(Hibernate),HttpSession

72銆乯ava涓湁鍑犵绫诲瀷鐨勬祦锛烰DK涓烘瘡绉嶇被鍨嬬殑娴佹彁渚涗簡涓�浜涙娊璞$被浠ヤ緵缁ф壙锛岃璇村嚭浠栦滑鍒嗗埆鏄摢浜涚被锛�

瀛楄妭娴侊紝瀛楃娴併�傚瓧鑺傛祦缁ф壙浜嶪nputStream OutputStream锛屽瓧绗︽祦缁ф壙浜嶪nputStreamReaderOutputStreamWriter銆傚湪java.io鍖呬腑杩樻湁璁稿鍏朵粬鐨勬祦锛屼富瑕佹槸涓轰簡鎻愰珮鎬ц兘鍜屼娇鐢ㄦ柟渚裤��

73銆佸瓧鑺傛祦涓庡瓧绗︽祦鐨勫尯鍒�

瑕佹妸涓�鐗囦簩杩涘埗鏁版嵁鏁版嵁閫愪竴杈撳嚭鍒版煇涓澶囦腑锛屾垨鑰呬粠鏌愪釜璁惧涓�愪竴璇诲彇涓�鐗囦簩杩涘埗鏁版嵁锛屼笉绠¤緭鍏ヨ緭鍑鸿澶囨槸浠�涔堬紝鎴戜滑瑕佺敤缁熶竴鐨勬柟寮忔潵瀹屾垚杩欎簺鎿嶄綔锛岀敤涓�绉嶆娊璞$殑鏂瑰紡杩涜鎻忚堪锛岃繖涓娊璞℃弿杩版柟寮忚捣鍚嶄负IO娴侊紝瀵瑰簲鐨勬娊璞$被涓篛utputStream鍜孖nputStream锛屼笉鍚岀殑瀹炵幇绫诲氨浠h〃涓嶅悓鐨勮緭鍏ュ拰杈撳嚭璁惧锛屽畠浠兘鏄拡瀵瑰瓧鑺傝繘琛屾搷浣滅殑銆�

鍦ㄥ簲鐢ㄤ腑锛岀粡甯歌瀹屽叏鏄瓧绗︾殑涓�娈垫枃鏈緭鍑哄幓鎴栬杩涙潵锛岀敤瀛楄妭娴佸彲浠ュ悧锛熻绠楁満涓殑涓�鍒囨渶缁堥兘鏄簩杩涘埗鐨勫瓧鑺傚舰寮忓瓨鍦ㄣ�傚浜庘�滀腑鍥解�濊繖浜涘瓧绗︼紝棣栧厛瑕佸緱鍒板叾瀵瑰簲鐨勫瓧鑺傦紝鐒跺悗灏嗗瓧鑺傚啓鍏ュ埌杈撳嚭娴併�傝鍙栨椂锛岄鍏堣鍒扮殑鏄瓧鑺傦紝鍙槸鎴戜滑瑕佹妸瀹冩樉绀轰负瀛楃锛屾垜浠渶瑕佸皢瀛楄妭杞崲鎴愬瓧绗︺�傜敱浜庤繖鏍风殑闇�姹傚緢骞挎硾锛屼汉瀹朵笓闂ㄦ彁渚涗簡瀛楃娴佺殑鍖呰绫汇��

搴曞眰璁惧姘歌繙鍙帴鍙楀瓧鑺傛暟鎹紝鏈夋椂鍊欒鍐欏瓧绗︿覆鍒板簳灞傝澶囷紝闇�瑕佸皢瀛楃涓茶浆鎴愬瓧鑺傚啀杩涜鍐欏叆銆�瀛楃娴佹槸瀛楄妭娴佺殑鍖呰锛�瀛楃娴佸垯鏄洿鎺ユ帴鍙楀瓧绗︿覆锛屽畠鍐呴儴灏嗕覆杞垚瀛楄妭锛屽啀鍐欏叆搴曞眰璁惧锛岃繖涓烘垜浠悜IO璁惧埆鍐欏叆鎴栬鍙栧瓧绗︿覆鎻愪緵浜嗕竴鐐圭偣鏂逛究銆�

瀛楃鍚戝瓧鑺傝浆鎹㈡椂锛岃娉ㄦ剰缂栫爜鐨勯棶棰�锛屽洜涓哄瓧绗︿覆杞垚瀛楄妭鏁扮粍锛�

鍏跺疄鏄浆鎴愯瀛楃鐨勬煇绉嶇紪鐮佺殑瀛楄妭褰㈠紡锛岃鍙栦篃鏄弽涔嬬殑閬撶悊銆�

璁茶В瀛楄妭娴佷笌瀛楃娴佸叧绯荤殑浠g爜妗堜緥锛�

import java.io.BufferedReader;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.InputStreamReader;

import java.io.PrintWriter;

public class IOTest {

public static void main(String[]args) throws Exception {

String str = "涓浗浜�";

/*FileOutputStreamfos 聽= newFileOutputStream("1.txt");

fos.write(str.getBytes("UTF-8"));

fos.close();*/

/*FileWriter fw =new FileWriter("1.txt");

fw.write(str);

fw.close();*/

PrintWriter pw =new PrintWriter("1.txt","utf-8");

pw.write(str);

pw.close();

/*FileReader fr =new FileReader("1.txt");

char[] buf = newchar[1024];

int len =fr.read(buf);

String myStr = newString(buf,0,len);

System.out.println(myStr);*/

/*FileInputStreamfr = new FileInputStream("1.txt");

byte[] buf = newbyte[1024];

int len =fr.read(buf);

String myStr = newString(buf,0,len,"UTF-8");

System.out.println(myStr);*/

BufferedReader br =new BufferedReader(

newInputStreamReader(

newFileInputStream("1.txt"),"UTF-8"

)

);

String myStr =br.readLine();

br.close();

System.out.println(myStr);

}

}

74銆佷粈涔堟槸java搴忓垪鍖栵紝濡備綍瀹炵幇java搴忓垪鍖栵紵鎴栬�呰瑙i噴Serializable鎺ュ彛鐨勪綔鐢ㄣ��

鎴戜滑鏈夋椂鍊欏皢涓�涓猨ava瀵硅薄鍙樻垚瀛楄妭娴佺殑褰㈠紡浼犲嚭鍘绘垨鑰呬粠涓�涓瓧鑺傛祦涓仮澶嶆垚涓�涓猨ava瀵硅薄锛屼緥濡傦紝瑕佸皢java瀵硅薄瀛樺偍鍒扮‖鐩樻垨鑰呬紶閫佺粰缃戠粶涓婄殑鍏朵粬璁$畻鏈猴紝杩欎釜杩囩▼鎴戜滑鍙互鑷繁鍐欎唬鐮佸幓鎶婁竴涓猨ava瀵硅薄鍙樻垚鏌愪釜鏍煎紡鐨勫瓧鑺傛祦鍐嶄紶杈擄紝浣嗘槸锛宩re鏈韩灏辨彁渚涗簡杩欑鏀寔锛屾垜浠彲浠ヨ皟鐢∣utputStream鐨剋riteObject鏂规硶鏉ュ仛锛屽鏋滆璁﹋ava甯垜浠仛锛�瑕佽浼犺緭鐨勫璞″繀椤诲疄鐜皊erializable鎺ュ彛锛岃繖鏍凤紝javac缂栬瘧鏃跺氨浼氳繘琛岀壒娈婂鐞嗭紝缂栬瘧鐨勭被鎵嶅彲浠ヨwriteObject鏂规硶鎿嶄綔锛岃繖灏辨槸鎵�璋撶殑搴忓垪鍖栥�傞渶瑕佽搴忓垪鍖栫殑绫诲繀椤诲疄鐜癝erializable鎺ュ彛锛岃鎺ュ彛鏄竴涓猰ini鎺ュ彛锛屽叾涓病鏈夐渶瑕佸疄鐜扮殑鏂规硶锛宨mplementsSerializable鍙槸涓轰簡鏍囨敞璇ュ璞℃槸鍙搴忓垪鍖栫殑銆�

渚嬪锛屽湪web寮�鍙戜腑锛屽鏋滃璞¤淇濆瓨鍦ㄤ簡Session涓紝tomcat鍦ㄩ噸鍚椂瑕佹妸Session瀵硅薄搴忓垪鍖栧埌纭洏锛岃繖涓璞″氨蹇呴』瀹炵幇Serializable鎺ュ彛銆傚鏋滃璞¤缁忚繃鍒嗗竷寮忕郴缁熻繘琛岀綉缁滀紶杈撴垨閫氳繃rmi绛夎繙绋嬭皟鐢紝杩欏氨闇�瑕佸湪缃戠粶涓婁紶杈撳璞★紝琚紶杈撶殑瀵硅薄灏卞繀椤诲疄鐜癝erializable鎺ュ彛銆�

75銆佹弿杩颁竴涓婮VM鍔犺浇class鏂囦欢鐨勫師鐞嗘満鍒�?

JVM涓被鐨勮杞芥槸鐢盋lassLoader鍜屽畠鐨勫瓙绫绘潵瀹炵幇鐨�,Java ClassLoader鏄竴涓噸瑕佺殑Java杩愯鏃剁郴缁熺粍浠躲�傚畠璐熻矗鍦ㄨ繍琛屾椂鏌ユ壘鍜岃鍏ョ被鏂囦欢鐨勭被銆�

76銆乭eap鍜宻tack鏈変粈涔堝尯鍒��

java鐨勫唴瀛樺垎涓轰袱绫伙紝涓�绫绘槸鏍堝唴瀛橈紝涓�绫绘槸鍫嗗唴瀛樸�傛爤鍐呭瓨鏄寚绋嬪簭杩涘叆涓�涓柟娉曟椂锛屼細涓鸿繖涓柟娉曞崟鐙垎閰嶄竴鍧楃灞炲瓨鍌ㄧ┖闂达紝鐢ㄤ簬瀛樺偍杩欎釜鏂规硶鍐呴儴鐨勫眬閮ㄥ彉閲忥紝褰撹繖涓柟娉曠粨鏉熸椂锛屽垎閰嶇粰杩欎釜鏂规硶鐨勬爤浼氶噴鏀撅紝杩欎釜鏍堜腑鐨勫彉閲忎篃灏嗛殢涔嬮噴鏀俱��

鍫嗘槸涓庢爤浣滅敤涓嶅悓鐨勫唴瀛橈紝涓�鑸敤浜庡瓨鏀句笉鏀惧湪褰撳墠鏂规硶鏍堜腑鐨勯偅浜涙暟鎹紝渚嬪锛屼娇鐢╪ew鍒涘缓鐨勫璞¢兘鏀惧湪鍫嗛噷锛屾墍浠ワ紝瀹冧笉浼氶殢鏂规硶鐨勭粨鏉熻�屾秷澶便��鏂规硶涓殑灞�閮ㄥ彉閲忎娇鐢╢inal淇グ鍚庯紝鏀惧湪鍫嗕腑锛岃�屼笉鏄爤涓��

77銆丟C鏄粈涔�?涓轰粈涔堣鏈塆C?

GC鏄瀮鍦炬敹闆嗙殑鎰忔�濓紙Gabage Collection锛�,鍐呭瓨澶勭悊鏄紪绋嬩汉鍛樺鏄撳嚭鐜伴棶棰樼殑鍦版柟锛屽繕璁版垨鑰呴敊璇殑鍐呭瓨鍥炴敹浼氬鑷寸▼搴忔垨绯荤粺鐨勪笉绋冲畾鐢氳嚦宕╂簝锛孞ava鎻愪緵鐨凣C鍔熻兘鍙互鑷姩鐩戞祴瀵硅薄鏄惁瓒呰繃浣滅敤鍩熶粠鑰岃揪鍒�鑷姩鍥炴敹鍐呭瓨鐨勭洰鐨勶紝Java璇█娌℃湁鎻愪緵閲婃斁宸插垎閰嶅唴瀛樼殑鏄剧ず鎿嶄綔鏂规硶銆�

78銆佸瀮鍦惧洖鏀剁殑浼樼偣鍜屽師鐞嗐�傚苟鑰冭檻2绉嶅洖鏀舵満鍒躲��

Java璇█涓竴涓樉钁楃殑鐗圭偣灏辨槸寮曞叆浜嗗瀮鍦惧洖鏀舵満鍒讹紝浣縞++绋嬪簭鍛樻渶澶寸柤鐨勫唴瀛樼鐞嗙殑闂杩庡垉鑰岃В锛屽畠浣垮緱Java绋嬪簭鍛樺湪缂栧啓绋嬪簭鐨勬椂鍊欎笉鍐嶉渶瑕佽�冭檻鍐呭瓨绠$悊銆傜敱浜庢湁涓瀮鍦惧洖鏀舵満鍒讹紝Java涓殑瀵硅薄涓嶅啀鏈�"浣滅敤鍩�"鐨勬蹇碉紝鍙湁瀵硅薄鐨勫紩鐢ㄦ墠鏈�"浣滅敤鍩�"銆傚瀮鍦惧洖鏀跺彲浠ユ湁鏁堢殑闃叉鍐呭瓨娉勯湶锛屾湁鏁堢殑浣跨敤鍙互浣跨敤鐨勫唴瀛樸�傚瀮鍦惧洖鏀跺櫒閫氬父鏄綔涓轰竴涓崟鐙殑浣庣骇鍒殑绾跨▼杩愯锛屼笉鍙鐭ョ殑鎯呭喌涓嬪鍐呭瓨鍫嗕腑宸茬粡姝讳骸鐨勬垨鑰呴暱鏃堕棿娌℃湁浣跨敤鐨勫璞¤繘琛屾竻妤氬拰鍥炴敹锛岀▼搴忓憳涓嶈兘瀹炴椂鐨勮皟鐢ㄥ瀮鍦惧洖鏀跺櫒瀵规煇涓璞℃垨鎵�鏈夊璞¤繘琛屽瀮鍦惧洖鏀躲�傚洖鏀舵満鍒舵湁鍒嗕唬澶嶅埗鍨冨溇鍥炴敹鍜屾爣璁板瀮鍦惧洖鏀讹紝澧為噺鍨冨溇鍥炴敹銆�

79銆佸瀮鍦惧洖鏀跺櫒鐨勫熀鏈師鐞嗘槸浠�涔堬紵鍨冨溇鍥炴敹鍣ㄥ彲浠ラ┈涓婂洖鏀跺唴瀛樺悧锛熸湁浠�涔堝姙娉曚富鍔ㄩ�氱煡铏氭嫙鏈鸿繘琛屽瀮鍦惧洖鏀讹紵

瀵逛簬GC鏉ヨ锛屽綋绋嬪簭鍛樺垱寤哄璞℃椂锛孏C灏卞紑濮嬬洃鎺ц繖涓璞$殑鍦板潃銆佸ぇ灏忎互鍙婁娇鐢ㄦ儏鍐点�傞�氬父锛孏C閲囩敤鏈夊悜鍥剧殑鏂瑰紡璁板綍鍜岀鐞�鍫�(heap)涓殑鎵�鏈夊璞°�傞�氳繃杩欑鏂瑰紡纭畾鍝簺瀵硅薄鏄�"鍙揪鐨�"锛屽摢浜涘璞℃槸"涓嶅彲杈剧殑"銆傚綋GC纭畾涓�浜涘璞′负"涓嶅彲杈�"鏃讹紝GC灏辨湁璐d换鍥炴敹杩欎簺鍐呭瓨绌洪棿銆傚彲浠ャ�傜▼搴忓憳鍙互鎵嬪姩鎵цSystem.gc()锛岄�氱煡GC杩愯锛屼絾鏄疛ava璇█瑙勮寖骞朵笉淇濊瘉GC涓�瀹氫細鎵ц銆�

80銆佷粈涔堟椂鍊欑敤assert銆�

assertion(鏂█)鍦ㄨ蒋浠跺紑鍙戜腑鏄竴绉嶅父鐢ㄧ殑璋冭瘯鏂瑰紡锛屽緢澶氬紑鍙戣瑷�涓兘鏀寔杩欑鏈哄埗銆傚湪瀹炵幇涓紝assertion灏辨槸鍦ㄧ▼搴忎腑鐨勪竴鏉¤鍙ワ紝瀹冨涓�涓猙oolean琛ㄨ揪寮忚繘琛屾鏌ワ紝涓�涓纭▼搴忓繀椤讳繚璇佽繖涓猙oolean琛ㄨ揪寮忕殑鍊间负true锛涘鏋滆鍊间负false锛岃鏄庣▼搴忓凡缁忓浜庝笉姝g‘鐨勭姸鎬佷笅锛宎ssert灏嗙粰鍑鸿鍛婃垨閫�鍑恒�備竴鑸潵璇达紝assertion鐢ㄤ簬淇濊瘉绋嬪簭鏈�鍩烘湰銆佸叧閿殑姝g‘鎬с��assertion妫�鏌ラ�氬父鍦ㄥ紑鍙戝拰娴嬭瘯鏃跺紑鍚�備负浜嗘彁楂樻�ц兘锛屽湪杞欢鍙戝竷鍚庯紝assertion妫�鏌ラ�氬父鏄叧闂殑銆�

packagecom.huawei.interview;

publicclassAssertTest {

/**

*@paramargs

*/

public static voidmain(String[] args) {

//TODOAuto-generated method stub

inti = 0;

for(i=0;i<5;i++)

{

System.out.println(i);

}

//鍋囪绋嬪簭涓嶅皬蹇冨浜嗕竴鍙�--i;

--i;

asserti==5;

}

}

81銆乯ava涓細瀛樺湪鍐呭瓨娉勬紡鍚楋紝璇风畝鍗曟弿杩般��

鎵�璋撳唴瀛樻硠闇插氨鏄寚涓�涓笉鍐嶈绋嬪簭浣跨敤鐨勫璞℃垨鍙橀噺涓�鐩磋鍗犳嵁鍦ㄥ唴瀛樹腑銆俲ava涓湁鍨冨溇鍥炴敹鏈哄埗锛屽畠鍙互淇濊瘉涓�瀵硅薄涓嶅啀琚紩鐢ㄧ殑鏃跺�欙紝鍗冲璞$紪绋嬩簡瀛ゅ効鐨勬椂鍊欙紝瀵硅薄灏嗚嚜鍔ㄨ鍨冨溇鍥炴敹鍣ㄤ粠鍐呭瓨涓竻闄ゆ帀銆傜敱浜嶫ava浣跨敤鏈夊悜鍥剧殑鏂瑰紡杩涜鍨冨溇鍥炴敹绠$悊锛屽彲浠ユ秷闄ゅ紩鐢ㄥ惊鐜殑闂锛屼緥濡傛湁涓や釜瀵硅薄锛岀浉浜掑紩鐢紝鍙瀹冧滑鍜屾牴杩涚▼涓嶅彲杈剧殑锛岄偅涔圙C涔熸槸鍙互鍥炴敹瀹冧滑鐨勶紝渚嬪涓嬮潰鐨勪唬鐮佸彲浠ョ湅鍒拌繖绉嶆儏鍐电殑鍐呭瓨鍥炴敹锛�

packagecom.huawei.interview;

importjava.io.IOException;

publicclassGarbageTest {

/**

*@paramargs

*@throwsIOException

*/

public static voidmain(String[] args)throwsIOException {

//TODOAuto-generated method stub

try{

gcTest();

}catch(IOException e) {

//TODOAuto-generated catch block

e.printStackTrace();

}

System.out.println("hasexited gcTest!");

System.in.read();

System.in.read();

System.out.println("out begingc!");

for(inti=0;i<100;i++)

{

System.gc();

System.in.read();

System.in.read();

}

}

private static voidgcTest()throwsIOException {

System.in.read();

System.in.read();

Person p1 =newPerson();

System.in.read();

System.in.read();

Person p2 =newPerson();

p1.setMate(p2);

p2.setMate(p1);

System.out.println("beforeexit gctest!");

System.in.read();

System.in.read();

System.gc();

System.out.println("exitgctest!");

}

private static classPerson

{

byte[] data =new byte[20000000];

Person mate =null;

public voidsetMate(Personother)

{

mate = other;

}

}

}

java涓殑鍐呭瓨娉勯湶鐨勬儏鍐碉細闀跨敓鍛藉懆鏈熺殑瀵硅薄鎸佹湁鐭敓鍛藉懆鏈熷璞$殑寮曠敤灏卞緢鍙兘鍙戠敓鍐呭瓨娉勯湶锛屽敖绠$煭鐢熷懡鍛ㄦ湡瀵硅薄宸茬粡涓嶅啀闇�瑕侊紝浣嗘槸鍥犱负闀跨敓鍛藉懆鏈熷璞℃寔鏈夊畠鐨勫紩鐢ㄨ�屽鑷翠笉鑳借鍥炴敹锛岃繖灏辨槸java涓唴瀛樻硠闇茬殑鍙戠敓鍦烘櫙锛岄�氫織鍦拌锛屽氨鏄▼搴忓憳鍙兘鍒涘缓浜嗕竴涓璞★紝浠ュ悗涓�鐩翠笉鍐嶄娇鐢ㄨ繖涓璞★紝杩欎釜瀵硅薄鍗翠竴鐩磋寮曠敤锛屽嵆杩欎釜瀵硅薄鏃犵敤浣嗘槸鍗存棤娉曡鍨冨溇鍥炴敹鍣ㄥ洖鏀剁殑锛岃繖灏辨槸java涓彲鑳藉嚭鐜板唴瀛樻硠闇茬殑鎯呭喌锛屼緥濡傦紝缂撳瓨绯荤粺锛屾垜浠姞杞戒簡涓�涓璞℃斁鍦ㄧ紦瀛樹腑(渚嬪鏀惧湪涓�涓叏灞�map瀵硅薄涓�)锛岀劧鍚庝竴鐩翠笉鍐嶄娇鐢ㄥ畠锛岃繖涓璞′竴鐩磋缂撳瓨寮曠敤锛屼絾鍗翠笉鍐嶈浣跨敤銆�

妫�鏌ava涓殑鍐呭瓨娉勯湶锛屼竴瀹氳璁╃▼搴忓皢鍚勭鍒嗘敮鎯呭喌閮藉畬鏁存墽琛屽埌绋嬪簭缁撴潫锛岀劧鍚庣湅鏌愪釜瀵硅薄鏄惁琚娇鐢ㄨ繃锛屽鏋滄病鏈夛紝鍒欐墠鑳藉垽瀹氳繖涓璞″睘浜庡唴瀛樻硠闇层��

濡傛灉涓�涓閮ㄧ被鐨勫疄渚嬪璞$殑鏂规硶杩斿洖浜嗕竴涓唴閮ㄧ被鐨勫疄渚嬪璞★紝杩欎釜鍐呴儴绫诲璞¤闀挎湡寮曠敤浜嗭紝鍗充娇閭d釜澶栭儴绫诲疄渚嬪璞′笉鍐嶈浣跨敤锛屼絾鐢变簬鍐呴儴绫绘寔涔呭閮ㄧ被鐨勫疄渚嬪璞★紝杩欎釜澶栭儴绫诲璞″皢涓嶄細琚瀮鍦惧洖鏀讹紝杩欎篃浼氶�犳垚鍐呭瓨娉勯湶銆�

涓嬮潰鍐呭鏉ヨ嚜浜庣綉涓婏紙涓昏鐗圭偣灏辨槸娓呯┖鍫嗘爤涓殑鏌愪釜鍏冪礌锛屽苟涓嶆槸褰诲簳鎶婂畠浠庢暟缁勪腑鎷挎帀锛岃�屾槸鎶婂瓨鍌ㄧ殑鎬绘暟鍑忓皯锛屾湰浜哄啓寰楀彲浠ユ瘮杩欎釜濂斤紝鍦ㄦ嬁鎺夋煇涓厓绱犳椂锛岄『渚夸篃璁╁畠浠庢暟缁勪腑娑堝け锛屽皢閭d釜鍏冪礌鎵�鍦ㄧ殑浣嶇疆鐨勫�艰缃负null鍗冲彲锛夛細

鎴戝疄鍦ㄦ兂涓嶅埌姣旈偅涓爢鏍堟洿缁忓吀鐨勪緥瀛愪簡,浠ヨ嚧浜庢垜杩樿寮曠敤鍒汉鐨勪緥瀛愶紝涓嬮潰鐨勪緥瀛愪笉鏄垜鎯冲埌鐨勶紝鏄功涓婄湅鍒扮殑锛屽綋鐒跺鏋滄病鏈夊湪涔︿笂鐪嬪埌锛屽彲鑳借繃涓�娈垫椂闂存垜鑷繁涔熸兂鐨勫埌锛屽彲鏄偅鏃舵垜璇存槸鎴戣嚜宸辨兂鍒扮殑涔熸病鏈変汉鐩镐俊鐨勩��

public class Stack {

private Object[] elements=new Object[10];

private int size = 0;

public void push(Object e){

ensureCapacity();

elements[size++] = e;

}

public Object pop(){

if( size == 0)

throw new EmptyStackException();

return elements[--size];

}

private void ensureCapacity(){

if(elements.length == size){

Object[] oldElements = elements;

elements = new Object[2 * elements.length+1];

System.arraycopy(oldElements,0, elements, 0, size);

}

}

}

涓婇潰鐨勫師鐞嗗簲璇ュ緢绠�鍗曪紝鍋囧鍫嗘爤鍔犱簡10涓厓绱狅紝鐒跺悗鍏ㄩ儴寮瑰嚭鏉ワ紝铏界劧鍫嗘爤鏄┖鐨勶紝娌℃湁鎴戜滑瑕佺殑涓滆タ锛屼絾鏄繖鏄釜瀵硅薄鏄棤娉曞洖鏀剁殑锛岃繖涓墠绗﹀悎浜嗗唴瀛樻硠闇茬殑涓や釜鏉′欢锛氭棤鐢紝鏃犳硶鍥炴敹銆�

浣嗘槸灏辨槸瀛樺湪杩欐牱鐨勪笢瑗夸篃涓嶄竴瀹氫細瀵艰嚧浠�涔堟牱鐨勫悗鏋滐紝濡傛灉杩欎釜鍫嗘爤鐢ㄧ殑姣旇緝灏戯紝涔熷氨娴垂浜嗗嚑涓狵鍐呭瓨鑰屽凡锛屽弽姝f垜浠殑鍐呭瓨閮戒笂G浜嗭紝鍝噷浼氭湁浠�涔堝奖鍝嶏紝鍐嶈杩欎釜涓滆タ寰堝揩灏变細琚洖鏀剁殑锛屾湁浠�涔堝叧绯汇�備笅闈㈢湅涓や釜渚嬪瓙銆�

渚嬪瓙1

public class Bad{

public static Stack s=Stack();

static{

s.push(new Object());

s.pop(); //杩欓噷鏈変竴涓璞″彂鐢熷唴瀛樻硠闇�

s.push(new Object()); //涓婇潰鐨勫璞″彲浠ヨ鍥炴敹浜嗭紝绛変簬鏄嚜鎰堜簡

}

}

鍥犱负鏄痵tatic锛屽氨涓�鐩村瓨鍦ㄥ埌绋嬪簭閫�鍑猴紝浣嗘槸鎴戜滑涔熷彲浠ョ湅鍒板畠鏈夎嚜鎰堝姛鑳斤紝灏辨槸璇村鏋滀綘鐨凷tack鏈�澶氭湁100涓璞★紝閭d箞鏈�澶氫篃灏卞彧鏈�100涓璞℃棤娉曡鍥炴敹鍏跺疄杩欎釜搴旇寰堝鏄撶悊瑙o紝Stack鍐呴儴鎸佹湁100涓紩鐢紝鏈�鍧忕殑鎯呭喌灏辨槸浠栦滑閮芥槸鏃犵敤鐨勶紝鍥犱负鎴戜滑涓�鏃︽斁鏂扮殑杩涘彇锛屼互鍓嶇殑寮曠敤鑷劧娑堝け锛�

鍐呭瓨娉勯湶鐨勫彟澶栦竴绉嶆儏鍐碉細褰撲竴涓璞¤瀛樺偍杩汬ashSet闆嗗悎涓互鍚庯紝灏变笉鑳戒慨鏀硅繖涓璞′腑鐨勯偅浜涘弬涓庤绠楀搱甯屽�肩殑瀛楁浜嗭紝鍚﹀垯锛屽璞′慨鏀瑰悗鐨勫搱甯屽�间笌鏈�鍒濆瓨鍌ㄨ繘HashSet闆嗗悎涓椂鐨勫搱甯屽�煎氨涓嶅悓浜嗭紝鍦ㄨ繖绉嶆儏鍐典笅锛屽嵆浣垮湪contains鏂规硶浣跨敤璇ュ璞$殑褰撳墠寮曠敤浣滀负鐨勫弬鏁板幓HashSet闆嗗悎涓绱㈠璞★紝涔熷皢杩斿洖鎵句笉鍒板璞$殑缁撴灉锛岃繖涔熶細瀵艰嚧鏃犳硶浠嶩ashSet闆嗗悎涓崟鐙垹闄ゅ綋鍓嶅璞★紝閫犳垚鍐呭瓨娉勯湶銆�

82銆佽兘涓嶈兘鑷繁鍐欎釜绫伙紝涔熷彨java.lang.String锛�

鍙互锛屼絾鍦ㄥ簲鐢ㄧ殑鏃跺�欙紝闇�瑕佺敤鑷繁鐨勭被鍔犺浇鍣ㄥ幓鍔犺浇锛屽惁鍒欙紝绯荤粺鐨勭被鍔犺浇鍣ㄦ案杩滃彧鏄幓鍔犺浇jre.jar鍖呬腑鐨勯偅涓猨ava.lang.String銆傜敱浜庡湪tomcat鐨剋eb搴旂敤绋嬪簭涓紝閮芥槸鐢眞ebapp鑷繁鐨勭被鍔犺浇鍣ㄥ厛鑷繁鍔犺浇WEB-INF/classess鐩綍涓殑绫伙紝鐒跺悗鎵嶅鎵樹笂绾х殑绫诲姞杞藉櫒鍔犺浇锛屽鏋滄垜浠湪tomcat鐨剋eb搴旂敤绋嬪簭涓啓涓�涓猨ava.lang.String锛岃繖鏃跺�橲ervlet绋嬪簭鍔犺浇鐨勫氨鏄垜浠嚜宸卞啓鐨刯ava.lang.String锛屼絾鏄繖涔堝共灏变細鍑哄緢澶氭綔鍦ㄧ殑闂锛屽師鏉ユ墍鏈夌敤浜唈ava.lang.String绫荤殑閮藉皢鍑虹幇闂銆�

铏界劧java鎻愪緵浜�endorsed鎶�鏈紝鍙互瑕嗙洊jdk涓殑鏌愪簺绫伙紝鍏蜂綋鍋氭硶鏄��.銆備絾鏄紝鑳藉琚鐩栫殑绫绘槸鏈夐檺鍒惰寖鍥达紝鍙嶆涓嶅寘鎷琷ava.lang杩欐牱鐨勫寘涓殑绫汇��

锛堜笅闈㈢殑渚嬪涓昏鏄究浜庡ぇ瀹跺涔犵悊瑙e彧鐢紝涓嶈浣滀负绛旀鐨勪竴閮ㄥ垎锛屽惁鍒欙紝浜哄鎬�鐤戞槸棰樼洰娉勯湶浜嗭級渚嬪锛岃繍琛屼笅闈㈢殑绋嬪簭锛�

packagejava.lang;

publicclassString {

/**

*@paramargs

*/

public static voidmain(String[] args) {

//TODOAuto-generated method stub

System.out.println("string");

}

}

鎶ュ憡鐨勯敊璇涓嬶細

java.lang.NoSuchMethodError:main

Exception inthread "main"

杩欐槸鍥犱负鍔犺浇浜唈re鑷甫鐨刯ava.lang.String锛岃�岃绫讳腑娌℃湁main鏂规硶銆�

83. Java浠g爜鏌ラ敊

1.

abstract class Name {

private String name;

public abstract boolean isStupidName(String name) {}

}

澶т緺浠紝杩欐湁浣曢敊璇�?

绛旀: 閿欍�俛bstract method蹇呴』浠ュ垎鍙风粨灏撅紝涓斾笉甯﹁姳鎷彿銆�

2.

public class Something {

void doSomething () {

private String s = "";

int l = s.length();

}

}

鏈夐敊鍚�?

绛旀: 閿欍�傚眬閮ㄥ彉閲忓墠涓嶈兘鏀剧疆浠讳綍璁块棶淇グ绗� (private锛宲ublic锛屽拰protected)銆俧inal鍙互鐢ㄦ潵淇グ灞�閮ㄥ彉閲�

(final濡傚悓abstract鍜宻trictfp锛岄兘鏄潪璁块棶淇グ绗︼紝strictfp鍙兘淇グclass鍜宮ethod鑰岄潪variable)銆�

3.

abstract class Something {

private abstract String doSomething ();

}

杩欏ソ鍍忔病浠�涔堥敊鍚�?

绛旀: 閿欍�俛bstract鐨刴ethods涓嶈兘浠rivate淇グ銆俛bstract鐨刴ethods灏辨槸璁╁瓙绫籭mplement(瀹炵幇)鍏蜂綋缁嗚妭鐨勶紝鎬庝箞鍙互鐢╬rivate鎶奱bstract

method灏侀攣璧锋潵鍛�? (鍚岀悊锛宎bstract method鍓嶄笉鑳藉姞final)銆�

4.

public class Something {

public int addOne(final int x) {

return ++x;

}

}

杩欎釜姣旇緝鏄庢樉銆�

绛旀: 閿欍�俰nt x琚慨楗版垚final锛屾剰鍛崇潃x涓嶈兘鍦╝ddOne method涓淇敼銆�

5.

public class Something {

public static void main(String[] args) {

Other o = new Other();

new Something().addOne(o);

}

public void addOne(final Other o) {

o.i++;

}

}

class Other {

public int i;

}

鍜屼笂闈㈢殑寰堢浉浼硷紝閮芥槸鍏充簬final鐨勯棶棰橈紝杩欐湁閿欏悧?

绛旀: 姝g‘銆傚湪addOne method涓紝鍙傛暟o琚慨楗版垚final銆傚鏋滃湪addOne method閲屾垜浠慨鏀逛簡o鐨剅eference

(姣斿: o = new Other();)锛岄偅涔堝鍚屼笂渚嬭繖棰樹篃鏄敊鐨勩�備絾杩欓噷淇敼鐨勬槸o鐨刴ember vairable

(鎴愬憳鍙橀噺)锛岃�宱鐨剅eference骞舵病鏈夋敼鍙樸��

6.

class Something {

int i;

public void doSomething() {

System.out.println("i = "+ i);

}

}

鏈変粈涔堥敊鍛�? 鐪嬩笉鍑烘潵鍟娿��

绛旀: 姝g‘銆傝緭鍑虹殑鏄�"i = 0"銆俰nt i灞炴柤instant variable (瀹炰緥鍙橀噺锛屾垨鍙垚鍛樺彉閲�)銆俰nstant variable鏈塪efault value銆俰nt鐨刣efault value鏄�0銆�

7.

class Something {

final int i;

public void doSomething() {

System.out.println("i = "+ i);

}

}

鍜屼笂闈竴棰樺彧鏈変竴涓湴鏂逛笉鍚岋紝灏辨槸澶氫簡涓�涓猣inal銆傝繖闅鹃亾灏遍敊浜嗗悧?

绛旀: 閿欍�俧inal int i鏄釜final鐨刬nstant variable (瀹炰緥鍙橀噺锛屾垨鍙垚鍛樺彉閲�)銆俧inal鐨刬nstant variable娌℃湁default value锛屽繀椤诲湪constructor (鏋勯�犲櫒)缁撴潫涔嬪墠琚祴浜堜竴涓槑纭殑鍊笺�傚彲浠ヤ慨鏀逛负"final int i =0;"銆�

8.

public class Something {

public static void main(String[] args) {

Something s = new Something();

System.out.println("s.doSomething() returns " + doSomething());

}

public String doSomething() {

return "Do something ...";

}

}

鐪嬩笂鍘诲緢瀹岀編銆�

绛旀: 閿欍�傜湅涓婂幓鍦╩ain閲宑all doSomething娌℃湁浠�涔堥棶棰橈紝姣曠珶涓や釜methods閮藉湪鍚屼竴涓猚lass閲屻�備絾浠旂粏鐪嬶紝main鏄痵tatic鐨勩�俿tatic method涓嶈兘鐩存帴call non-staticmethods銆傚彲鏀规垚"System.out.println("s.doSomething()returns " + s.doSomething());"銆傚悓鐞嗭紝static method涓嶈兘璁块棶non-static instant variable銆�

9.

姝ゅ锛孲omething绫荤殑鏂囦欢鍚嶅彨OtherThing.java

class Something {

private static void main(String[] something_to_do){

System.out.println("Dosomething ...");

}

}

杩欎釜濂藉儚寰堟槑鏄俱��

绛旀: 姝g‘銆備粠鏉ユ病鏈変汉璇磋繃Java鐨凜lass鍚嶅瓧蹇呴』鍜屽叾鏂囦欢鍚嶇浉鍚屻�備絾public class鐨勫悕瀛楀繀椤诲拰鏂囦欢鍚嶇浉鍚屻��

10锛�

interface 聽A{

int x = 0;

}

class B{

int x =1;

}

class C extends B implements A {

public void pX(){

System.out.println(x);

}

public static void main(String[] args) {

new C().pX();

}

}

绛旀锛氶敊璇�傚湪缂栬瘧鏃朵細鍙戠敓閿欒(閿欒鎻忚堪涓嶅悓鐨凧VM鏈変笉鍚岀殑淇℃伅锛屾剰鎬濆氨鏄湭鏄庣‘鐨剎璋冪敤锛屼袱涓獂閮藉尮閰嶏紙灏辫薄鍦ㄥ悓鏃秈mport java.util鍜宩ava.sql涓や釜鍖呮椂鐩存帴澹版槑Date涓�鏍凤級銆傚浜庣埗绫荤殑鍙橀噺,鍙互鐢╯uper.x鏉ユ槑纭紝鑰屾帴鍙g殑灞炴�ч粯璁ら殣鍚负 public staticfinal.鎵�浠ュ彲浠ラ�氳繃A.x鏉ユ槑纭��

11.

interface Playable {

void play();

}

interface Bounceable {

void play();

}

interface Rollable extends Playable, Bounceable {

Ball ball = new Ball("PingPang");

}

class Ball implements Rollable {

private String name;

public String getName() {

return name;

}

public Ball(String name) {

this.name =name;

}

public void play() {

ball = newBall("Football");

System.out.println(ball.getName());

}

}

杩欎釜閿欒涓嶅鏄撳彂鐜般��

绛旀: 閿欍��"interfaceRollable extends Playable, Bounceable"娌℃湁闂銆俰nterface鍙户鎵垮涓猧nterfaces锛屾墍浠ヨ繖閲屾病閿欍�傞棶棰樺嚭鍦╥nterface Rollable閲岀殑"Ball ball =new Ball("PingPang");"銆備换浣曞湪interface閲屽0鏄庣殑interface variable (鎺ュ彛鍙橀噺锛屼篃鍙О鎴愬憳鍙橀噺)锛岄粯璁や负public static final銆備篃灏辨槸璇�"Ball ball = new Ball("PingPang");"瀹為檯涓婃槸"public staticfinal Ball ball = new Ball("PingPang");"銆傚湪Ball绫荤殑Play()鏂规硶涓紝"ball = newBall("Football");"鏀瑰彉浜哹all鐨剅eference锛岃�岃繖閲岀殑ball鏉ヨ嚜Rollable interface锛孯ollable interface閲岀殑ball鏄痯ublic static final鐨勶紝final鐨刼bject鏄笉鑳借鏀瑰彉reference鐨勩�傚洜姝ょ紪璇戝櫒灏嗗湪"ball = newBall("Football");"杩欓噷鏄剧ず鏈夐敊銆�

浜�.绠楁硶涓庣紪绋�

1銆佺紪鍐欎竴涓▼搴忥紝灏哸.txt鏂囦欢涓殑鍗曡瘝涓巄.txt鏂囦欢涓殑鍗曡瘝浜ゆ浛鍚堝苟鍒癱.txt鏂囦欢涓紝a.txt鏂囦欢涓殑鍗曡瘝鐢ㄥ洖杞︾鍒嗛殧锛宐.txt鏂囦欢涓敤鍥炶溅鎴栫┖鏍艰繘琛屽垎闅斻��

绛旓細

packagecn.itcast;

import java.io.File;

import java.io.FileReader;

import java.io.FileWriter;

public class MainClass{

public static voidmain(String[] args) throws Exception{

FileManager a= new FileManager("a.txt",new char[]{'\n'});

FileManager b= new FileManager("b.txt",new char[]{'\n',' '});

FileWriter c= new FileWriter("c.txt");

String aWord= null;

String bWord= null;

while((aWord= a.nextWord()) !=null ){

c.write(aWord+ "\n");

bWord= b.nextWord();

if(bWord!= null)

c.write(bWord+ "\n");

}

while((bWord= b.nextWord()) != null){

c.write(bWord+ "\n");

}

c.close();

}

}

class FileManager{

String[] words =null;

int pos = 0;

Public FileManager(String filename,char[] seperators) throws Exception{

File f = new File(filename);

FileReader reader = new FileReader(f);

char[] buf =new char[(int)f.length()];

int len =reader.read(buf);

Stringresults = new String(buf,0,len);

String regex= null;

if(seperators.length>1 ){

regex= "" + seperators[0] + "|" + seperators[1];

}else{

regex= "" + seperators[0];

}

words =results.split(regex);

}

public StringnextWord(){

if(pos ==words.length)

returnnull;

returnwords[pos++];

}

}

2銆佺紪鍐欎竴涓▼搴忥紝灏哾:\java鐩綍涓嬬殑鎵�鏈�.java鏂囦欢澶嶅埗鍒癲:\jad鐩綍涓嬶紝骞跺皢鍘熸潵鏂囦欢鐨勬墿灞曞悕浠�.java鏀逛负.jad銆�

锛堝ぇ瀹舵鍦ㄥ仛涓婇潰杩欓亾棰橈紝缃戜笂杩熷埌鐨勬湅鍙嬩篃璇峰仛鍋氳繖閬撻锛屾壘宸ヤ綔蹇呴』鑳界紪鍐欒繖浜涚畝鍗曢棶棰樼殑浠g爜锛侊級

绛旓細listFiles鏂规硶鎺ュ彈涓�涓狥ileFilter瀵硅薄锛�杩欎釜FileFilter瀵硅薄灏辨槸杩囪檻鐨勭瓥鐣ュ璞�锛屼笉鍚岀殑浜烘彁渚涗笉鍚岀殑FileFilter瀹炵幇锛屽嵆鎻愪緵浜嗕笉鍚岀殑杩囨护绛栫暐銆�

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.FilenameFilter;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

public class Jad2Java {

public static voidmain(String[] args) throws Exception {

File srcDir =new File("java");

if(!(srcDir.exists()&& srcDir.isDirectory()))

thrownew Exception("鐩綍涓嶅瓨鍦�");

File[] files= srcDir.listFiles(

newFilenameFilter(){

publicboolean accept(File dir, String name) {

returnname.endsWith(".java");

}

}

);

System.out.println(files.length);

File destDir= new File("jad");

if(!destDir.exists())destDir.mkdir();

for(File f:files){

FileInputStream 聽fis = new FileInputStream(f);

StringdestFileName = f.getName().replaceAll("\\.java$", ".jad");

FileOutputStreamfos = new FileOutputStream(new File(destDir,destFileName));

copy(fis,fos);

fis.close();

fos.close();

}

}

private static voidcopy(InputStream ips,OutputStream ops) throws Exception{

int len = 0;

byte[] buf =new byte[1024];

while((len =ips.read(buf)) != -1){

ops.write(buf,0,len);

}

}

}

鐢辨湰棰樻�荤粨鐨勬�濇兂鍙婄瓥鐣ユā寮忕殑瑙f瀽锛�

1.

class jad2java{

1.寰楀埌鏌愪釜鐩綍涓嬬殑鎵�鏈夌殑java鏂囦欢闆嗗悎

1.1寰楀埌鐩綍 File srcDir = newFile("d:\\java");

1.2寰楀埌鐩綍涓嬬殑鎵�鏈塲ava鏂囦欢锛欶ile[] files =srcDir.listFiles(new MyFileFilter());

1.3鍙兂寰楀埌.java鐨勬枃浠讹細 class MyFileFilterimplememyts FileFilter{

publicboolean accept(File pathname){

returnpathname.getName().endsWith(".java")

}

}

2.灏嗘瘡涓枃浠跺鍒跺埌鍙﹀涓�涓洰褰曪紝骞舵敼鎵╁睍鍚�

2.1寰楀埌鐩爣鐩綍锛屽鏋滅洰鏍囩洰褰曚笉瀛樺湪锛屽垯鍒涘缓涔�

2.2鏍规嵁婧愭枃浠跺悕寰楀埌鐩爣鏂囦欢鍚嶏紝娉ㄦ剰瑕佺敤姝e垯琛ㄨ揪寮忥紝娉ㄦ剰.鐨勮浆涔夈��

2.3鏍规嵁琛ㄧず鐩綍鐨凢ile鍜岀洰鏍囨枃浠跺悕鐨勫瓧绗︿覆锛屽緱鍒拌〃绀虹洰鏍囨枃浠剁殑File銆�

//瑕佸湪纭洏涓噯纭湴鍒涘缓鍑轰竴涓枃浠讹紝闇�瑕佺煡閬撴枃浠跺悕鍜屾枃浠剁殑鐩綍銆�

2.4灏嗘簮鏂囦欢鐨勬祦鎷疯礉鎴愮洰鏍囨枃浠舵祦锛屾嫹璐濇柟娉曠嫭绔嬫垚涓轰竴涓柟娉曪紝鏂规硶鐨勫弬鏁伴噰鐢ㄦ娊璞℃祦鐨勫舰寮忋��

//鏂规硶鎺ュ彈鐨勫弬鏁扮被鍨嬪敖閲忛潰鍚戠埗绫伙紝瓒婃娊璞¤秺濂斤紝杩欐牱閫傚簲闈㈡洿瀹藉箍銆�

}

鍒嗘瀽listFiles鏂规硶鍐呴儴鐨勭瓥鐣ユā寮忓疄鐜板師鐞�

File[] listFiles(FileFilter filter){

File[] files =listFiles();

//ArraylistacceptedFilesList = new ArrayList();

File[] acceptedFiles= new File[files.length];

int pos = 0;

for(File file:files){

booleanaccepted = filter.accept(file);

if(accepted){

//acceptedFilesList.add(file);

acceptedFiles[pos++]= file;

}

}

Arrays.copyOf(acceptedFiles,pos);

//return(File[])accpetedFilesList.toArray();

}

3銆佺紪鍐欎竴涓埅鍙栧瓧绗︿覆鐨勫嚱鏁帮紝杈撳叆涓轰竴涓瓧绗︿覆鍜屽瓧鑺傛暟锛岃緭鍑轰负鎸夊瓧鑺傛埅鍙栫殑瀛楃涓诧紝浣嗚淇濊瘉姹夊瓧涓嶈鎴彇鍗婁釜锛屽鈥滄垜ABC鈥濓紝4锛屽簲璇ユ埅鍙栤�滄垜AB鈥濓紝杈撳叆鈥滄垜ABC姹塂EF鈥濓紝6锛屽簲璇ヨ緭鍑衡�滄垜ABC鈥濓紝鑰屼笉鏄�滄垜ABC+姹夌殑鍗婁釜鈥濄��

绛旓細

棣栧厛瑕佷簡瑙d腑鏂囧瓧绗︽湁澶氱缂栫爜鍙婂悇绉嶇紪鐮佺殑鐗瑰緛銆�

鍋囪n涓鸿鎴彇鐨勫瓧鑺傛暟銆�

public static voidmain(String[] args) throws Exception{

String str ="鎴慳鐖变腑鍗巃bc鎴戠埍浼犳櫤def';

String str ="鎴慉BC姹�";

int num =trimGBK(str.getBytes("GBK"),5);

System.out.println(str.substring(0,num));

}

public staticint 聽trimGBK(byte[] buf,int n){

int num = 0;

booleanbChineseFirstHalf = false;

for(inti=0;i

{

if(buf[i]<0&& !bChineseFirstHalf){

bChineseFirstHalf= true;

}else{

num++;

bChineseFirstHalf= false;

}

}

return num;

}

4銆佹湁涓�涓瓧绗︿覆锛屽叾涓寘鍚腑鏂囧瓧绗︺�佽嫳鏂囧瓧绗﹀拰鏁板瓧瀛楃锛岃缁熻鍜屾墦鍗板嚭鍚勪釜瀛楃鐨勪釜鏁般��

绛旓細鍝堝搱锛屽叾瀹炲寘鍚腑鏂囧瓧绗︺�佽嫳鏂囧瓧绗︺�佹暟瀛楀瓧绗﹀師鏉ユ槸鍑洪鑰呮斁鐨勭儫闆惧脊銆�

String content = 鈥滀腑鍥絘adf鐨�111钀╞bb鑿茬殑zz钀ㄨ彶鈥�;

HashMap map = new HashMap();

for(int i=0;i

{

char c =content.charAt(i);

Integer num =map.get(c);

if(num == null)

num = 1;

else

num = num +1;

map.put(c,num);

}

for(Map.EntrySet entry : map)

{

system.out.println(entry.getkey()+ 鈥�:鈥� + entry.getValue());

}

浼拌鏄綋鍒濋潰璇曠殑閭d釜瀛﹀憳琛ㄨ堪涓嶆竻妤氾紝闂寰堝彲鑳芥槸锛�

濡傛灉涓�涓插瓧绗﹀"aaaabbc涓浗1512"瑕佸垎鍒粺璁¤嫳鏂囧瓧绗︾殑鏁伴噺锛屼腑鏂囧瓧绗︾殑鏁伴噺锛屽拰鏁板瓧瀛楃鐨勬暟閲忥紝鍋囪瀛楃涓病鏈変腑鏂囧瓧绗︺�佽嫳鏂囧瓧绗︺�佹暟瀛楀瓧绗︿箣澶栫殑鍏朵粬鐗规畩瀛楃銆�

int engishCount;

int chineseCount;

int digitCount;

for(int i=0;i

{

charch = str.charAt(i);

if(ch>=鈥�0鈥�&& ch<=鈥�9鈥�)

{

digitCount++

}

elseif((ch>=鈥檃鈥�&& ch<=鈥檢鈥�) || (ch>=鈥橝鈥� && ch<=鈥橺鈥�))

{

engishCount++;

}

else

{

chineseCount++;

}

}

System.out.println(鈥︹�︹�︹�︹��);

5銆佽鏄庣敓娲讳腑閬囧埌鐨勪簩鍙夋爲锛岀敤java瀹炵幇浜屽弶鏍�

杩欐槸缁勫悎璁捐妯″紡銆�

鎴戞湁寰堝涓�(鍋囪10涓囦釜)鏁版嵁瑕佷繚瀛樿捣鏉ワ紝浠ュ悗杩橀渶瑕佷粠淇濆瓨鐨勮繖浜涙暟鎹腑妫�绱㈡槸鍚﹀瓨鍦ㄦ煇涓暟鎹紝锛堟垜鎯宠鍑轰簩鍙夋爲鐨勫ソ澶勶紝璇ユ�庝箞璇村憿锛熼偅灏辨槸璇村埆浜虹殑缂虹偣锛夛紝鍋囧瀛樺湪鏁扮粍涓紝閭d箞锛岀宸ц鎵剧殑鏁板瓧浣嶄簬99999閭d釜鍦版柟锛岄偅鏌ユ壘鐨勯�熷害灏嗗緢鎱紝鍥犱负瑕佷粠绗�1涓緷娆″線鍚庡彇锛屽彇鍑烘潵鍚庤繘琛屾瘮杈冦�傚钩琛′簩鍙夋爲锛堟瀯寤哄钩琛′簩鍙夋爲闇�瑕佸厛鎺掑簭锛屾垜浠繖閲屽氨涓嶄綔鑰冭檻浜嗭級鍙互寰堝ソ鍦拌В鍐宠繖涓棶棰橈紝浣嗕簩鍙夋爲鐨勯亶鍘嗭紙鍓嶅簭锛屼腑搴忥紝鍚庡簭锛夋晥鐜囪姣旀暟缁勪綆寰堝锛屽師鐞嗗涓嬪浘锛�

浠g爜濡備笅锛�

packagecom.huawei.interview;

publicclassNode {

public intvalue;

publicNode left;

publicNode right;

public voidstore(intvalue)

{

if(value<this.value)

{

if(left ==null)

{

left =newNode();

left.value=value;

}

else

{

left.store(value);

}

}

else if(value>this.value)

{

if(right ==null)

{

right =newNode();

right.value=value;

}

else

{

right.store(value);

}

}

}

public booleanfind(intvalue)

{

System.out.println("happen" +this.value);

if(value ==this.value)

{

return true;

}

else if(value>this.value)

{

if(right ==null)returnfalse;

returnright.find(value);

}else

{

if(left ==null)returnfalse;

returnleft.find(value);

}

}

public 聽voidpreList()

{

System.out.print(this.value+ ",");

if(left!=null)left.preList();

if(right!=null) right.preList();

}

public voidmiddleList()

{

if(left!=null)left.preList();

System.out.print(this.value+ ",");

if(right!=null)right.preList();

}

public voidafterList()

{

if(left!=null)left.preList();

if(right!=null)right.preList();

System.out.print(this.value+ ",");

}

public static voidmain(String [] args)

{

int[] data =new int[20];

for(inti=0;i

{

data[i] = (int)(Math.random()*100)+ 1;

System.out.print(data[i] +",");

}

System.out.println();

Node root =newNode();

root.value = data[0];

for(inti=1;i

{

root.store(data[i]);

}

root.find(data[19]);

root.preList();

System.out.println();

root.middleList();

System.out.println();

root.afterList();

}

}

-----------------鍙堜竴娆′复鍦哄啓鐨勪唬鐮�---------------------------

importjava.util.Arrays;

importjava.util.Iterator;

public class Node{

private Node left;

private Node right;

private int value;

//private int num;

public Node(int value){

this.value = value;

}

public void add(int value){

if(value > this.value)

{

if(right != null)

right.add(value);

else

{

Node node = new Node(value);

right = node;

}

}

else{

if(left != null)

left.add(value);

else

{

Node node = new Node(value);

left = node;

}

}

}

public boolean find(int value){

if(value == this.value) return true;

else if(value > this.value){

if(right == null) return false;

else return right.find(value);

}else{

if(left == null) return false;

else return left.find(value);

}

}

public void display(){

System.out.println(value);

if(left != null) left.display();

if(right != null) right.display();

}

/*public Iterator iterator(){

}*/

public static void main(String[] args){

int[] values = new int[8];

for(int i=0;i<8;i++){

int num = (int)(Math.random() * 15);

//System.out.println(num);

//if(Arrays.binarySearch(values,num)<0)

if(!contains(values,num))

values[i] = num;

else

i--;

}

System.out.println(Arrays.toString(values));

Node root = new Node(values[0]);

for(int i=1;i

root.add(values[i]);

}

System.out.println(root.find(13));

root.display();

}

public static boolean contains(int [] arr,int value){

int i = 0;

for(;i

if(arr[i] == value) return true;

}

return false;

}

}

6銆佷粠绫讳技濡備笅鐨勬枃鏈枃浠朵腑璇诲彇鍑烘墍鏈夌殑濮撳悕锛屽苟鎵撳嵃鍑洪噸澶嶇殑濮撳悕鍜岄噸澶嶇殑娆℃暟锛屽苟鎸夐噸澶嶆鏁版帓搴忥細

1,寮犱笁,28

2,鏉庡洓,35

3,寮犱笁,28

4,鐜嬩簲,35

5,寮犱笁,28

6,鏉庡洓,35

7,璧靛叚,28

8,鐢颁竷,35

绋嬪簭浠g爜濡備笅锛堢瓟棰樿鍗氬緱鐢ㄤ汉鍗曚綅鐨勫枩娆紝鍖呭悕鐢ㄨ鍏徃锛岄潰璇曞墠灏辨彁鍓嶆煡濂借鍏徃鐨勭綉鍧�锛屽鏋滄煡涓嶅埌锛岀幇鍦洪棶涔熸槸鍙互鐨勩�傝繕瑕佸姞涓婂疄鐜版�濊矾鐨勬敞閲婏級锛�

packagecom.huawei.interview;

importjava.io.BufferedReader;

importjava.io.IOException;

importjava.io.InputStream;

importjava.io.InputStreamReader;

importjava.util.Comparator;

importjava.util.HashMap;

importjava.util.Iterator;

importjava.util.Map;

importjava.util.TreeSet;

publicclassGetNameTest {

/**

*@paramargs

*/

public static voidmain(String[] args) {

//TODOAuto-generated method stub

//InputStream ips =GetNameTest.class.getResourceAsStream("/com/huawei/interview/info.txt");

//鐢ㄤ笂涓�琛屾敞閲婄殑浠g爜鍜屼笅涓�琛岀殑浠g爜閮藉彲浠ワ紝鍥犱负info.txt涓嶨etNameTest绫诲湪鍚屼竴鍖呬笅闈紝鎵�浠ワ紝鍙互鐢ㄤ笅闈㈢殑鐩稿璺緞褰㈠紡

Map results =newHashMap();

InputStream ips = GetNameTest.class.getResourceAsStream("info.txt");

BufferedReader in =newBufferedReader(newInputStreamReader(ips));

String line =null;

try{

while((line=in.readLine())!=null)

{

dealLine(line,results);

}

sortResults(results);

}catch(IOException e) {

//TODOAuto-generated catchblock

e.printStackTrace();

}

}

static classUser

{

publicString name;

publicInteger value;

publicUser(String name,Integervalue)

{

this.name = name;

this.value = value;

}

@Override

public booleanequals(Object obj) {

//TODOAuto-generated methodstub

//涓嬮潰鐨勪唬鐮佹病鏈夋墽琛岋紝璇存槑寰�treeset涓鍔犳暟鎹椂锛屼笉浼氫娇鐢ㄥ埌equals鏂规硶銆�

booleanresult =super.equals(obj);

System.out.println(result);

returnresult;

}

}

private static voidsortResults(Map results) {

//TODOAuto-generated method stub

TreeSet sortedResults =newTreeSet(

newComparator(){

public intcompare(Object o1, Object o2) {

//TODOAuto-generated method stub

User user1 = (User)o1;

User user2 = (User)o2;

/*濡傛灉compareTo杩斿洖缁撴灉0锛屽垯璁や负涓や釜瀵硅薄鐩哥瓑锛屾柊鐨勫璞′笉浼氬鍔犲埌闆嗗悎涓幓

*鎵�浠ワ紝涓嶈兘鐩存帴鐢ㄤ笅闈㈢殑浠g爜锛屽惁鍒欙紝閭d簺涓暟鐩稿悓鐨勫叾浠栧鍚嶅氨鎵撳嵃涓嶅嚭鏉ャ��

* */

//returnuser1.value-user2.value;

//returnuser1.value

if(user1.value

{

return-1;

}else if(user1.value>user2.value)

{

return1;

}else

{

returnuser1.name.compareTo(user2.name);

}

}

}

);

Iterator iterator =results.keySet().iterator();

while(iterator.hasNext())

{

String name = (String)iterator.next();

Integer value =(Integer)results.get(name);

if(value > 1)

{

sortedResults.add(newUser(name,value));

}

}

printResults(sortedResults);

}

private static voidprintResults(TreeSet sortedResults)

{

Iterator iterator 聽= sortedResults.iterator();

while(iterator.hasNext())

{

User user = (User)iterator.next();

System.out.println(user.name +":" + user.value);

}

}

public static voiddealLine(String line,Map map)

{

if(!"".equals(line.trim()))

{

String [] results =line.split(",");

if(results.length == 3)

{

String name = results[1];

Integer value =(Integer)map.get(name);

if(value ==null)value = 0;

map.put(name,value + 1);

}

}

}

}

7銆佸啓涓�涓猄ingleton鍑烘潵銆�

绗竴绉嶏細楗辨眽妯″紡

public classSingleTon {

private SingleTon(){

}

//瀹炰緥鍖栨斁鍦ㄩ潤鎬佷唬鐮佸潡閲屽彲鎻愰珮绋嬪簭鐨勬墽琛屾晥鐜囷紝浣嗕篃鍙兘鏇村崰鐢ㄧ┖闂�

private final static SingleTon instance =new SingleTon();

public static SingleTon getInstance(){

return instance;

}

}

绗簩绉嶏細楗ユ眽妯″紡

public classSingleTon {

private SingleTon(){}

private static instance = null;//newSingleTon();

public static synchronized SingleTongetInstance(){

if(instance == null)

instance = new SingleTon();

return instance;

}

}

绗笁绉嶏細鐢ㄦ灇涓�

public enum SingleTon{

ONE;

}

绗笁锛氭洿瀹為檯鐨勫簲鐢紙鍦ㄤ粈涔堟儏鍐电敤鍗曚緥锛�

public classSequenceGenerator{

//涓嬮潰鏄绫昏嚜韬殑涓氬姟鍔熻兘浠g爜

private int count = 0;

public synchronized int getSequence(){

++count;

}

//涓嬮潰鏄妸璇ョ被鍙樻垚鍗曚緥鐨勪唬鐮�

private SequenceGenerator(){}

private final static instance = newSequenceGenerator();

public static SingleTon getInstance(){

return instance;

}

}

绗洓锛�

public class MemoryDao

{

private HashMap map = new HashMap();

publicvoid add(Student stu1){

map.put(SequenceGenerator.getInstance().getSequence(),stu1);

}

//鎶奙emoryDao鍙樻垚鍗曚緥

}

Singleton妯″紡涓昏浣滅敤鏄繚璇佸湪Java搴旂敤绋嬪簭涓紝涓�涓被Class鍙湁涓�涓疄渚嬪瓨鍦ㄣ��

涓�鑸琒ingleton妯″紡閫氬父鏈夊嚑绉嶇褰㈠紡:

绗竴绉嶅舰寮�: 瀹氫箟涓�涓被锛屽畠鐨勬瀯閫犲嚱鏁颁负private鐨勶紝瀹冩湁涓�涓猻tatic鐨刾rivate鐨勮绫诲彉閲忥紝鍦ㄧ被鍒濆鍖栨椂瀹炰緥璇濓紝閫氳繃涓�涓猵ublic鐨刧etInstance鏂规硶鑾峰彇瀵瑰畠鐨勫紩鐢�,缁ц�岃皟鐢ㄥ叾涓殑鏂规硶銆�

public class Singleton {

private Singleton(){}

//鍦ㄨ嚜宸卞唴閮ㄥ畾涔夎嚜宸变竴涓疄渚嬶紝鏄笉鏄緢濂囨�紵

//娉ㄦ剰杩欐槸private鍙緵鍐呴儴璋冪敤

private staticSingleton instance = new Singleton();

//杩欓噷鎻愪緵浜嗕竴涓緵澶栭儴璁块棶鏈琧lass鐨勯潤鎬佹柟娉曪紝鍙互鐩存帴璁块棶

public staticSingleton getInstance() {

return instance;

}

}

绗簩绉嶅舰寮�:

public class Singleton {

private static Singleton instance = null;

public static synchronized Singleton getInstance() {

//杩欎釜鏂规硶姣斾笂闈㈡湁鎵�鏀硅繘锛屼笉鐢ㄦ瘡娆¢兘杩涜鐢熸垚瀵硅薄锛屽彧鏄涓�娆�

//浣跨敤鏃剁敓鎴愬疄渚嬶紝鎻愰珮浜嗘晥鐜囷紒

if (instance==null)

instance锛漬ew Singleton();

return instance;

}

}

鍏朵粬褰㈠紡:

瀹氫箟涓�涓被锛屽畠鐨勬瀯閫犲嚱鏁颁负private鐨勶紝鎵�鏈夋柟娉曚负static鐨勩��

涓�鑸涓虹涓�绉嶅舰寮忚鏇村姞瀹夊叏浜�

8銆侀�掑綊绠楁硶棰�1

涓�涓暣鏁帮紝澶т簬0锛屼笉鐢ㄥ惊鐜拰鏈湴鍙橀噺锛屾寜鐓锛�2n锛�4n锛�8n鐨勯『搴忛�掑锛屽綋鍊煎ぇ浜�5000鏃讹紝鎶婂�兼寜鐓ф寚瀹氶『搴忚緭鍑烘潵銆�

渚嬶細n=1237

鍒欒緭鍑轰负锛�

1237锛�

2474锛�

4948锛�

9896锛�

9896锛�

4948锛�

2474锛�

1237锛�

鎻愮ず锛氬啓绋嬪簭鏃讹紝鍏堣嚧璋㈡寜閫掑鏂瑰紡鐨勪唬鐮侊紝鍐欏ソ閫掑鐨勪互鍚庯紝鍐嶅鍔犺�冭檻閫掑噺閮ㄥ垎銆�

public static void doubleNum(int n)

{

System.out.println(n);

if(n<=5000)

doubleNum(n*2);

System.out.println(n);

}

Gaibaota(N) = Gaibaota(N-1) + n

9銆侀�掑綊绠楁硶棰�2

绗�1涓汉10锛岀2涓瘮绗�1涓汉澶�2宀侊紝渚濇閫掓帹锛岃鐢ㄩ�掑綊鏂瑰紡璁$畻鍑虹8涓汉澶氬ぇ锛�

packagecn.itcast;

importjava.util.Date;

publicclassA1 {

public static voidmain(String [] args)

{

System.out.println(computeAge(8));

}

public static intcomputeAge(intn)

{

if(n==1)return10;

returncomputeAge(n-1) + 2;

}

}

public static voidtoBinary(intn,StringBuffer result)

{

if(n/2 != 0)

toBinary(n/2,result);

result.append(n%2);

}

10銆佹帓搴忛兘鏈夊摢鍑犵鏂规硶锛熻鍒椾妇銆傜敤JAVA瀹炵幇涓�涓揩閫熸帓搴忋��

鏈汉鍙爺绌惰繃鍐掓场鎺掑簭銆侀�夋嫨鎺掑簭鍜屽揩閫熸帓搴忥紝涓嬮潰鏄揩閫熸帓搴忕殑浠g爜锛�

public class QuickSort {

/**

* 蹇�熸帓搴�

* @param strDate

* @param left

* @param right

*/

public void quickSort(String[] strDate,int left,int right){

String middle,tempDate;

int i,j;

i=left;

j=right;

middle=strDate[(i+j)/2];

do{

while(strDate[i].compareTo(middle)<0&& i

i++; //鎵惧嚭宸﹁竟姣斾腑闂村�煎ぇ鐨勬暟

while(strDate[j].compareTo(middle)>0&& j>left)

j--; //鎵惧嚭鍙宠竟姣斾腑闂村�煎皬鐨勬暟

if(i<=j){ //灏嗗乏杈瑰ぇ鐨勬暟鍜屽彸杈瑰皬鐨勬暟杩涜鏇挎崲

tempDate=strDate[i];

strDate[i]=strDate[j];

strDate[j]=tempDate;

i++;

j--;

}

}while(i<=j); //褰撲袱鑰呬氦閿欐椂鍋滄

if(i

quickSort(strDate,i,right);//浠�

}

if(j>left){

quickSort(strDate,left,j);

}

}

/**

* @param args

*/

public static void main(String[] args){

String[] strVoid=newString[]{"11","66","22","0","55","22","0","32"};

QuickSort sort=new QuickSort();

sort.quickSort(strVoid,0,strVoid.length-1);

for(int i=0;i

System.out.println(strVoid[i]+" ");

}

}

}

11銆佹湁鏁扮粍a[n]锛岀敤java浠g爜灏嗘暟缁勫厓绱犻『搴忛鍊�

//鐢ㄤ笅闈㈢殑涔熷彲浠�

//for(inti=0,int j=a.length-1;i

importjava.util.Arrays;

public classSwapDemo{

public static void main(String[] args){

int [] a = new int[]{

(int)(Math.random() *1000),

(int)(Math.random() * 1000),

(int)(Math.random() *1000),

(int)(Math.random() *1000),

(int)(Math.random() * 1000)

};

System.out.println(a);

System.out.println(Arrays.toString(a));

swap(a);

System.out.println(Arrays.toString(a));

}

public static void swap(int a[]){

int len = a.length;

for(int i=0;i

int tmp = a[i];

a[i] = a[len-1-i];

a[len-1-i] = tmp;

}

}

}

12锛庨噾棰濊浆鎹紝闃挎媺浼暟瀛楃殑閲戦杞崲鎴愪腑鍥戒紶缁熺殑褰㈠紡濡傦細锛堬骏1011锛夛紞>锛堜竴鍗冮浂涓�鎷句竴鍏冩暣锛夎緭鍑恒��

鍘婚浂鐨勪唬鐮侊細

returnsb.reverse().toString().replaceAll("闆禰鎷句桨浠焆","闆�").replaceAll("闆�+涓�","涓�").replaceAll("闆�+鍏�","鍏�").replaceAll("闆�+","闆�");

public class RenMingBi {

/**

* @param args add by zxx ,Nov 29, 2008

*/

private static finalchar[] data = new char[]{

'闆�','澹�','璐�','鍙�','鑲�','浼�','闄�','鏌�','鎹�','鐜�'

};

private static finalchar[] units = new char[]{

'鍏�','鎷�','浣�','浠�','涓�','鎷�','浣�','浠�','浜�'

};

public static voidmain(String[] args) {

// TODOAuto-generated method stub

System.out.println(

convert(135689123));

}

public static Stringconvert(int money)

{

StringBuffersbf = new StringBuffer();

int unit = 0;

while(money!=0)

{

sbf.insert(0,units[unit++]);

intnumber = money%10;

sbf.insert(0,data[number]);

money/= 10;

}

returnsbf.toString();

}

}

涓�. html&JavaScript&ajax閮ㄥ垎

1. 鍒ゆ柇绗簩涓棩鏈熸瘮绗竴涓棩鏈熷ぇ

濡備綍鐢ㄨ剼鏈垽鏂敤鎴疯緭鍏ョ殑鐨勫瓧绗︿覆鏄笅闈㈢殑鏃堕棿鏍煎紡2004-11-21蹇呴』瑕佷繚璇佺敤鎴风殑杈撳叆鏄鏍煎紡锛屽苟涓旀槸鏃堕棿锛屾瘮濡傝鏈堜唤涓嶅ぇ浜�12绛夌瓑锛屽彟澶栨垜闇�瑕佺敤鎴疯緭鍏ヤ袱涓紝骞朵笖鍚庝竴涓姣斿墠涓�涓櫄锛屽彧鍏佽鐢↗AVASCRIPT锛岃璇︾粏甯姪浣滅瓟锛�,

//杩欓噷鍙敤姝e垯琛ㄨ揪寮忓垽鏂彁鍓嶅垽鏂竴涓嬫牸寮忥紝鐒跺悗鎸変笅鎻愬彇鍚勬椂闂村瓧娈靛唴瀹�

window.onload =function()

{

//杩欎箞鍐欐槸涓轰簡瀹炵幇js浠g爜涓巋tml浠g爜鐨勫垎绂伙紝褰撴垜淇敼js鏃讹紝涓嶈兘褰卞搷html浠g爜銆�

document.getElementById("frm1").onsubmit=

function(){

vard1 = this.d1.value;

vard2 = this.d2.value;

if(!verifyDate(d1)) {alert("绗竴涓棩鏈熸牸寮忎笉瀵�");return false;}

if(!verifyDate(d2)) {alert("绗簩涓棩鏈熸牸寮忎笉瀵�");return false;}

if(!compareDate(d1,d2)){alert("绗簩涓棩鏈熸瘮绗竴鏃ユ湡灏�");return false;}

};

}

functioncompareDate(d1,d2)

{

var arrayD1= d1.split("-");

var date1 =new Date(arrayD1[0],arrayD1[1],arrayD1[2]);

var arrayD2= d2.split("-");

var date2 =new Date(arrayD2[0],arrayD2[1],arrayD2[2]);

if(date1> date2) return false;

return true;

}

functionverifyDate(d)

{

vardatePattern = /^\d{4}-(0?[1-9]|1[0-2])-(0?[1-9]|[1-2]\d|3[0-1])$/;

returndatePattern.test(d);

}

2. 鐢╰able鏄剧ずn鏉¤褰曪紝姣�3琛屾崲涓�娆¢鑹诧紝鍗�1锛�2锛�3鐢ㄧ孩鑹插瓧浣擄紝4锛�5锛�6鐢ㄧ豢鑹插瓧浣擄紝7锛�8锛�9鐢ㄧ孩棰滆壊瀛椾綋銆�

1

2

3

4

5

6

7

8

9

10

window.onload=function()

{

var tbl =document.getElementById("tbl");

rows =tbl.getElementsByTagName("tr");

for(i=0;i

{

var j= parseInt(i/3);

if(j%2==0)rows[i].style.backgroundColor="#f00";

else 聽rows[i].style.backgroundColor="#0f0";

}

}

3銆丠TML鐨� form鎻愪氦涔嬪墠濡備綍楠岃瘉鏁板�兼枃鏈鐨勫唴瀹瑰叏閮ㄤ负鏁板瓧?鍚﹀垯鐨勮瘽鎻愮ず鐢ㄦ埛骞剁粓姝㈡彁浜�?

function chkForm(this)

{

var value = thist.d1.value;

var len =value.length;

for(vari=0;i

{

if(value.charAt(i)>"9"|| value.charAt(i)<"0")

{

alert("鍚湁闈炴暟瀛楀瓧绗�");

returnfalse;

}

}

return true;

}

4銆佽鍐欏嚭鐢ㄤ簬鏍¢獙HTML鏂囨湰妗嗕腑杈撳叆鐨勫唴瀹瑰叏閮ㄤ负鏁板瓧鐨刯avascript浠g爜

function chkNumber(eleText)

{

var value =eleText.value;

var len =value.length;

for(vari=0;i

{

if(value.charAt(i)>"9"|| value.charAt(i)<"0")

{

alert("鍚湁闈炴暟瀛楀瓧绗�");

eleText.focus();

break;

}

}

}

闄や簡鍐欏畬浠g爜锛岃繕搴旇鍦ㄧ綉椤典笂鍐欏嚭瀹為獙姝ラ鍜屽湪浠g爜涓姞鍏ュ疄鐜版�濊矾锛岃闈㈣瘯瀹樹竴鐪嬪氨鏄庣櫧浣犵殑鎰忓浘鍜屾鏌ヤ綘鐨勭粨鏋溿��

5銆佽璇翠綘鐢ㄨ繃閭d簺ajax鎶�鏈拰妗嗘灦锛岃璇村畠浠殑鍖哄埆

鍥�. Java web閮ㄥ垎

1銆乀omcat鐨勪紭鍖栫粡楠�

绛�:鍘绘帀瀵箇eb.xml鐨勭洃瑙嗭紝鎶妀sp鎻愬墠缂栬緫鎴怱ervlet銆�

鏈夊瘜浣欑墿鐞嗗唴瀛樼殑鎯呭喌锛屽姞澶omcat浣跨敤鐨刯vm鐨勫唴瀛�

2銆丠TTP璇锋眰鐨凣ET涓嶱OST鏂瑰紡鐨勫尯鍒�

绛�:servlet鏈夎壇濂界殑鐢熷瓨鏈熺殑瀹氫箟锛屽寘鎷姞杞藉拰瀹炰緥鍖栥�佸垵濮嬪寲銆佸鐞嗚姹備互鍙婃湇鍔$粨鏉熴�傝繖涓敓瀛樻湡鐢眏avax.servlet.Servlet鎺ュ彛鐨刬nit,service鍜宒estroy鏂规硶琛ㄨ揪銆�

3銆佽В閲婁竴涓嬩粈涔堟槸servlet;

绛�:servlet鏈夎壇濂界殑鐢熷瓨鏈熺殑瀹氫箟锛屽寘鎷姞杞藉拰瀹炰緥鍖栥�佸垵濮嬪寲銆佸鐞嗚姹備互鍙婃湇鍔$粨鏉熴�傝繖涓敓瀛樻湡鐢眏avax.servlet.Servlet鎺ュ彛鐨刬nit,service鍜宒estroy鏂规硶琛ㄨ揪銆�

4銆佽涓�璇碨ervlet鐨勭敓鍛藉懆鏈�?

绛�:servlet鏈夎壇濂界殑鐢熷瓨鏈熺殑瀹氫箟锛屽寘鎷姞杞藉拰瀹炰緥鍖栥�佸垵濮嬪寲銆佸鐞嗚姹備互鍙婃湇鍔$粨鏉熴�傝繖涓敓瀛樻湡鐢眏avax.servlet.Servlet鎺ュ彛鐨刬nit,service鍜宒estroy鏂规硶琛ㄨ揪銆�

Servlet琚湇鍔″櫒瀹炰緥鍖栧悗锛屽鍣ㄨ繍琛屽叾init鏂规硶锛岃姹傚埌杈炬椂杩愯鍏秙ervice鏂规硶锛宻ervice鏂规硶鑷姩娲鹃仯杩愯涓庤姹傚搴旂殑doXXX鏂规硶锛坉oGet锛宒oPost锛夌瓑锛屽綋鏈嶅姟鍣ㄥ喅瀹氬皢瀹炰緥閿�姣佺殑鏃跺�欒皟鐢ㄥ叾destroy鏂规硶銆�

web瀹瑰櫒鍔犺浇servlet锛岀敓鍛藉懆鏈熷紑濮嬨�傞�氳繃璋冪敤servlet鐨刬nit()鏂规硶杩涜servlet鐨勫垵濮嬪寲銆傞�氳繃璋冪敤service()鏂规硶瀹炵幇锛屾牴鎹姹傜殑涓嶅悓璋冪敤涓嶅悓鐨刣o***()鏂规硶銆傜粨鏉熸湇鍔★紝web瀹瑰櫒璋冪敤servlet鐨刣estroy()鏂规硶銆�

5銆丼ervlet鐨勫熀鏈灦鏋�

public class ServletName extends HttpServlet {

public void doPost(HttpServletRequest request,HttpServletResponse response) throws

ServletException, IOException {

}

public void doGet(HttpServletRequest request,HttpServletResponse response) throws

ServletException, IOException {

}

}

6銆丼ERVLET API涓璮orward()涓巖edirect()鐨勫尯鍒紵

绛�:鍓嶈�呬粎鏄鍣ㄤ腑鎺у埗鏉冪殑杞悜锛屽湪瀹㈡埛绔祻瑙堝櫒鍦板潃鏍忎腑涓嶄細鏄剧ず鍑鸿浆鍚戝悗鐨勫湴鍧�锛涘悗鑰呭垯鏄畬鍏ㄧ殑璺宠浆锛屾祻瑙堝櫒灏嗕細寰楀埌璺宠浆鐨勫湴鍧�锛屽苟閲嶆柊鍙戦�佽姹傞摼鎺ャ�傝繖鏍凤紝浠庢祻瑙堝櫒鐨勫湴鍧�鏍忎腑鍙互鐪嬪埌璺宠浆鍚庣殑閾炬帴鍦板潃銆傛墍浠ワ紝鍓嶈�呮洿鍔犻珮鏁堬紝鍦ㄥ墠鑰呭彲浠ユ弧瓒抽渶瑕佹椂锛屽敖閲忎娇鐢╢orward()鏂规硶锛屽苟涓旓紝杩欐牱涔熸湁鍔╀簬闅愯棌瀹為檯鐨勯摼鎺ャ�傚湪鏈変簺鎯呭喌涓嬶紝姣斿锛岄渶瑕佽烦杞埌涓�涓叾瀹冩湇鍔″櫒涓婄殑璧勬簮锛屽垯蹇呴』浣跨敤

sendRedirect()鏂规硶銆�

7銆佷粈涔堟儏鍐典笅璋冪敤doGet()鍜宒oPost()锛�

Jsp椤甸潰涓殑FORM鏍囩閲岀殑method灞炴�т负get鏃惰皟鐢╠oGet()锛屼负post鏃惰皟鐢╠oPost()銆�

8銆丷equest瀵硅薄鐨勪富瑕佹柟娉曪細

setAttribute(String name,Object)锛氳缃悕瀛椾负name鐨剅equest鐨勫弬鏁板��

getAttribute(String name)锛氳繑鍥炵敱name鎸囧畾鐨勫睘鎬у��

getAttributeNames()锛氳繑鍥瀝equest瀵硅薄鎵�鏈夊睘鎬х殑鍚嶅瓧闆嗗悎锛岀粨鏋滄槸涓�涓灇涓剧殑瀹炰緥

getCookies()锛氳繑鍥炲鎴风鐨勬墍鏈塁ookie瀵硅薄锛岀粨鏋滄槸涓�涓狢ookie鏁扮粍

getCharacterEncoding()锛氳繑鍥炶姹備腑鐨勫瓧绗︾紪鐮佹柟寮�

getContentLength()锛氳繑鍥炶姹傜殑Body鐨勯暱搴�

getHeader(String name)锛氳幏寰桯TTP鍗忚瀹氫箟鐨勬枃浠跺ご淇℃伅

getHeaders(String name)锛氳繑鍥炴寚瀹氬悕瀛楃殑request Header鐨勬墍鏈夊�硷紝缁撴灉鏄竴涓灇涓剧殑瀹炰緥

getHeaderNames()锛氳繑鍥炴墍浠equest Header鐨勫悕瀛楋紝缁撴灉鏄竴涓灇涓剧殑瀹炰緥

getInputStream()锛氳繑鍥炶姹傜殑杈撳叆娴侊紝鐢ㄤ簬鑾峰緱璇锋眰涓殑鏁版嵁

getMethod()锛氳幏寰楀鎴风鍚戞湇鍔″櫒绔紶閫佹暟鎹殑鏂规硶

getParameter(String name)锛氳幏寰楀鎴风浼犻�佺粰鏈嶅姟鍣ㄧ鐨勬湁name鎸囧畾鐨勫弬鏁板��

getParameterNames()锛氳幏寰楀鎴风浼犻�佺粰鏈嶅姟鍣ㄧ鐨勬墍鏈夊弬鏁扮殑鍚嶅瓧锛岀粨鏋滄槸涓�涓灇涓剧殑瀹炰緥

getParametervalues(String name)锛氳幏寰楁湁name鎸囧畾鐨勫弬鏁扮殑鎵�鏈夊��

getProtocol()锛氳幏鍙栧鎴风鍚戞湇鍔″櫒绔紶閫佹暟鎹墍渚濇嵁鐨勫崗璁悕绉�

getQueryString()锛氳幏寰楁煡璇㈠瓧绗︿覆

getRequestURI()锛氳幏鍙栧彂鍑鸿姹傚瓧绗︿覆鐨勫鎴风鍦板潃

getRemoteAddr()锛氳幏鍙栧鎴风鐨処P鍦板潃

getRemoteHost()锛氳幏鍙栧鎴风鐨勫悕瀛�

getSession([Boolean create])锛氳繑鍥炲拰璇锋眰鐩稿叧Session

getServerName()锛氳幏鍙栨湇鍔″櫒鐨勫悕瀛�

getServletPath()锛氳幏鍙栧鎴风鎵�璇锋眰鐨勮剼鏈枃浠剁殑璺緞

getServerPort()锛氳幏鍙栨湇鍔″櫒鐨勭鍙e彿

removeAttribute(String name)锛氬垹闄よ姹備腑鐨勪竴涓睘鎬�

9銆乫orward鍜宺edirect鐨勫尯鍒�

forward鏄湇鍔″櫒璇锋眰璧勬簮锛屾湇鍔″櫒鐩存帴璁块棶鐩爣鍦板潃鐨刄RL锛屾妸閭d釜URL鐨勫搷搴斿唴瀹硅鍙栬繃鏉ワ紝鐒跺悗鎶婅繖浜涘唴瀹瑰啀鍙戠粰娴忚鍣紝娴忚鍣ㄦ牴鏈笉鐭ラ亾鏈嶅姟鍣ㄥ彂閫佺殑鍐呭鏄粠鍝効鏉ョ殑锛屾墍浠ュ畠鐨勫湴鍧�鏍忎腑杩樻槸鍘熸潵鐨勫湴鍧�銆�

redirect灏辨槸鏈嶅姟绔牴鎹�昏緫,鍙戦�佷竴涓姸鎬佺爜,鍛婅瘔娴忚鍣ㄩ噸鏂板幓璇锋眰閭d釜鍦板潃锛屼竴鑸潵璇存祻瑙堝櫒浼氱敤鍒氭墠璇锋眰鐨勬墍鏈夊弬鏁伴噸鏂拌姹傦紝鎵�浠ession,request鍙傛暟閮藉彲浠ヨ幏鍙栥��

10銆乺equest.getAttribute()鍜� request.getParameter()鏈変綍鍖哄埆?

11. jsp鏈夊摢浜涘唴缃璞�?浣滅敤鍒嗗埆鏄粈涔�?鍒嗗埆鏈変粈涔堟柟娉曪紵

绛�:JSP鍏辨湁浠ヤ笅9涓唴缃殑瀵硅薄锛�

request 鐢ㄦ埛绔姹傦紝姝よ姹備細鍖呭惈鏉ヨ嚜GET/POST璇锋眰鐨勫弬鏁�

response 缃戦〉浼犲洖鐢ㄦ埛绔殑鍥炲簲

pageContext 缃戦〉鐨勫睘鎬ф槸鍦ㄨ繖閲岀鐞�

session 涓庤姹傛湁鍏崇殑浼氳瘽鏈�

application servlet 姝e湪鎵ц鐨勫唴瀹�

out 鐢ㄦ潵浼犻�佸洖搴旂殑杈撳嚭

configservlet鐨勬瀯鏋堕儴浠�

page JSP缃戦〉鏈韩

exception 閽堝閿欒缃戦〉锛屾湭鎹曟崏鐨勪緥澶�

request琛ㄧずHttpServletRequest瀵硅薄銆傚畠鍖呭惈浜嗘湁鍏虫祻瑙堝櫒璇锋眰鐨勪俊鎭紝骞朵笖鎻愪緵浜嗗嚑涓敤浜庤幏鍙朿ookie, header,鍜宻ession鏁版嵁鐨勬湁鐢ㄧ殑鏂规硶銆�

response琛ㄧずHttpServletResponse瀵硅薄锛屽苟鎻愪緵浜嗗嚑涓敤浜庤缃�佸洖娴忚鍣ㄧ殑鍝嶅簲鐨勬柟娉曪紙濡俢ookies,澶翠俊鎭瓑锛�

out瀵硅薄鏄痡avax.jsp.JspWriter鐨勪竴涓疄渚嬶紝骞舵彁渚涗簡鍑犱釜鏂规硶浣夸綘鑳界敤浜庡悜娴忚鍣ㄥ洖閫佽緭鍑虹粨鏋溿��

pageContext琛ㄧず涓�涓猨avax.servlet.jsp.PageContext瀵硅薄銆傚畠鏄敤浜庢柟渚垮瓨鍙栧悇绉嶈寖鍥寸殑鍚嶅瓧绌洪棿銆乻ervlet鐩稿叧鐨勫璞$殑API锛屽苟涓斿寘瑁呬簡閫氱敤鐨剆ervlet鐩稿叧鍔熻兘鐨勬柟娉曘��

session琛ㄧず涓�涓姹傜殑javax.servlet.http.HttpSession瀵硅薄銆係ession鍙互瀛樿串鐢ㄦ埛鐨勭姸鎬佷俊鎭�

applicaton琛ㄧず涓�涓猨avax.servle.ServletContext瀵硅薄銆傝繖鏈夊姪浜庢煡鎵炬湁鍏硈ervlet寮曟搸鍜宻ervlet鐜鐨勪俊鎭�

config琛ㄧず涓�涓猨avax.servlet.ServletConfig瀵硅薄銆傝瀵硅薄鐢ㄤ簬瀛樺彇servlet瀹炰緥鐨勫垵濮嬪寲鍙傛暟銆�

page琛ㄧず浠庤椤甸潰浜х敓鐨勪竴涓猻ervlet瀹炰緥

12. jsp鏈夊摢浜涘姩浣�?浣滅敤鍒嗗埆鏄粈涔�?

锛堣繖涓棶棰樹技涔庝笉閲嶈锛屼笉鏄庣櫧涓轰綍鏈夋棰橈級

绛�:JSP鍏辨湁浠ヤ笅6绉嶅熀鏈姩浣�

jsp:include锛氬湪椤甸潰琚姹傜殑鏃跺�欏紩鍏ヤ竴涓枃浠躲��

jsp:useBean锛氬鎵炬垨鑰呭疄渚嬪寲涓�涓狫avaBean銆�

jsp:setProperty锛氳缃甁avaBean鐨勫睘鎬с��

jsp:getProperty锛氳緭鍑烘煇涓狫avaBean鐨勫睘鎬с��

jsp:forward锛氭妸璇锋眰杞埌涓�涓柊鐨勯〉闈€��

jsp:plugin锛氭牴鎹祻瑙堝櫒绫诲瀷涓篔ava鎻掍欢鐢熸垚OBJECT鎴朎MBED鏍囪

13銆丣SP鐨勫父鐢ㄦ寚浠�

isErrorPage(鏄惁鑳戒娇鐢‥xception瀵硅薄)锛宨sELIgnored(鏄惁蹇界暐琛ㄨ揪寮�)

14. JSP涓姩鎬両NCLUDE涓庨潤鎬両NCLUDE鐨勫尯鍒紵

绛旓細鍔ㄦ�両NCLUDE鐢╦sp:include鍔ㄤ綔瀹炵幇

瀹冩�绘槸浼氭鏌ユ墍鍚枃浠朵腑鐨勫彉鍖栵紝閫傚悎鐢ㄤ簬鍖呭惈鍔ㄦ�侀〉闈紝骞朵笖鍙互甯﹀弬鏁� 闈欐�両NCLUDE鐢╥nclude浼爜瀹炵幇,瀹氫笉浼氭鏌ユ墍鍚枃浠剁殑鍙樺寲锛岄�傜敤浜庡寘鍚潤鎬侀〉闈� <%@include file=included.htm %>

15銆佷袱绉嶈烦杞柟寮忓垎鍒槸浠�涔�?鏈変粈涔堝尯鍒�?

锛堜笅闈㈢殑鍥炵瓟涓ラ噸閿欒锛屽簲璇ユ槸鎯抽棶forward鍜宻endRedirect鐨勫尯鍒紝姣曠珶鍑洪鐨勪汉涓嶆槸涓撲笟鎼炴枃瀛楄壓鏈殑浜猴紝鍙兘琛ㄨ揪鑳藉姏骞朵笉瑙佸緱寰堝己锛岀敤璇嶄笉涓�瀹氱簿鍑嗭紝鍔犱箣鍏惰嚜韬殑鎶�鏈潰涔熷彲鑳藉瓨鍦ㄤ竴浜涢棶棰橈紝涓嶄竴瀹氱湡姝e皢浠栫殑鎰忔�濊〃杈炬竻妤氫簡锛屼弗鏍兼剰鎬濅笂鏉ヨ锛屼竴浜涢鐩彲鑳芥牴鏈氨鏃犱汉鑳界瓟锛屾墍浠ワ紝绛旈鏃惰鎺屾彙涓诲姩锛屽彧瑕佹妸鑷繁鐭ラ亾鐨勮〃杈炬竻妤氬氨澶熶簡锛岃�屼笉瑕佸幓鎺ㄦ暡鍘熷棰樼洰鐨勫叿浣撳惈涔夋槸浠�涔堬紝涓嶈涓�鍛虫兂鐫�鏄湪绛旈锛�

绛旓細鏈変袱绉嶏紝鍒嗗埆涓猴細

鍓嶈�呴〉闈笉浼氳浆鍚慽nclude鎵�鎸囩殑椤甸潰锛屽彧鏄樉绀鸿椤电殑缁撴灉锛屼富椤甸潰杩樻槸鍘熸潵鐨勯〉闈€�傛墽琛屽畬鍚庤繕浼氬洖鏉ワ紝鐩稿綋浜庡嚱鏁拌皟鐢ㄣ�傚苟涓斿彲浠ュ甫鍙傛暟.鍚庤�呭畬鍏ㄨ浆鍚戞柊椤甸潰锛屼笉浼氬啀鍥炴潵銆傜浉褰撲簬go to 璇彞銆�

16銆侀〉闈㈤棿瀵硅薄浼犻�掔殑鏂规硶

request锛宻ession锛宎pplication锛宑ookie绛�

17銆丣SP鍜孲ervlet鏈夊摢浜涚浉鍚岀偣鍜屼笉鍚岀偣锛屼粬浠箣闂寸殑鑱旂郴鏄粈涔堬紵

JSP鏄疭ervlet鎶�鏈殑鎵╁睍锛屾湰璐ㄤ笂鏄疭ervlet鐨勭畝鏄撴柟寮忥紝鏇村己璋冨簲鐢ㄧ殑澶栬〃琛ㄨ揪銆侸SP缂栬瘧鍚庢槸"绫籹ervlet"銆係ervlet鍜孞SP鏈�涓昏鐨勪笉鍚岀偣鍦ㄤ簬锛孲ervlet鐨勫簲鐢ㄩ�昏緫鏄湪Java鏂囦欢涓紝骞朵笖瀹屽叏浠庤〃绀哄眰涓殑HTML閲屽垎绂诲紑鏉ャ�傝�孞SP鐨勬儏鍐垫槸Java鍜孒TML鍙互缁勫悎鎴愪竴涓墿灞曞悕涓�.jsp鐨勬枃浠躲�侸SP渚ч噸浜庤鍥撅紝Servlet涓昏鐢ㄤ簬鎺у埗閫昏緫銆�

18銆丮VC鐨勫悇涓儴鍒嗛兘鏈夐偅浜涙妧鏈潵瀹炵幇?濡備綍瀹炵幇?

绛�:MVC鏄疢odel锛峍iew锛岰ontroller鐨勭畝鍐欍�侻odel浠h〃鐨勬槸搴旂敤鐨勪笟鍔¢�昏緫锛堥�氳繃JavaBean锛孍JB缁勪欢瀹炵幇锛夛紝View鏄簲鐢ㄧ殑琛ㄧず闈紙鐢盝SP椤甸潰浜х敓锛夛紝Controller鏄彁渚涘簲鐢ㄧ殑澶勭悊杩囩▼鎺у埗锛堜竴鑸槸涓�涓猄ervlet锛夛紝閫氳繃杩欑璁捐妯″瀷鎶婂簲鐢ㄩ�昏緫锛屽鐞嗚繃绋嬪拰鏄剧ず閫昏緫鍒嗘垚涓嶅悓鐨勭粍浠跺疄鐜般�傝繖浜涚粍浠跺彲浠ヨ繘琛屼氦浜掑拰閲嶇敤銆�

19銆佹垜浠湪web搴旂敤寮�鍙戣繃绋嬩腑缁忓父閬囧埌杈撳嚭鏌愮缂栫爜鐨勫瓧绗︼紝濡俰so8859-1绛夛紝濡備綍杈撳嚭涓�涓煇绉嶇紪鐮佺殑瀛楃涓诧紵

Public String translate(String str) {

String tempStr ="";

try {

tempStr = newString(str.getBytes("ISO-8859-1"), "GBK");

tempStr =tempStr.trim();

}

catch (Exception e) {

System.err.println(e.getMessage());

}

return tempStr;

}

20锛�鐜板湪杈撳叆n涓暟瀛楋紝浠ラ�楀彿锛屽垎寮�锛涚劧鍚庡彲閫夋嫨鍗囨垨鑰呴檷搴忔帓搴忥紱鎸夋彁浜ら敭灏卞湪鍙︿竴椤甸潰鏄剧ず鎸変粈涔堟帓搴忥紝缁撴灉涓猴紝鎻愪緵reset

浜�.鏁版嵁搴撻儴鍒�

1銆佺敤涓ょ鏂瑰紡鏍规嵁閮ㄩ棬鍙蜂粠楂樺埌浣庯紝宸ヨ祫浠庝綆鍒伴珮鍒楀嚭姣忎釜鍛樺伐鐨勪俊鎭��

employee:

eid,ename,salary,deptid;

select * from employeeorder by deptid desc,salary

2銆佸垪鍑哄悇涓儴闂ㄤ腑宸ヨ祫楂樹簬鏈儴闂ㄧ殑骞冲潎宸ヨ祫鐨勫憳宸ユ暟鍜岄儴闂ㄥ彿锛屽苟鎸夐儴闂ㄥ彿鎺掑簭

鍒涘缓琛細

mysql> createtable employee921(id int primary key auto_increment,name varchar(5

0),salary bigint,deptid int);

鎻掑叆瀹為獙鏁版嵁锛�

mysql> insert into employee921values(null,'zs',1000,1),(null,'ls',1100,1),(null

,'ww',1100,1),(null,'zl',900,1) ,(null,'zl',1000,2), (null,'zl',900,2),(null,'z

l',1000,2) , (null,'zl',1100,2);

缂栧啓sql璇彞锛�

锛堬級select avg(salary) from employee921 group by deptid;

锛堬級mysql> selectemployee921.id,employee921.name,employee921.salary,employee921.dep

tid tid fromemployee921where salary > (select avg(salary) from employee921 where 聽deptid = tid);

鏁堢巼浣庣殑涓�涓鍙ワ紝浠呬緵瀛︿範鍙傝�冧娇鐢紙鍦╣roup by涔嬪悗涓嶈兘浣跨敤where锛屽彧鑳戒娇鐢╤aving锛屽湪group by涔嬪墠鍙互浣跨敤where锛屽嵆琛ㄧず瀵硅繃婊ゅ悗鐨勭粨鏋滃垎缁勶級锛�

mysql> selectemployee921.id,employee921.name,employee921.salary,employee921.dep

tid tid fromemployee921where salary > (select avg(salary) from employee921 group by deptid havingdeptid = tid);

锛堬級select count(*) ,tid

from (

selectemployee921.id,employee921.name,employee921.salary,employee921.deptid tid

from 聽 聽 聽employee921

where salary>

(selectavg(salary) from employee921 where deptid = tid)

) as t

group by tid ;

鍙﹀涓�绉嶆柟寮忥細鍏宠仈鏌ヨ

select a.ename,a.salary,a.deptid

from emp a,

(selectdeptd,avg(salary) avgsal from emp group by deptid ) b

where a.deptid=b.deptidand a.salary>b.avgsal;

3銆佸瓨鍌ㄨ繃绋嬩笌瑙﹀彂鍣ㄥ繀椤昏锛岀粡甯歌闈㈣瘯鍒�?

create procedure insert_Student (_name varchar(50),_age int ,out_id int)

begin

insert into studentvalue(null,_name,_age);

select max(stuId)into _id from student;

end;

call insert_Student('wfz',23,@id);

select @id;

mysql> create trigger update_Student BEFORE update on studentFOR EACH ROW

-> select * from student;

瑙﹀彂鍣ㄤ笉鍏佽杩斿洖缁撴灉

create trigger update_StudentBEFORE update on student FOR EACH ROW

insert intostudent value(null,'zxx',28);

mysql鐨勮Е鍙戝櫒鐩墠涓嶈兘瀵瑰綋鍓嶈〃杩涜鎿嶄綔

create trigger update_StudentBEFORE update on student FOR EACH ROW

delete from articleswhere id=8;

杩欎釜渚嬪瓙涓嶆槸寰堝ソ锛屾渶濂芥槸鐢ㄥ垹闄や竴涓敤鎴锋椂锛岄『甯﹀垹闄よ鐢ㄦ埛鐨勬墍鏈夊笘瀛�

杩欓噷瑕佹敞鎰忎娇鐢∣LD.id

瑙﹀彂鍣ㄧ敤澶勮繕鏄緢澶氱殑锛屾瘮濡傛牎鍐呯綉銆佸紑蹇冪綉銆丗acebook锛屼綘鍙戜竴涓棩蹇楋紝鑷姩閫氱煡濂藉弸锛屽叾瀹炲氨鏄湪澧炲姞鏃ュ織鏃跺仛涓�涓悗瑙﹀彂锛屽啀鍚戦�氱煡琛ㄤ腑鍐欏叆鏉$洰銆傚洜涓鸿Е鍙戝櫒鏁堢巼楂樸�傝�孶CH娌℃湁鐢ㄨЕ鍙戝櫒锛屾晥鐜囧拰鏁版嵁澶勭悊鑳藉姏閮藉緢浣庛��

瀛樺偍杩囩▼鐨勫疄楠屾楠わ細

mysql> delimiter |

mysql> create procedure insertArticle_Procedure (pTitle varchar(50),pBid int,out

pId int)

-> begin

-> insert into article1value(null,pTitle,pBid);

-> select max(id) into pId fromarticle1;

-> end;

-> |

Query OK, 0 rows affected (0.05sec)

mysql> callinsertArticle_Procedure('浼犳櫤鎾',1,@pid);

-> |

Query OK, 0 rows affected (0.00sec)

mysql> delimiter ;

mysql> select @pid;

+------+

| @pid |

+------+

| 3|

+------+

1 row in set (0.00 sec)

mysql> select * fromarticle1;

+----+--------------+------+

| id | title| bid |

+----+--------------+------+

| 1| test 聽 聽 聽 聽| 1 聽 聽|

| 2| chuanzhiboke | 1 聽 聽|

| 3|浼犳櫤鎾| 1 聽 聽|

+----+--------------+------+

3 rows in set (0.00 sec)

瑙﹀彂鍣ㄧ殑瀹為獙姝ラ锛�

create table board1(id intprimary key auto_increment,name varchar(50),ar

ticleCount int);

create table article1(id intprimary key auto_increment,title varchar(50)

,bid int referencesboard1(id));

delimiter |

create triggerinsertArticle_Trigger after insert on article1 for each ro

w begin

-> update board1 setarticleCount=articleCount+1 where id= NEW.bid;

-> end;

-> |

delimiter ;

insert into board1 value(null,'test',0);

insert into article1value(null,'test',1);

杩樻湁锛屾瘡鎻掑叆涓�涓笘瀛愶紝閮藉笇鏈涘皢鐗堥潰琛ㄤ腑鐨勬渶鍚庡彂甯栨椂闂达紝甯栧瓙鎬绘暟瀛楁杩涜鍚屾鏇存柊锛岀敤瑙﹀彂鍣ㄥ仛鏁堢巼灏卞緢楂樸�備笅娆¤璁捐杩欐牱涓�涓渚嬶紝鍐欒Е鍙戝櫒鏃讹紝瀵逛簬鏈�鍚庡彂甯栨椂闂村彲鑳介渶瑕佺敤declare鏂瑰紡澹版槑涓�涓彉閲忥紝鎴栬�呮槸鐢∟EW.posttime鏉ョ敓鎴愩��

4銆佹暟鎹簱涓夎寖寮忔槸浠�涔�?

绗竴鑼冨紡锛�1NF锛夛細瀛楁鍏锋湁鍘熷瓙鎬�,涓嶅彲鍐嶅垎銆傛墍鏈夊叧绯诲瀷鏁版嵁搴撶郴缁熼兘婊¤冻绗竴鑼冨紡锛�

鏁版嵁搴撹〃涓殑瀛楁閮芥槸鍗曚竴灞炴�х殑锛屼笉鍙啀鍒嗐�備緥濡傦紝濮撳悕瀛楁锛屽叾涓殑濮撳拰鍚嶅繀椤讳綔涓轰竴涓暣浣擄紝鏃犳硶鍖哄垎鍝儴鍒嗘槸濮擄紝鍝儴鍒嗘槸鍚嶏紝濡傛灉瑕佸尯鍒嗗嚭濮撳拰鍚嶏紝蹇呴』璁捐鎴愪袱涓嫭绔嬬殑瀛楁銆�

绗簩鑼冨紡锛�2NF锛夛細

绗簩鑼冨紡锛�2NF锛夋槸鍦ㄧ涓�鑼冨紡锛�1NF锛夌殑鍩虹涓婂缓绔嬭捣鏉ョ殑锛屽嵆婊¤冻绗簩鑼冨紡锛�2NF锛夊繀椤诲厛婊¤冻绗竴鑼冨紡锛�1NF锛夈��

瑕佹眰鏁版嵁搴撹〃涓殑姣忎釜瀹炰緥鎴栬蹇呴』鍙互琚儫涓�鍦板尯鍒嗐�傞�氬父闇�瑕佷负琛ㄥ姞涓婁竴涓垪锛屼互瀛樺偍鍚勪釜瀹炰緥鐨勬儫涓�鏍囪瘑銆傝繖涓儫涓�灞炴�у垪琚О涓轰富鍏抽敭瀛楁垨涓婚敭銆�

绗簩鑼冨紡锛�2NF锛夎姹傚疄浣撶殑灞炴�у畬鍏ㄤ緷璧栦簬涓诲叧閿瓧銆傛墍璋撳畬鍏ㄤ緷璧栨槸鎸囦笉鑳藉瓨鍦ㄤ粎渚濊禆涓诲叧閿瓧涓�閮ㄥ垎鐨勫睘鎬э紝濡傛灉瀛樺湪锛岄偅涔堣繖涓睘鎬у拰涓诲叧閿瓧鐨勮繖涓�閮ㄥ垎搴旇鍒嗙鍑烘潵褰㈡垚涓�涓柊鐨勫疄浣擄紝鏂板疄浣撲笌鍘熷疄浣撲箣闂存槸涓�瀵瑰鐨勫叧绯汇�備负瀹炵幇鍖哄垎閫氬父闇�瑕佷负琛ㄥ姞涓婁竴涓垪锛屼互瀛樺偍鍚勪釜瀹炰緥鐨勬儫涓�鏍囪瘑銆傜畝鑰岃█涔嬶紝绗簩鑼冨紡灏辨槸闈炰富灞炴�ч潪閮ㄥ垎渚濊禆浜庝富鍏抽敭瀛椼��

绗笁鑼冨紡鐨勮姹傚涓嬶細

婊¤冻绗笁鑼冨紡锛�3NF锛夊繀椤诲厛婊¤冻绗簩鑼冨紡锛�2NF锛夈�傜畝鑰岃█涔嬶紝绗笁鑼冨紡锛�3NF锛夎姹備竴涓暟鎹簱琛ㄤ腑涓嶅寘鍚凡鍦ㄥ叾瀹冭〃涓凡鍖呭惈鐨勯潪涓诲叧閿瓧淇℃伅銆�

鎵�浠ョ涓夎寖寮忓叿鏈夊涓嬬壒寰侊細

1锛屾瘡涓�鍒楀彧鏈変竴涓��

2锛屾瘡涓�琛岄兘鑳藉尯鍒嗐��

3锛屾瘡涓�涓〃閮戒笉鍖呭惈鍏朵粬琛ㄥ凡缁忓寘鍚殑闈炰富鍏抽敭瀛椾俊鎭��

渚嬪锛屽笘瀛愯〃涓彧鑳藉嚭鐜板彂甯栦汉鐨刬d锛岃�屼笉鑳藉嚭鐜板彂甯栦汉鐨刬d锛岃繕鍚屾椂鍑虹幇鍙戝笘浜哄鍚嶏紝鍚﹀垯锛屽彧瑕佸嚭鐜板悓涓�鍙戝笘浜篿d鐨勬墍鏈夎褰曪紝瀹冧滑涓殑濮撳悕閮ㄥ垎閮藉繀椤讳弗鏍间繚鎸佷竴鑷达紝杩欏氨鏄暟鎹啑浣欍��

5銆佽鍑轰竴浜涙暟鎹簱浼樺寲鏂归潰鐨勭粡楠�?

鐢≒reparedStatement涓�鑸潵璇存瘮Statement鎬ц兘楂橈細涓�涓猻ql鍙戠粰鏈嶅姟鍣ㄥ幓鎵ц锛屾秹鍙婃楠わ細璇硶妫�鏌ャ�佽涔夊垎鏋愶紝缂栬瘧锛岀紦瀛�

鈥渋nert into user values(1,1,1)鈥�-脿浜岃繘鍒�

鈥渋nert into user values(2,2,2)鈥�-脿浜岃繘鍒�

鈥渋nert into user values(?,?,?)鈥�-脿浜岃繘鍒�

鏈夊閿害鏉熶細褰卞搷鎻掑叆鍜屽垹闄ゆ�ц兘锛屽鏋滅▼搴忚兘澶熶繚璇佹暟鎹殑瀹屾暣鎬э紝閭e湪璁捐鏁版嵁搴撴椂灏卞幓鎺夊閿�傦紙姣斿柣锛氬氨濂芥瘮鍏嶆浜у搧锛屽氨鏄负浜嗘彁楂樻晥鐜囷紝鍏呭垎鐩镐俊浜у搧鐨勫埗閫犲晢锛�

锛堝浜巋ibernate鏉ヨ锛屽氨搴旇鏈変竴涓彉鍖栵細empleyee->Deptment瀵硅薄锛岀幇鍦ㄨ璁℃椂灏辨垚浜唀mployee脿deptid锛�

鐪媘ysql甯姪鏂囨。瀛愭煡璇㈢珷鑺傜殑鏈�鍚庨儴鍒嗭紝渚嬪锛屾牴鎹壂鎻忕殑鍘熺悊锛屼笅闈㈢殑瀛愭煡璇㈣鍙ヨ姣旂浜屾潯鍏宠仈鏌ヨ鐨勬晥鐜囬珮锛�

1.select e.name,e.salarywhere e.managerid=(select id from employee where name='zxx');

2.select e.name,e.salary,m.name,m.salary fromemployees e,employees m where

e.managerid = m.id andm.name='zxx';

琛ㄤ腑鍏佽閫傚綋鍐椾綑锛岃濡傦紝涓婚甯栫殑鍥炲鏁伴噺鍜屾渶鍚庡洖澶嶆椂闂寸瓑

灏嗗鍚嶅拰瀵嗙爜鍗曠嫭浠庣敤鎴疯〃涓嫭绔嬪嚭鏉ャ�傝繖鍙互鏄潪甯稿ソ鐨勪竴瀵逛竴鐨勬渚嬪摕锛�

sql璇彞鍏ㄩ儴澶у啓锛岀壒鍒槸鍒楀悕鍜岃〃鍚嶉兘澶у啓銆傜壒鍒槸sql鍛戒护鐨勭紦瀛樺姛鑳斤紝鏇村姞闇�瑕佺粺涓�澶у皬鍐欙紝sql璇彞脿鍙戠粰oracle鏈嶅姟鍣犺娉曟鏌ュ拰缂栬瘧鎴愪负鍐呴儴鎸囦护脿缂撳瓨鍜屾墽琛屾寚浠ゃ�傛牴鎹紦瀛樼殑鐗圭偣锛屼笉瑕佹嫾鍑戞潯浠讹紝鑰屾槸鐢�?鍜孭reparedStatment

杩樻湁绱㈠紩瀵规煡璇㈡�ц兘鐨勬敼杩涗篃鏄�煎緱鍏虫敞鐨勩��

澶囨敞锛氫笅闈㈡槸鍏充簬鎬ц兘鐨勮璁轰妇渚�

4鑸彮 3涓煄甯�

m*n

select * from flight,city where flight.startcityid=city.cityidand city.name='beijing';

m + n

select * from flight where startcityid = (select cityid fromcity where cityname='beijing');

select flight.id,'beijing',flight.flightTime from flight wherestartcityid = (select cityid from city where cityname='beijing')

6銆乽nion鍜寀nion all鏈変粈涔堜笉鍚�?

鍋囪鎴戜滑鏈変竴涓〃Student锛屽寘鎷互涓嬪瓧娈典笌鏁版嵁锛�

drop table student;

create table student

(

id int primary key,

name nvarchar2(50) not null,

score number not null

);

insert into student values(1,'Aaron',78);

insert into student values(2,'Bill',76);

insert into student values(3,'Cindy',89);

insert into student values(4,'Damon',90);

insert into student values(5,'Ella',73);

insert into student values(6,'Frado',61);

insert into student values(7,'Gill',99);

insert into student values(8,'Hellen',56);

insert into student values(9,'Ivan',93);

insert into student values(10,'Jay',90);

commit;

Union鍜孶nion All鐨勫尯鍒��

select *

from student

where id < 4

union

select *

from student

where id > 2 and id < 6

缁撴灉灏嗘槸

1 聽 聽Aaron 聽 聽78

2 聽 聽Bill 聽 聽76

3 聽 聽Cindy 聽 聽89

4 聽 聽Damon 聽 聽90

5 聽 聽Ella 聽 聽73

濡傛灉鎹㈡垚Union All杩炴帴涓や釜缁撴灉闆嗭紝鍒欒繑鍥炵粨鏋滄槸锛�

1 聽 聽Aaron 聽 聽78

2 聽 聽Bill 聽 聽76

3 聽 聽Cindy 聽 聽89

3 聽 聽Cindy 聽 聽89

4 聽 聽Damon 聽 聽90

5 聽 聽Ella 聽 聽73

鍙互鐪嬪埌锛孶nion鍜孶nion All鐨勫尯鍒箣涓�鍦ㄤ簬瀵归噸澶嶇粨鏋滅殑澶勭悊銆�

UNION鍦ㄨ繘琛岃〃閾炬帴鍚庝細绛涢�夋帀閲嶅鐨勮褰曪紝鎵�浠ュ湪琛ㄩ摼鎺ュ悗浼氬鎵�浜х敓鐨勭粨鏋滈泦杩涜鎺掑簭杩愮畻锛屽垹闄ら噸澶嶇殑璁板綍鍐嶈繑鍥炵粨鏋溿�傚疄闄呭ぇ閮ㄥ垎搴旂敤涓槸涓嶄細浜х敓閲嶅鐨勮褰曪紝鏈�甯歌鐨勬槸杩囩▼琛ㄤ笌鍘嗗彶琛║NION銆傚锛�

select * from gc_dfys

union

select * from ls_jg_dfys

杩欎釜SQL鍦ㄨ繍琛屾椂鍏堝彇鍑轰袱涓〃鐨勭粨鏋滐紝鍐嶇敤鎺掑簭绌洪棿杩涜鎺掑簭鍒犻櫎閲嶅鐨勮褰曪紝鏈�鍚庤繑鍥炵粨鏋滈泦锛屽鏋滆〃鏁版嵁閲忓ぇ鐨勮瘽鍙兘浼氬鑷寸敤纾佺洏杩涜鎺掑簭銆�

鑰孶NION ALL鍙槸绠�鍗曠殑灏嗕袱涓粨鏋滃悎骞跺悗灏辫繑鍥炪�傝繖鏍凤紝濡傛灉杩斿洖鐨勪袱涓粨鏋滈泦涓湁閲嶅鐨勬暟鎹紝閭d箞杩斿洖鐨勭粨鏋滈泦灏变細鍖呭惈閲嶅鐨勬暟鎹簡銆�

浠庢晥鐜囦笂璇达紝UNION ALL瑕佹瘮UNION蹇緢澶氾紝鎵�浠ワ紝濡傛灉鍙互纭鍚堝苟鐨勪袱涓粨鏋滈泦涓笉鍖呭惈閲嶅鐨勬暟鎹殑璇濓紝閭d箞灏变娇鐢║NION ALL锛�

7.鍒嗛〉璇彞

鍙栧嚭sql琛ㄤ腑绗�31鍒�40鐨勮褰曪紙浠ヨ嚜鍔ㄥ闀縄D涓轰富閿級

sql server鏂规1锛�

selecttop 10 * from t where id not in (select top 30 id from t order by id ) orde byid

sql server鏂规2锛�

selecttop 10 * from t where id in (select top 40 id from t order by id) order by iddesc

mysql鏂规锛歴elect * from t order by idlimit 30,10

oracle鏂规锛歴elect * from (select rownum r,* from t where r<=40) wherer>30

--------------------寰呮暣鐞嗚繘鍘荤殑鍐呭-------------------------------------

pageSize=20;

pageNo = 5;

1.鍒嗛〉鎶�鏈�1锛堢洿鎺ュ埄鐢╯ql璇彞杩涜鍒嗛〉锛屾晥鐜囨渶楂樺拰鏈�鎺ㄨ崘鐨勶級

mysql:sql = "select * from articles limit " +(pageNo-1)*pageSize + "," + pageSize;

oracle: sql = "select * from " +

"(selectrownum r,* from " +

"(select* from articles order by postime desc)" +

"whererownum<= " + pageNo*pageSize +") tmp " +

"wherer>" + (pageNo-1)*pageSize;

娉ㄩ噴锛氱7琛屼繚璇乺ownum鐨勯『搴忔槸纭畾鐨勶紝鍥犱负oracle鐨勭储寮曚細閫犳垚rownum杩斿洖涓嶅悓鐨勫��

绠�娲嬫彁绀猴細娌℃湁order by鏃讹紝rownum鎸夐『搴忚緭鍑猴紝涓�鏃︽湁浜唎rder by锛宺ownum涓嶆寜椤哄簭杈撳嚭浜嗭紝杩欒鏄巖ownum鏄帓搴忓墠鐨勭紪鍙枫�傚鏋滃order by浠庡彞涓殑瀛楁寤虹珛浜嗙储寮曪紝閭d箞锛宺ownum涔熸槸鎸夐『搴忚緭鍑虹殑锛屽洜涓鸿繖鏃跺�欑敓鎴愬師濮嬬殑鏌ヨ缁撴灉闆嗘椂浼氬弬鐓х储寮曡〃鐨勯『搴忔潵鏋勫缓銆�

sqlserver:sql = "select top 10 * from id not id(select top" + (pageNo-1)*pageSize + "id from articles)"

DataSource ds = new InitialContext().lookup(jndiurl);

Connection cn = ds.getConnection();

//"select * from user where id=?"--->binary directive

PreparedStatement pstmt = cn.prepareSatement(sql);

ResultSet rs = pstmt.executeQuery()

while(rs.next())

{

out.println(rs.getString(1));

}

2.涓嶅彲婊氬姩鐨勬父鏍�

pageSize=20;

pageNo = 5;

cn = null

stmt = null;

rs = null;

try

{

sqlserver:sql = "select* from articles";

DataSource ds = new InitialContext().lookup(jndiurl);

Connection cn = ds.getConnection();

//"select * from user where id=?"--->binary directive

PreparedStatement pstmt = cn.prepareSatement(sql);

ResultSet rs = pstmt.executeQuery()

for(int j=0;j<(pageNo-1)*pageSize;j++)

{

rs.next();

}

int i=0;

while(rs.next() && i<10)

{

i++;

out.println(rs.getString(1));

}

}

cacth(){}

finnaly

{

if(rs!=null)try{rs.close();}catch(Exceptione){}

if(stm.........

if(cn............

}

3.鍙粴鍔ㄧ殑娓告爣

pageSize=20;

pageNo = 5;

cn = null

stmt = null;

rs = null;

try

{

sqlserver:sql = "select* from articles";

DataSource ds = new InitialContext().lookup(jndiurl);

Connection cn = ds.getConnection();

//"select * from user where id=?"--->binary directive

PreparedStatement pstmt = cn.prepareSatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,...);

//鏍规嵁涓婇潰杩欒浠g爜鐨勫紓甯窼QLFeatureNotSupportedException锛屽氨鍙垽鏂┍鍔ㄦ槸鍚︽敮鎸佸彲婊氬姩娓告爣

ResultSet rs = pstmt.executeQuery()

rs.absolute((pageNo-1)*pageSize)

int i=0;

while(rs.next() && i<10)

{

i++;

out.println(rs.getString(1));

}

}

cacth(){}

finnaly

{

if(rs!=null)try{rs.close();}catch(Exceptione){}

if(stm.........

if(cn............

}

8.鐢ㄤ竴鏉QL璇彞鏌ヨ鍑烘瘡闂ㄨ閮藉ぇ浜�80鍒嗙殑瀛︾敓濮撳悕

name 聽 kecheng 聽 fenshu

寮犱笁 聽 聽璇枃 聽 聽 聽81

寮犱笁 聽 聽鏁板 聽 聽 聽75

鏉庡洓 聽 聽璇枃 聽 聽 聽76

鏉庡洓 聽 聽鏁板 聽 聽 聽90

鐜嬩簲 聽 聽璇枃 聽 聽 聽81

鐜嬩簲 聽 聽鏁板 聽 聽 聽100

鐜嬩簲 聽 聽鑻辫 聽 聽 聽90

鍑嗗鏁版嵁鐨剆ql浠g爜锛�

create table score(id int primary key auto_increment,namevarchar(20),subject varchar(20),score int);

insert into score values

(null,'寮犱笁','璇枃',81),

(null,'寮犱笁','鏁板',75),

(null,'鏉庡洓','璇枃',76),

(null,'鏉庡洓','鏁板',90),

(null,'鐜嬩簲','璇枃',81),

(null,'鐜嬩簲','鏁板',100),

(null,'鐜嬩簲 ','鑻辫',90);

鎻愮ず锛氬綋鐧炬�濅笉寰楀叾瑙f椂锛岃鐞嗘兂鎬濈淮锛屾妸灏忓彉鎴愬ぇ鍋氾紝鎶婂ぇ鍙樻垚灏忓仛锛�

绛旀锛�

A: select distinct name from score 聽where 聽name not in (selectdistinct name from score where score<=80)

B:select distince name t1 from score where 80< all (selectscore from score where name=t1);

9.鎵�鏈夐儴闂ㄤ箣闂寸殑姣旇禌缁勫悎

涓�涓彨department鐨勮〃锛岄噷闈㈠彧鏈変竴涓瓧娈祅ame,涓�鍏辨湁4鏉$邯褰曪紝鍒嗗埆鏄痑,b,c,d,瀵瑰簲鍥涗釜鐞冨锛岀幇鍦ㄥ洓涓悆瀵硅繘琛屾瘮璧涳紝鐢ㄤ竴鏉ql璇彞鏄剧ず鎵�鏈夊彲鑳界殑姣旇禌缁勫悎.

绛旓細select a.name,b.name

from team a, team b

where a.name < b.name

10.姣忎釜鏈堜唤鐨勫彂鐢熼閮芥瘮101绉戠洰澶氱殑绉戠洰

璇风敤SQL璇彞瀹炵幇锛氫粠TestDB鏁版嵁琛ㄤ腑鏌ヨ鍑烘墍鏈夋湀浠界殑鍙戠敓棰濋兘姣�101绉戠洰鐩稿簲鏈堜唤鐨勫彂鐢熼楂樼殑绉戠洰銆傝娉ㄦ剰锛歍estDB涓湁寰堝绉戠洰锛岄兘鏈�1锛�12鏈堜唤鐨勫彂鐢熼銆�

AccID锛氱鐩唬鐮侊紝Occmonth锛氬彂鐢熼鏈堜唤锛孌ebitOccur锛氬彂鐢熼銆�

鏁版嵁搴撳悕锛欽cyAudit锛屾暟鎹泦锛歋elect * from TestDB

鍑嗗鏁版嵁鐨剆ql浠g爜锛�

drop table if exists TestDB;

create table TestDB(id int primary key auto_increment,AccIDvarchar(20), Occmonth date, DebitOccur bigint);

insert into TestDB values

(null,'101','1988-1-1',100),

(null,'101','1988-2-1',110),

(null,'101','1988-3-1',120),

(null,'101','1988-4-1',100),

(null,'101','1988-5-1',100),

(null,'101','1988-6-1',100),

(null,'101','1988-7-1',100),

(null,'101','1988-8-1',100);

--澶嶅埗涓婇潰鐨勬暟鎹紝鏁呮剰鎶婄涓�涓湀浠界殑鍙戠敓棰濇暟瀛楁敼灏忎竴鐐�

insert into TestDB values

(null,'102','1988-1-1',90),

(null,'102','1988-2-1',110),

(null,'102','1988-3-1',120),

(null,'102','1988-4-1',100),

(null,'102','1988-5-1',100),

(null,'102','1988-6-1',100),

(null,'102','1988-7-1',100),

(null,'102','1988-8-1',100);

--澶嶅埗鏈�涓婇潰鐨勬暟鎹紝鏁呮剰鎶婃墍鏈夊彂鐢熼鏁板瓧鏀瑰ぇ涓�鐐�

insert into TestDB values

(null,'103','1988-1-1',150),

(null,'103','1988-2-1',160),

(null,'103','1988-3-1',180),

(null,'103','1988-4-1',120),

(null,'103','1988-5-1',120),

(null,'103','1988-6-1',120),

(null,'103','1988-7-1',120),

(null,'103','1988-8-1',120);

--澶嶅埗鏈�涓婇潰鐨勬暟鎹紝鏁呮剰鎶婃墍鏈夊彂鐢熼鏁板瓧鏀瑰ぇ涓�鐐�

insert into TestDB values

(null,'104','1988-1-1',130),

(null,'104','1988-2-1',130),

(null,'104','1988-3-1',140),

(null,'104','1988-4-1',150),

(null,'104','1988-5-1',160),

(null,'104','1988-6-1',170),

(null,'104','1988-7-1',180),

(null,'104','1988-8-1',140);

--澶嶅埗鏈�涓婇潰鐨勬暟鎹紝鏁呮剰鎶婄浜屼釜鏈堜唤鐨勫彂鐢熼鏁板瓧鏀瑰皬涓�鐐�

insert into TestDB values

(null,'105','1988-1-1',100),

(null,'105','1988-2-1',80),

(null,'105','1988-3-1',120),

(null,'105','1988-4-1',100),

(null,'105','1988-5-1',100),

(null,'105','1988-6-1',100),

(null,'105','1988-7-1',100),

(null,'105','1988-8-1',100);

绛旀锛�

select distinct AccID from TestDB

where AccID not in

(selectTestDB.AccIDfrom TestDB,

(select * from TestDB where AccID='101') asdb101

whereTestDB.Occmonth=db101.Occmonth and TestDB.DebitOccur<=db101.DebitOccur

);

11.缁熻姣忓勾姣忔湀鐨勪俊鎭�

year 聽monthamount

1991 聽 1 聽 聽 1.1

1991 聽 2 聽 聽 1.2

1991 聽 3 聽 聽 1.3

1991 聽 4 聽 聽 1.4

1992 聽 1 聽 聽 2.1

1992 聽 2 聽 聽 2.2

1992 聽 3 聽 聽 2.3

1992 聽 4 聽 聽 2.4

鏌ユ垚杩欐牱涓�涓粨鏋�

year m1 聽m2 聽m3 聽m4

1991 1.1 1.2 1.3 1.4

1992 2.1 2.2 2.3 2.4

鎻愮ず锛氳繖涓笌宸ヨ祫鏉¢潪甯哥被浼硷紝涓庡鐢熺殑绉戠洰鎴愮哗涔熷緢鐩镐技銆�

鍑嗗sql璇彞锛�

drop table if existssales;

create table sales(idint auto_increment primary key,year varchar(10), month varchar(10), amountfloat(2,1));

insert into salesvalues

(null,'1991','1',1.1),

(null,'1991','2',1.2),

(null,'1991','3',1.3),

(null,'1991','4',1.4),

(null,'1992','1',2.1),

(null,'1992','2',2.2),

(null,'1992','3',2.3),

(null,'1992','4',2.4);

绛旀涓�銆�

select sales.year ,

(select t.amount fromsales t where t.month='1' and t.year= sales.year) '1',

(select t.amount fromsales t where t.month='1' and t.year= sales.year) '2',

(select t.amount fromsales t where t.month='1' and t.year= sales.year) '3',

(select t.amount fromsales t where t.month='1' and t.year= sales.year) as '4'

from salesgroup by year;

12.鏄剧ず鏂囩珷鏍囬锛屽彂甯栦汉銆佹渶鍚庡洖澶嶆椂闂�

琛細id,title,postuser,postdate,parentid

鍑嗗sql璇彞锛�

drop table if exists articles;

create table articles(id int auto_increment primary key,titlevarchar(50), postuser varchar(10), postdate datetime,parentid int referencesarticles(id));

insert into articles values

(null,'绗竴鏉�','寮犱笁','1998-10-10 12:32:32',null),

(null,'绗簩鏉�','寮犱笁','1998-10-10 12:34:32',null),

(null,'绗竴鏉″洖澶�1','鏉庡洓','1998-10-10 12:35:32',1),

(null,'绗簩鏉″洖澶�1','鏉庡洓','1998-10-10 12:36:32',2),

(null,'绗竴鏉″洖澶�2','鐜嬩簲','1998-10-10 12:37:32',1),

(null,'绗竴鏉″洖澶�3','鏉庡洓','1998-10-10 12:38:32',1),

(null,'绗簩鏉″洖澶�2','鏉庡洓','1998-10-10 12:39:32',2),

(null,'绗竴鏉″洖澶�4','鐜嬩簲','1998-10-10 12:39:40',1);

绛旀锛�

select a.title,a.postuser,

(selectmax(postdate) from articles where parentid=a.id) reply

from articles a where a.parentid is null;

娉ㄩ噴锛氬瓙鏌ヨ鍙互鐢ㄥ湪閫夋嫨鍒椾腑锛屼篃鍙敤浜巜here鐨勬瘮杈冩潯浠朵腑锛岃繕鍙互鐢ㄤ簬from浠庡彞涓��

13.鍒犻櫎闄や簡id鍙蜂笉鍚�,鍏朵粬閮界浉鍚岀殑瀛︾敓鍐椾綑淇℃伅

2.瀛︾敓琛ㄥ涓�:

id鍙� 聽 瀛﹀彿 聽 濮撳悕璇剧▼缂栧彿璇剧▼鍚嶇О鍒嗘暟

1 聽 聽 聽 聽2005001 聽寮犱笁 0001 聽 聽 鏁板 聽 聽69

2 聽 聽 聽 聽2005002 聽鏉庡洓 0001 聽 聽 鏁板 聽 聽89

3 聽 聽 聽 聽2005001 聽寮犱笁 0001 聽 聽 鏁板 聽 聽69

A: delete from tablename where id鍙� not in(select min(id鍙�) from tablename group by瀛﹀彿,濮撳悕,璇剧▼缂栧彿,璇剧▼鍚嶇О,鍒嗘暟)

瀹為獙锛�

create table student2(id int auto_increment primary key,codevarchar(20),name varchar(20));

insert into student2 values(null,'2005001','寮犱笁'),(null,'2005002','鏉庡洓'),(null,'2005001','寮犱笁');

//濡備笅璇彞锛宮ysql鎶ュ憡閿欒锛屽彲鑳藉垹闄や緷璧栧悗闈㈢粺璁¤鍙ワ紝鑰屽垹闄ゅ張瀵艰嚧缁熻璇彞缁撴灉涓嶄竴鑷淬��

delete from student2 where id not in(select min(id) fromstudent2 group by name);

//浣嗘槸锛屽涓嬭鍙ユ病鏈夐棶棰橈細

select *from student2where id not in(select min(id) from student2 group by name);

//浜庢槸锛屾垜鎯冲厛鎶婂垎缁勭殑缁撴灉鍋氭垚铏氳〃锛岀劧鍚庝粠铏氳〃涓�夊嚭缁撴灉锛屾渶鍚庡啀灏嗙粨鏋滀綔涓哄垹闄ょ殑鏉′欢鏁版嵁銆�

delete from student2 where id not in(select mid from (selectmin(id) mid

from student2 group by name) as t);

鎴栬�咃細

delete from student2 where id not in(select min(id) from (select* from s

tudent2) as t group by t.name);

14.鑸┖缃戠殑鍑犱釜鑸彮鏌ヨ棰橈細

琛ㄧ粨鏋勫涓嬶細

flight{flightID,StartCityID ,endCityID,StartTime}

city{cityID, CityName)

瀹為獙鐜锛�

create table city(cityID int auto_increment primary key,cityNamevarchar(20));

create table flight (flightID int auto_increment primary key,

StartCityID intreferences city(cityID),

endCityID 聽int references city(cityID),

StartTimetimestamp);

//鑸彮鏈潵搴旇娌℃湁鏃ユ湡閮ㄥ垎鎵嶅ソ锛屼絾鏄笅闈㈢殑棰樼洰褰撲腑娑夊強鍒颁簡鏃ユ湡

insert into city values(null,'鍖椾含'),(null,'涓婃捣'),(null,'骞垮窞');

insert into flight values

(null,1,2,'9:37:23'),(null,1,3,'9:37:23'),(null,1,2,'10:37:23'),(null,2,3,'10:37:23');

1銆佹煡璇㈣捣椋炲煄甯傛槸鍖椾含鐨勬墍鏈夎埅鐝紝鎸夊埌杈惧煄甯傜殑鍚嶅瓧鎺掑簭

鍙備笌杩愮畻鐨勫垪鏄垜璧风爜鑳藉鏄剧ず鍑烘潵鐨勯偅浜涘垪锛屼絾鏈�缁堟垜涓嶄竴瀹氭妸瀹冧滑鏄剧ず鍑烘潵銆傚悇涓〃缁勫悎鍑烘潵鐨勪腑闂寸粨鏋滃瓧娈典腑蹇呴』鍖呭惈鎵�鏈夎繍绠楃殑瀛楁銆�

select 聽* from flight f,city c

where f.endcityid =c.cityid and startcityid =

(select c1.cityidfrom city c1 where c1.cityname = "鍖椾含")

order by c.citynameasc;

mysql> select flight.flightid,'鍖椾含' startcity, e.cityname from flight,city e wh

ere flight.endcityid=e.cityid and flight.startcityid=(selectcityid from city wh

ere cityname='鍖椾含');

mysql> select flight.flightid,s.cityname,e.cityname fromflight,city s,city e wh

ere flight.startcityid=s.cityid and s.cityname='鍖椾含' andflight.endCityId=e.cit

yID order by e.cityName desc;

2銆佹煡璇㈠寳浜埌涓婃捣鐨勬墍鏈夎埅鐝邯褰曪紙璧烽鍩庡競锛屽埌杈惧煄甯傦紝璧烽鏃堕棿锛岃埅鐝彿锛�

select c1.CityName,c2.CityName,f.StartTime,f.flightID

from city c1,city c2,flight f

where f.StartCityID=c1.cityID

and f.endCityID=c2.cityID

and c1.cityName='鍖椾含'

and c2.cityName='涓婃捣'

3銆佹煡璇㈠叿浣撴煇涓�澶╋紙2005-5-8锛夌殑鍖椾含鍒颁笂娴风殑鐨勮埅鐝鏁�

select count(*) from

(select c1.CityName,c2.CityName,f.StartTime,f.flightID

from city c1,city c2,flight f

where f.StartCityID=c1.cityID

and f.endCityID=c2.cityID

and c1.cityName='鍖椾含'

and c2.cityName='涓婃捣'

and 鏌ュ府鍔╄幏寰楃殑鏌愪釜鏃ユ湡澶勭悊鍑芥暟(startTime) like '2005-5-8%'

mysql涓彁鍙栨棩鏈熼儴鍒嗚繘琛屾瘮杈冪殑绀轰緥浠g爜濡備笅锛�

select * from flight wheredate_format(starttime,'%Y-%m-%d')='1998-01-02'

15.鏌ュ嚭姣旂粡鐞嗚柂姘磋繕楂樼殑鍛樺伐淇℃伅锛�

Drop table if not exists employees;

create table employees(id int primary key auto_increment,namevarchar(50)

,salary int,managerid intreferences employees(id));

insert into employees values (null,' lhm',10000,null), (null,'zxx',15000,1

),(null,'flx',9000,1),(null,'tg',10000,2),(null,'wzg',10000,3);

Wzg澶т簬flx,lhm澶т簬zxx

瑙i鎬濊矾锛�

鏍规嵁sql璇彞鐨勬煡璇㈢壒鐐癸紝鏄�愯杩涜杩愮畻锛屼笉鍙兘涓よ鍚屾椂鍙備笌杩愮畻銆�

娑夊強浜嗗憳宸ヨ柂姘村拰缁忕悊钖按锛屾墍鏈夛紝涓�琛岃褰曡鍚屾椂鍖呭惈涓や釜钖按锛屾墍鏈夋兂鍒拌鎶婅繖涓〃鑷叧鑱旂粍鍚堜竴涓嬨��

棣栧厛瑕佺粍鍚堝嚭涓�涓寘鍚湁鍚勪釜鍛樺伐鍙婅鍛樺伐鐨勭粡鐞嗕俊鎭殑闀胯褰曪紝璀锛屽乏鍗婇儴鍒嗘槸鍛樺伐锛屽彸鍗婇儴鍒嗘槸缁忕悊銆傝�岃开鍗″皵绉細缁勫悎鍑哄緢澶氬瀮鍦句俊鎭紝鍏堝幓闄よ繖浜涘瀮鍦句俊鎭��

select e.* from employees e,employees m where e.managerid=m.idand e.sala

ry>m.salary;

16銆佹眰鍑哄皬浜�45宀佺殑鍚勪釜鑰佸笀鎵�甯︾殑澶т簬12宀佺殑瀛︾敓浜烘暟

鏁版嵁搴撲腑鏈�3涓〃 teacher琛紝student琛紝tea_stu鍏崇郴琛ㄣ��

teacher 琛� teaID name age

student 琛� stuID name age

teacher_student琛� teaID stuID

瑕佹眰鐢ㄤ竴鏉ql鏌ヨ鍑鸿繖鏍风殑缁撴灉

1.鏄剧ず鐨勫瓧娈佃鏈夎�佸笀name, age姣忎釜鑰佸笀鎵�甯︾殑瀛︾敓浜烘暟

2 鍙垪鍑鸿�佸笀age涓�40浠ヤ笅锛屽鐢焌ge涓�12浠ヤ笂鐨勮褰�

棰勫鐭ヨ瘑锛�

1.sql璇彞鏄姣忎竴鏉¤褰曚緷娆″鐞嗭紝鏉′欢涓虹湡鍒欐墽琛屽姩浣滐紙select,insert,delete,update锛�

2.鍙鏄开鍗″皵绉紝灏变細浜х敓鈥滃瀮鍦锯�濅俊鎭紝鎵�浠ワ紝鍙杩崱灏旂Н浜嗭紝鎴戜滑棣栧厛灏辫鎯冲埌娓呴櫎鈥滃瀮鍦锯�濅俊鎭�

瀹為獙鍑嗗锛�

drop table if exists tea_stu;

drop table if exists teacher;

drop table if exists student;

create table teacher(teaID int primarykey,name varchar(50),age int);

create table student(stuID int primarykey,name varchar(50),age int);

create table tea_stu(teaID int referencesteacher(teaID),stuID int references student(stuID));

insertinto teacher values(1,'zxx',45), (2,'lhm',25) , (3,'wzg',26) , (4,'tg',27);

insertinto student values(1,'wy',11), (2,'dh',25) , (3,'ysq',26) , (4,'mxc',27);

insertinto tea_stu values(1,1), (1,2), (1,3);

insertinto tea_stu values(2,2), (2,3), (2,4);

insert into tea_stu values(3,3), (3,4), (3,1);

insertinto tea_stu values(4,4), (4,1), (4,2) , (4,3);

缁撴灉锛�2脿3,3脿2,4脿3

瑙i鎬濊矾锛氾紙鐪熷疄闈㈣瘯绛旈鏃讹紝涔熻鍐欏嚭姣忎釜鍒嗘瀽姝ラ锛屽鏋滅焊寮犱笉澶燂紝灏辨壘鍒汉瑕侊級

1瑕佷細缁熻鍒嗙粍淇℃伅锛岀粺璁′俊鎭斁鍦ㄤ腑闂磋〃涓細

selectteaid,count(*) from tea_stu group by teaid;

2鎺ョ潃鍏跺疄搴旇鏄瓫闄ゆ帀灏忎簬12宀佺殑瀛︾敓锛岀劧鍚庡啀杩涜缁熻锛屼腑闂磋〃蹇呴』涓巗tudent鍏宠仈鎵嶈兘寰楀埌12宀佷互涓嬪鐢熷拰鎶婅瀛︾敓璁板綍浠庝腑闂磋〃涓墧闄わ紝浠g爜鏄細

selecttea_stu.teaid,count(*) total from student,tea_stu

wherestudent.stuid=tea_stu.stuid and student.age>12 group by tea_stu.teaid

3.鎺ョ潃鎶婁笂闈㈢殑缁撴灉鍋氭垚铏氳〃涓巘eacher杩涜鍏宠仈锛屽苟绛涢櫎澶т簬45鐨勮�佸笀

selectteacher.teaid,teacher.name,total from teacher ,(select tea_stu.tea

id,count(*)total from student,tea_stu where student.stuid=tea_stu.stuid and stu

dent.age>12group by tea_stu.teaid) as tea_stu2where teacher.teaid=tea_stu2.tea

idand teacher.age<45;

17.姹傚嚭鍙戝笘鏈�澶氱殑浜猴細

select authorid,count(*) total from articles

group by authorid

having total=

(select max(total2) from(select count(*) total2 from articles group by authorid) as t);

select t.authorid,max(t.total) from

锛坰elect authorid,count(*) total from articles锛塧s t

杩欐潯璇彞涓嶈锛屽洜涓簃ax鍙湁涓�鍒楋紝涓嶈兘涓庡叾浠栧垪娣锋穯銆�

select authorid,count(*) total from articles

group by authorid having total=max(total)涔熶笉琛屻��

18銆佷竴涓敤鎴疯〃涓湁涓�涓Н鍒嗗瓧娈碉紝鍋囧鏁版嵁搴撲腑鏈�100澶氫竾涓敤鎴凤紝鑻ヨ鍦ㄦ瘡骞寸涓�澶╁噷鏅ㄥ皢绉垎娓呴浂锛屼綘灏嗚�冭檻浠�涔堬紝浣犲皢鎯充粈涔堝姙娉曡В鍐�?

alter table drop column score;

alter table add colunm score int;

鍙兘浼氬緢蹇紝浣嗘槸闇�瑕佽瘯楠岋紝璇曢獙涓嶈兘鎷跨湡瀹炵殑鐜鏉ユ搷鍒�锛屽苟涓旇娉ㄦ剰锛�

杩欐牱鐨勬搷浣滄椂鏃犳硶鍥炴粴鐨勶紝鍦ㄦ垜鐨勫嵃璞′腑锛屽彧鏈塱nert update delete绛塂ML璇彞鎵嶈兘鍥炴粴锛�

瀵逛簬create table,drop table ,alter table绛塂DL璇彞鏄笉鑳藉洖婊氥��

瑙e喅鏂规涓�锛寀pdate user set score=0;

瑙e喅鏂规浜岋紝鍋囪涓婇潰鐨勪唬鐮佽鎵ц濂介暱鏃堕棿锛岃秴鍑烘垜浠殑瀹瑰繊鑼冨洿锛岄偅鎴戝氨alter table user drop columnscore;alter table user add column score int銆�

涓嬮潰浠g爜瀹炵幇姣忓勾鐨勯偅涓噷鏅ㄦ椂鍒昏繘琛屾竻闆躲��

Runnable runnable =

new Runnable(){

public voidrun(){

clearDb();

schedule(this,newDate(new Date().getYear()+1,0,0));

}

};

schedule(runnable,

new Date(newDate().getYear()+1,0,1));

19銆佷竴涓敤鎴峰叿鏈夊涓鑹诧紝璇锋煡璇㈠嚭璇ヨ〃涓叿鏈夎鐢ㄦ埛鐨勬墍鏈夎鑹茬殑鍏朵粬鐢ㄦ埛銆�

select count(*) as num,tb.id

from

tb,

(select role from tbwhere id=xxx) as t1

where

tb.role = t1.role andtb.id != t1.id

group by tb.id

having

num = select count(role)from tb where id=xxx;

20. xxx鍏徃鐨剆ql闈㈣瘯

TableEMPLOYEESStructure:

EMPLOYEE_IDNUMBER 聽 聽 聽 聽Primary Key,

FIRST_NAMEVARCHAR2(25),

LAST_NAMEVARCHAR2(25),

Salary number(8,2),

HiredDate DATE,

Departmentid number(2)

TableDepartmentsStructure:

Departmentid number(2)Primary Key,

DepartmentNameVARCHAR2(25).

(2锛夊熀浜庝笂杩癊MPLOYEES琛ㄥ啓鍑烘煡璇細鍐欏嚭闆囩敤鏃ユ湡鍦ㄤ粖骞寸殑锛屾垨鑰呭伐璧勫湪[1000,2000]涔嬮棿鐨勶紝鎴栬�呭憳宸ュ鍚嶏紙last_name锛変互鈥橭bama鈥欐墦澶寸殑鎵�鏈夊憳宸ワ紝鍒楀嚭杩欎簺鍛樺伐鐨勫叏閮ㄤ釜浜轰俊鎭�傦紙4鍒嗭級

select* from employees

whereYear(hiredDate) = Year(date())

or (salary between 1000 and 200)

or left(last_name,3)='abc';

(3) 鍩轰簬涓婅堪EMPLOYEES琛ㄥ啓鍑烘煡璇細鏌ュ嚭閮ㄩ棬骞冲潎宸ヨ祫澶т簬1800鍏冪殑閮ㄩ棬鐨勬墍鏈夊憳宸ワ紝鍒楀嚭杩欎簺鍛樺伐鐨勫叏閮ㄤ釜浜轰俊鎭�傦紙4鍒嗭級

mysql>select id,name,salary,deptid did from employee1 where (select avg(salary)

from employee1 where deptid = did) > 1800;

(4) 鍩轰簬涓婅堪EMPLOYEES琛ㄥ啓鍑烘煡璇細鏌ュ嚭涓汉宸ヨ祫楂樹簬鍏舵墍鍦ㄩ儴闂ㄥ钩鍧囧伐璧勭殑鍛樺伐锛屽垪鍑鸿繖浜涘憳宸ョ殑鍏ㄩ儴涓汉淇℃伅鍙婅鍛樺伐宸ヨ祫楂樺嚭閮ㄩ棬骞冲潎宸ヨ祫鐧惧垎姣斻�傦紙5鍒嗭級

selectemployee1.*,(employee1.salary-t.avgSalary)*100/employee1.salary

fromemployee1,

(select deptid,avg(salary) avgSalary fromemployee1 group by deptid) as t

whereemployee1.deptid = t.deptid and employee1.salary>t.avgSalary;

21銆佹敞鍐孞dbc椹卞姩绋嬪簭鐨勪笁绉嶆柟寮�

22銆佺敤JDBC濡備綍璋冪敤瀛樺偍杩囩▼

浠g爜濡備笅锛�

packagecom.huawei.interview.lym;

importjava.sql.CallableStatement;

importjava.sql.Connection;

importjava.sql.DriverManager;

importjava.sql.SQLException;

importjava.sql.Types;

publicclassJdbcTest {

/**

*@paramargs

*/

public static voidmain(String[] args) {

//TODOAuto-generated method stub

Connection cn =null;

CallableStatement cstmt =null;

try{

//杩欓噷鏈�濂戒笉瑕佽繖涔堝共锛屽洜涓洪┍鍔ㄥ悕鍐欐鍦ㄧ▼搴忎腑浜�

Class.forName("com.mysql.jdbc.Driver");

//瀹為檯椤圭洰涓紝杩欓噷搴旂敤DataSource鏁版嵁锛屽鏋滅敤妗嗘灦锛�

//杩欎釜鏁版嵁婧愪笉闇�瑕佹垜浠紪鐮佸垱寤猴紝鎴戜滑鍙渶Datasource ds =context.lookup()

//cn = ds.getConnection();

cn = DriverManager.getConnection("jdbc:mysql:///test","root","root");

cstmt = cn.prepareCall("{callinsert_Student(?,?,?)}");

cstmt.registerOutParameter(3,Types.INTEGER);

cstmt.setString(1,"wangwu");

cstmt.setInt(2, 25);

cstmt.execute();

//get绗嚑涓紝涓嶅悓鐨勬暟鎹簱涓嶄竴鏍凤紝寤鸿涓嶅啓

System.out.println(cstmt.getString(3));

}catch(Exception e) {

//TODOAuto-generated catchblock

e.printStackTrace();

}

finally

{

/*try{cstmt.close();}catch(Exceptione){}

try{cn.close();}catch(Exceptione){}*/

try{

if(cstmt !=null)

cstmt.close();

if(cn !=null)

cn.close();

}catch(SQLException e) {

//TODOAuto-generatedcatch block

e.printStackTrace();

}

}

}

23銆丣DBC涓殑PreparedStatement鐩告瘮Statement鐨勫ソ澶�

绛旓細涓�涓猻ql鍛戒护鍙戠粰鏈嶅姟鍣ㄥ幓鎵ц鐨勬楠や负锛氳娉曟鏌ワ紝璇箟鍒嗘瀽锛岀紪璇戞垚鍐呴儴鎸囦护锛岀紦瀛樻寚浠わ紝鎵ц鎸囦护绛夎繃绋嬨��

select * from student where id =3----缂撳瓨--脿xxxxx浜岃繘鍒跺懡浠�

select * from student where id =3----鐩存帴鍙�-脿xxxxx浜岃繘鍒跺懡浠�

select * from student where id =4--- -脿浼氭�庝箞骞诧紵

濡傛灉褰撳垵鏄痵elect * from student where id =?--- -脿鍙堜細鎬庝箞骞诧紵

涓婇潰璇寸殑鏄�ц兘鎻愰珮

鍙互闃叉sql娉ㄥ叆銆�

24. 鍐欎竴涓敤jdbc杩炴帴骞惰闂畂racle鏁版嵁鐨勭▼搴忎唬鐮�

25銆丆lass.forName鐨勪綔鐢�?涓轰粈涔堣鐢�?

绛旓細鎸夊弬鏁颁腑鎸囧畾鐨勫瓧绗︿覆褰㈠紡鐨勭被鍚嶅幓鎼滅储骞跺姞杞界浉搴旂殑绫伙紝濡傛灉璇ョ被瀛楄妭鐮佸凡缁忚鍔犺浇杩囷紝鍒欒繑鍥炰唬琛ㄨ瀛楄妭鐮佺殑Class瀹炰緥瀵硅薄锛屽惁鍒欙紝鎸夌被鍔犺浇鍣ㄧ殑濮旀墭鏈哄埗鍘绘悳绱㈠拰鍔犺浇璇ョ被锛屽鏋滄墍鏈夌殑绫诲姞杞藉櫒閮芥棤娉曞姞杞藉埌璇ョ被锛屽垯鎶涘嚭ClassNotFoundException銆傚姞杞藉畬杩欎釜Class瀛楄妭鐮佸悗锛屾帴鐫�灏卞彲浠ヤ娇鐢–lass瀛楄妭鐮佺殑newInstance鏂规硶鍘诲垱寤鸿绫荤殑瀹炰緥瀵硅薄浜嗐��

鏈夋椂鍊欙紝鎴戜滑绋嬪簭涓墍鏈変娇鐢ㄧ殑鍏蜂綋绫诲悕鍦ㄨ璁℃椂锛堝嵆寮�鍙戞椂锛夋棤娉曠‘瀹氾紝鍙湁绋嬪簭杩愯鏃舵墠鑳界‘瀹氾紝杩欐椂鍊欏氨闇�瑕佷娇鐢–lass.forName鍘诲姩鎬佸姞杞借绫伙紝杩欎釜绫诲悕閫氬父鏄湪閰嶇疆鏂囦欢涓厤缃殑锛屼緥濡傦紝spring鐨刬oc涓瘡娆′緷璧栨敞鍏ョ殑鍏蜂綋绫诲氨鏄繖鏍烽厤缃殑锛宩dbc鐨勯┍鍔ㄧ被鍚嶉�氬父涔熸槸閫氳繃閰嶇疆鏂囦欢鏉ラ厤缃殑锛屼互渚垮湪浜у搧浜や粯浣跨敤鍚庝笉鐢ㄤ慨鏀规簮绋嬪簭灏卞彲浠ユ洿鎹㈤┍鍔ㄧ被鍚嶃��

26銆佸ぇ鏁版嵁閲忎笅鐨勫垎椤佃В鍐虫柟娉曘��

绛旓細鏈�濂界殑鍔炴硶鏄埄鐢╯ql璇彞杩涜鍒嗛〉锛岃繖鏍锋瘡娆℃煡璇㈠嚭鐨勭粨鏋滈泦涓氨鍙寘鍚煇椤电殑鏁版嵁鍐呭銆傚啀sql璇彞鏃犳硶瀹炵幇鍒嗛〉鐨勬儏鍐典笅锛屽彲浠ヨ�冭檻瀵瑰ぇ鐨勭粨鏋滈泦閫氳繃娓告爣瀹氫綅鏂瑰紡鏉ヨ幏鍙栨煇椤电殑鏁版嵁銆�

sql璇彞鍒嗛〉锛屼笉鍚岀殑鏁版嵁搴撲笅鐨勫垎椤垫柟妗堝悇涓嶄竴鏍凤紝涓嬮潰鏄富娴佺殑涓夌鏁版嵁搴撶殑鍒嗛〉sql锛�

sql server:

String sql =

"select top" + pageSize + " * from students where id not in" +

"(select top "+ pageSize * (pageNumber-1) + " id from students order by id)" +

"order by id";

mysql:

String sql =

"select * fromstudents order by id limit " + pageSize*(pageNumber-1) + "," +pageSize;

oracle:

String sql =

"select * from " +

(select *,rownum rid from (select * fromstudents order by postime desc) where rid<=" + pagesize*pagenumber +") as t" +

"where t>" +pageSize*(pageNumber-1);

27銆佺敤 JDBC鏌ヨ瀛︾敓鎴愮哗鍗�,鎶婁富瑕佷唬鐮佸啓鍑烘潵锛堣�冭瘯姒傜巼鏋佸ぇ锛�.

Connection cn = null;

PreparedStatement pstmt =null;

Resultset rs = null;

try

{

Class.forname(driveClassName);

cn = 聽DriverManager.getConnection(url,username,password);

pstmt =cn.prepareStatement(鈥渟elect 聽score.* fromscore ,student 鈥� +

鈥渨herescore.stuId = student.id and student.name = ?鈥�);

pstmt.setString(1,studentName);

Resultset rs =pstmt.executeQuery();

while(rs.next())

{

system.out.println(rs.getInt(鈥渟ubject鈥�) 聽+ 聽鈥� 聽 聽鈥� + rs.getFloat(鈥渟core鈥�) );

}

}catch(Exception e){e.printStackTrace();}

finally

{

if(rs != null) try{rs.close() }catch(exception e){}

if(pstmt != null)try{pstmt.close()}catch(exception e){}

if(cn != null) try{cn.close() }catch(exception e){}

}

28銆佽繖娈典唬鐮佹湁浠�涔堜笉瓒充箣澶�?

try {

Connection conn = ...;

Statement stmt = ...;

ResultSet rs =stmt.executeQuery("select * from table1");

while(rs.next()) {

}

} catch(Exception ex) {

}

绛旓細娌℃湁finally璇彞鏉ュ叧闂悇涓璞★紝鍙﹀锛屼娇鐢╢inally涔嬪悗锛岃鎶婂彉閲忕殑瀹氫箟鏀惧湪try璇彞鍧楃殑澶栭潰锛屼互渚垮湪try璇彞鍧椾箣澶栫殑finally鍧椾腑浠嶅彲浠ヨ闂繖浜涘彉閲忋��

29銆佽鍑烘暟鎹繛鎺ユ睜鐨勫伐浣滄満鍒舵槸浠�涔�?

J2EE鏈嶅姟鍣ㄥ惎鍔ㄦ椂浼氬缓绔嬩竴瀹氭暟閲忕殑姹犺繛鎺ワ紝骞朵竴鐩寸淮鎸佷笉灏戜簬姝ゆ暟鐩殑姹犺繛鎺ャ�傚鎴风绋嬪簭闇�瑕佽繛鎺ユ椂锛屾睜椹卞姩绋嬪簭浼氳繑鍥炰竴涓湭浣跨敤鐨勬睜杩炴帴骞跺皢鍏惰〃璁颁负蹇欍�傚鏋滃綋鍓嶆病鏈夌┖闂茶繛鎺ワ紝姹犻┍鍔ㄧ▼搴忓氨鏂板缓涓�瀹氭暟閲忕殑杩炴帴锛屾柊寤鸿繛鎺ョ殑鏁伴噺鏈夐厤缃弬鏁板喅瀹氥�傚綋浣跨敤鐨勬睜杩炴帴璋冪敤瀹屾垚鍚庯紝姹犻┍鍔ㄧ▼搴忓皢姝よ繛鎺ヨ〃璁颁负绌洪棽锛屽叾浠栬皟鐢ㄥ氨鍙互浣跨敤杩欎釜杩炴帴銆�

瀹炵幇鏂瑰紡锛岃繑鍥炵殑Connection鏄師濮婥onnection鐨勪唬鐞嗭紝浠g悊Connection鐨刢lose鏂规硶涓嶆槸鐪熸鍏宠繛鎺ワ紝鑰屾槸鎶婂畠浠g悊鐨凜onnection瀵硅薄杩樺洖鍒拌繛鎺ユ睜涓��

30銆佷负浠�涔堣鐢� ORM? 鍜� JDBC鏈変綍涓嶄竴鏍�?

orm鏄竴绉嶆�濇兂锛屽氨鏄妸object杞彉鎴愭暟鎹簱涓殑璁板綍锛屾垨鑰呮妸鏁版嵁搴撲腑鐨勮褰曡浆鍙樻垚objecdt锛屾垜浠彲浠ョ敤jdbc鏉ュ疄鐜拌繖绉嶆�濇兂锛屽叾瀹烇紝濡傛灉鎴戜滑鐨勯」鐩槸涓ユ牸鎸夌収oop鏂瑰紡缂栧啓鐨勮瘽锛屾垜浠殑jdbc绋嬪簭涓嶇鏄湁鎰忚繕鏄棤鎰忥紝灏卞凡缁忓湪瀹炵幇orm鐨勫伐浣滀簡銆�

鐜板湪鏈夎澶歰rm宸ュ叿锛屽畠浠簳灞傝皟鐢╦dbc鏉ュ疄鐜颁簡orm宸ヤ綔锛屾垜浠洿鎺ヤ娇鐢ㄨ繖浜涘伐鍏凤紝灏辩渷鍘讳簡鐩存帴浣跨敤jdbc鐨勭箒鐞愮粏鑺傦紝鎻愰珮浜嗗紑鍙戞晥鐜囷紝鐜板湪鐢ㄧ殑杈冨鐨刼rm宸ュ叿鏄痟ibernate銆備篃鍚涓�浜涘叾浠杘rm宸ュ叿锛屽toplink,ojb绛夈��

鍏�. XML閮ㄥ垎

1銆亁ml鏈夊摢浜涜В鏋愭妧鏈�?鍖哄埆鏄粈涔�?

绛�:鏈塂OM,SAX,STAX绛�

DOM:澶勭悊澶у瀷鏂囦欢鏃跺叾鎬ц兘涓嬮檷鐨勯潪甯稿帀瀹炽�傝繖涓棶棰樻槸鐢盌OM鐨勬爲缁撴瀯鎵�閫犳垚鐨勶紝杩欑缁撴瀯鍗犵敤鐨勫唴瀛樿緝澶氾紝鑰屼笖DOM蹇呴』鍦ㄨВ鏋愭枃浠朵箣鍓嶆妸鏁翠釜鏂囨。瑁呭叆鍐呭瓨,閫傚悎瀵筙ML鐨勯殢鏈鸿闂甋AX:涓嶇幇浜嶥OM,SAX鏄簨浠堕┍鍔ㄥ瀷鐨刋ML瑙f瀽鏂瑰紡銆傚畠椤哄簭璇诲彇XML鏂囦欢锛屼笉闇�瑕佷竴娆″叏閮ㄨ杞芥暣涓枃浠躲�傚綋閬囧埌鍍忔枃浠跺紑澶达紝鏂囨。缁撴潫锛屾垨鑰呮爣绛惧紑澶翠笌鏍囩缁撴潫鏃讹紝瀹冧細瑙﹀彂涓�涓簨浠讹紝鐢ㄦ埛閫氳繃鍦ㄥ叾鍥炶皟浜嬩欢涓啓鍏ュ鐞嗕唬鐮佹潵澶勭悊XML鏂囦欢锛岄�傚悎瀵筙ML鐨勯『搴忚闂�

STAX:Streaming API for XML (StAX)

璁茶В杩欎簺鍖哄埆鏄笉闇�瑕佺壒鍒幓姣旇緝锛屽氨鍍忚浼犳櫤鎾涓庡叾浠栧煿璁満鏋勭殑鍖哄埆鏃讹紝鎴戜滑鍙渶璇存竻妤氫紶鏅烘挱瀹㈡湁浠�涔堢壒鐐瑰拰浼樼偣灏辫浜嗭紝杩欏氨宸茬粡闂存帴鍥炵瓟浜嗗郊姝ょ殑鍖哄埆銆�

2銆佷綘鍦ㄩ」鐩腑鐢ㄥ埌浜唜ml鎶�鏈殑鍝簺鏂归潰?濡備綍瀹炵幇鐨�?

绛�:鐢ㄥ埌浜嗘暟鎹瓨璐紝淇℃伅閰嶇疆涓ゆ柟闈€�傚湪鍋氭暟鎹氦鎹㈠钩鍙版椂锛屽皢涓嶈兘鏁版嵁婧愮殑鏁版嵁缁勮鎴怷ML鏂囦欢锛岀劧鍚庡皢XML鏂囦欢鍘嬬缉鎵撳寘鍔犲瘑鍚庨�氳繃缃戠粶浼犻�佺粰鎺ユ敹鑰咃紝鎺ユ敹瑙e瘑涓庤В鍘嬬缉鍚庡啀鍚孹ML鏂囦欢涓繕鍘熺浉鍏充俊鎭繘琛屽鐞嗐�傚湪鍋氳蒋浠堕厤缃椂锛屽埄鐢╔ML鍙互寰堟柟渚跨殑杩涜锛岃蒋浠剁殑鍚勭閰嶇疆鍙傛暟閮藉瓨璐湪XML鏂囦欢涓��

3銆佺敤jdom瑙f瀽xml鏂囦欢鏃跺浣曡В鍐充腑鏂囬棶棰�?濡備綍瑙f瀽?

绛�:鐪嬪涓嬩唬鐮�,鐢ㄧ紪鐮佹柟寮忓姞浠ヨВ鍐�

package test;

import java.io.*;

public class DOMTest

{

private String inFile = "c:\\people.xml"

private String outFile = "c:\\people.xml"

public static void main(String args[])

{

new DOMTest();

}

public DOMTest()

{

try

{

javax.xml.parsers.DocumentBuilder builder =

javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder();

org.w3c.dom.Document doc = builder.newDocument();

org.w3c.dom.Element root = doc.createElement("鑰佸笀");

org.w3c.dom.Element wang = doc.createElement("鐜�");

org.w3c.dom.Element liu = doc.createElement("鍒�");

wang.appendChild(doc.createTextNode("鎴戞槸鐜嬭�佸笀"));

root.appendChild(wang);

doc.appendChild(root);

javax.xml.transform.Transformer transformer =

javax.xml.transform.TransformerFactory.newInstance().newTransformer();

transformer.setOutputProperty(javax.xml.transform.OutputKeys.ENCODING,"gb2312");

transformer.setOutputProperty(javax.xml.transform.OutputKeys.INDENT,"yes");

transformer.transform(newjavax.xml.transform.dom.DOMSource(doc),

new

javax.xml.transform.stream.StreamResult(outFile));

}

catch (Exception e)

{

System.out.println (e.getMessage());

}

}

}

4銆佺紪绋嬬敤JAVA瑙f瀽XML鐨勬柟寮�.

绛�:鐢⊿AX鏂瑰紡瑙f瀽XML锛孹ML鏂囦欢濡備笅锛�


鐜嬪皬鏄�

淇℃伅瀛﹂櫌

6258113

鐢�,1955骞寸敓,鍗氬+锛�95骞磋皟鍏ユ捣鍗楀ぇ瀛�

浜嬩欢鍥炶皟绫籗AXHandler.java

import java.io.*;

import java.util.Hashtable;

import org.xml.sax.*;

public class SAXHandler extends HandlerBase

{

private Hashtable table = new Hashtable();

private String currentElement = null;

private String currentValue = null;

public void setTable(Hashtable table)

{

this.table = table;

}

public Hashtable getTable()

{

return table;

}

public void startElement(String tag, AttributeList attrs)

throws SAXException

{

currentElement = tag;

}

public void characters(char[] ch, int start, int length)

throws SAXException

{

currentValue = new String(ch, start, length);

}

public void endElement(String name) throws SAXException

{

if (currentElement.equals(name))

table.put(currentElement, currentValue);

}

}

JSP鍐呭鏄剧ず婧愮爜,SaxXml.jsp:

鍓栨瀽XML鏂囦欢people.xml

<%@ page errorPage=ErrPage.jsp

contentType=text/html;charset=GB2312 %>

<%@ page import=java.io.* %>

<%@ page import=java.util.Hashtable %>

<%@ page import=org.w3c.dom.* %>

<%@ page import=org.xml.sax.* %>

<%@ page import=javax.xml.parsers.SAXParserFactory %>

<%@ page import=javax.xml.parsers.SAXParser %>

<%@ page import=SAXHandler %>

<%

File file = new File(c:\people.xml);

FileReader reader = new FileReader(file);

Parser parser;

SAXParserFactory spf = SAXParserFactory.newInstance();

SAXParser sp = spf.newSAXParser();

SAXHandler handler = new SAXHandler();

sp.parse(new InputSource(reader), handler);

Hashtable hashTable = handler.getTable();

out.println();鏁欏笀淇℃伅琛�

濮撳悕 +

(String)hashTable.get(new String(name)) +);

瀛﹂櫌 +

(String)hashTable.get(newString(college))+);

鐢佃瘽 +

(String)hashTable.get(new String(telephone)) +);

澶囨敞 +

(String)hashTable.get(new String(notes)) +);

out.println();

%>

5銆乆ML鏂囨。瀹氫箟鏈夊嚑绉嶅舰寮忥紵瀹冧滑涔嬮棿鏈変綍鏈川鍖哄埆锛熻В鏋怷ML鏂囨。鏈夊摢鍑犵鏂瑰紡锛�

a: 涓ょ褰㈠紡 dtdschema锛宐:鏈川鍖哄埆:schema鏈韩鏄痻ml鐨勶紝鍙互琚玐ML瑙f瀽鍣ㄨВ鏋�(杩欎篃鏄粠DTD涓婂彂灞晄chema鐨勬牴鏈洰鐨�)锛宑:鏈塂OM,SAX,STAX绛�

DOM:澶勭悊澶у瀷鏂囦欢鏃跺叾鎬ц兘涓嬮檷鐨勯潪甯稿帀瀹炽�傝繖涓棶棰樻槸鐢盌OM鐨勬爲缁撴瀯鎵�閫犳垚鐨勶紝杩欑缁撴瀯鍗犵敤鐨勫唴瀛樿緝澶氾紝鑰屼笖DOM蹇呴』鍦ㄨВ鏋愭枃浠朵箣鍓嶆妸鏁翠釜鏂囨。瑁呭叆鍐呭瓨,閫傚悎瀵筙ML鐨勯殢鏈鸿闂�

SAX:涓嶇幇浜嶥OM,SAX鏄簨浠堕┍鍔ㄥ瀷鐨刋ML瑙f瀽鏂瑰紡銆傚畠椤哄簭璇诲彇XML鏂囦欢锛屼笉闇�瑕佷竴娆″叏閮ㄨ杞芥暣涓枃浠躲�傚綋閬囧埌鍍忔枃浠跺紑澶达紝鏂囨。缁撴潫锛屾垨鑰呮爣绛惧紑澶翠笌鏍囩缁撴潫鏃讹紝瀹冧細瑙﹀彂涓�涓簨浠讹紝鐢ㄦ埛閫氳繃鍦ㄥ叾鍥炶皟浜嬩欢涓啓鍏ュ鐞嗕唬鐮佹潵澶勭悊XML鏂囦欢锛岄�傚悎瀵筙ML鐨勯『搴忚闂�

STAX:Streaming API forXML (StAX)

涓�.娴佽鐨勬鏋朵笌鏂版妧鏈�

1銆佽皥璋堜綘瀵筍truts鐨勭悊瑙c��

绛�:

1. struts鏄竴涓寜MVC妯″紡璁捐鐨刉eb灞傛鏋讹紝鍏跺疄瀹冨氨鏄竴涓ぇ澶х殑servlet锛岃繖涓猄ervlet鍚嶄负ActionServlet锛屾垨鏄疉ctionServlet鐨勫瓙绫汇�傛垜浠彲浠ュ湪web.xml鏂囦欢涓皢绗﹀悎鏌愮鐗瑰緛鐨勬墍鏈夎姹備氦缁欒繖涓猄ervlet澶勭悊锛岃繖涓猄ervlet鍐嶅弬鐓т竴涓厤缃枃浠讹紙閫氬父涓�/WEB-INF/struts-config.xml锛夊皢鍚勪釜璇锋眰鍒嗗埆鍒嗛厤缁欎笉鍚岀殑action鍘诲鐞嗐��

涓�涓墿灞曠煡璇嗙偣锛歴truts鐨勯厤缃枃浠跺彲浠ユ湁澶氫釜锛屽彲浠ユ寜妯″潡閰嶇疆鍚勮嚜鐨勯厤缃枃浠讹紝杩欐牱鍙互闃叉閰嶇疆鏂囦欢鐨勮繃搴﹁啫鑳�锛�

2.ActionServlet鎶婅姹備氦缁檃ction鍘诲鐞嗕箣鍓嶏紝浼氬皢璇锋眰鍙傛暟灏佽鎴愪竴涓猣ormbean瀵硅薄锛堝氨鏄竴涓猨ava绫伙紝杩欎釜绫讳腑鐨勬瘡涓睘鎬у搴斾竴涓姹傚弬鏁帮級锛屽皝瑁呮垚涓�涓粈涔堟牱鐨刦ormbean瀵硅薄鍛紵鐪嬮厤缃枃浠躲��

3.瑕佽鏄庣殑鏄紝 ActionServlet鎶奻ormbean瀵硅薄浼犻�掔粰action鐨別xecute鏂规硶涔嬪墠锛屽彲鑳戒細璋冪敤formbean鐨剉alidate鏂规硶杩涜鏍¢獙锛屽彧鏈夋牎楠岄�氳繃鍚庢墠灏嗚繖涓猣ormbean瀵硅薄浼犻�掔粰action鐨別xecute鏂规硶锛屽惁鍒欙紝瀹冨皢杩斿洖涓�涓敊璇〉闈紝杩欎釜閿欒椤甸潰鐢眎nput灞炴�ф寚瀹氾紝锛堢湅閰嶇疆鏂囦欢锛変綔鑰呬负浠�涔堝皢杩欓噷鍛藉悕涓篿nput灞炴�э紝鑰屼笉鏄痚rror灞炴�э紝鎴戜滑鍚庨潰缁撳悎瀹為檯鐨勮繍琛屾晥鏋滆繘琛屽垎鏋愩��

4.action鎵ц瀹屽悗瑕佽繑鍥炴樉绀虹殑缁撴灉瑙嗗浘锛岃繖涓粨鏋滆鍥炬槸鐢ㄤ竴涓狝ctionForward瀵硅薄鏉ヨ〃绀虹殑锛宎ctionforward瀵硅薄閫氳繃struts-config.xml閰嶇疆鏂囦欢涓殑閰嶇疆鍏宠仈鍒版煇涓猨sp椤甸潰锛屽洜涓虹▼搴忎腑浣跨敤鐨勬槸鍦╯truts-config.xml閰嶇疆鏂囦欢涓簀sp椤甸潰璁剧疆鐨勯�昏緫鍚嶏紝杩欐牱鍙互瀹炵幇action绋嬪簭浠g爜涓庤繑鍥炵殑jsp椤甸潰鍚嶇О鐨勮В鑰︺��

浣犲struts鍙兘杩樻湁鑷繁鐨勫簲鐢ㄦ柟闈㈢殑缁忛獙锛岄偅涔熻涓�骞惰鍑烘潵銆�

2銆佽皥璋堜綘瀵笻ibernate鐨勭悊瑙c��

绛�:

1. 闈㈠悜瀵硅薄璁捐鐨勮蒋浠跺唴閮ㄨ繍琛岃繃绋嬪彲浠ョ悊瑙f垚灏辨槸鍦ㄤ笉鏂垱寤哄悇绉嶆柊瀵硅薄銆佸缓绔嬪璞′箣闂寸殑鍏崇郴锛岃皟鐢ㄥ璞$殑鏂规硶鏉ユ敼鍙樺悇涓璞$殑鐘舵�佸拰瀵硅薄娑堜骸鐨勮繃绋嬶紝涓嶇绋嬪簭杩愯鐨勮繃绋嬪拰鎿嶄綔鎬庝箞鏍凤紝鏈川涓婇兘鏄寰楀埌涓�涓粨鏋滐紝绋嬪簭涓婁竴涓椂鍒诲拰涓嬩竴涓椂鍒荤殑杩愯缁撴灉鐨勫樊寮傚氨琛ㄧ幇鍦ㄥ唴瀛樹腑鐨勫璞$姸鎬佸彂鐢熶簡鍙樺寲銆�

2.涓轰簡鍦ㄥ叧鏈哄拰鍐呭瓨绌洪棿涓嶅鐨勭姸鍐典笅锛屼繚鎸佺▼搴忕殑杩愯鐘舵�侊紝闇�瑕佸皢鍐呭瓨涓殑瀵硅薄鐘舵�佷繚瀛樺埌鎸佷箙鍖栬澶囧拰浠庢寔涔呭寲璁惧涓仮澶嶅嚭瀵硅薄鐨勭姸鎬侊紝閫氬父閮芥槸淇濆瓨鍒板叧绯绘暟鎹簱鏉ヤ繚瀛樺ぇ閲忓璞′俊鎭�備粠Java绋嬪簭鐨勮繍琛屽姛鑳戒笂鏉ヨ锛屼繚瀛樺璞$姸鎬佺殑鍔熻兘鐩告瘮绯荤粺杩愯鐨勫叾浠栧姛鑳芥潵璇达紝搴旇鏄竴涓緢涓嶈捣鐪肩殑闄勫睘鍔熻兘锛宩ava閲囩敤jdbc鏉ュ疄鐜拌繖涓姛鑳斤紝杩欎釜涓嶈捣鐪肩殑鍔熻兘鍗磋缂栧啓澶ч噺鐨勪唬鐮侊紝鑰屽仛鐨勪簨鎯呬粎浠呮槸淇濆瓨瀵硅薄鍜屾仮澶嶅璞★紝骞朵笖閭d簺澶ч噺鐨刯dbc浠g爜骞舵病鏈変粈涔堟妧鏈惈閲忥紝鍩烘湰涓婃槸閲囩敤涓�濂椾緥琛屽叕浜嬬殑鏍囧噯浠g爜妯℃澘鏉ョ紪鍐欙紝鏄竴绉嶈嫤娲诲拰閲嶅鎬х殑宸ヤ綔銆�

3.閫氳繃鏁版嵁搴撲繚瀛榡ava绋嬪簭杩愯鏃朵骇鐢熺殑瀵硅薄鍜屾仮澶嶅璞★紝鍏跺疄灏辨槸瀹炵幇浜唈ava瀵硅薄涓庡叧绯绘暟鎹簱璁板綍鐨勬槧灏勫叧绯伙紝绉颁负ORM锛堝嵆Object RelationMapping锛夛紝浜轰滑鍙互閫氳繃灏佽JDBC浠g爜鏉ュ疄鐜颁簡杩欑鍔熻兘锛屽皝瑁呭嚭鏉ョ殑浜у搧绉颁箣涓篛RM妗嗘灦锛孒ibernate灏辨槸鍏朵腑鐨勪竴绉嶆祦琛孫RM妗嗘灦銆備娇鐢℉ibernate妗嗘灦锛屼笉鐢ㄥ啓JDBC浠g爜锛屼粎浠呮槸璋冪敤涓�涓猻ave鏂规硶锛屽氨鍙互灏嗗璞′繚瀛樺埌鍏崇郴鏁版嵁搴撲腑锛屼粎浠呮槸璋冪敤涓�涓猤et鏂规硶锛屽氨鍙互浠庢暟鎹簱涓姞杞藉嚭涓�涓璞°��

4.浣跨敤Hibernate鐨勫熀鏈祦绋嬫槸锛氶厤缃瓹onfiguration瀵硅薄銆佷骇鐢烻essionFactory銆佸垱寤簊ession瀵硅薄锛屽惎鍔ㄤ簨鍔★紝瀹屾垚CRUD鎿嶄綔锛屾彁浜や簨鍔★紝鍏抽棴session銆�

5.浣跨敤Hibernate鏃讹紝鍏堣閰嶇疆hibernate.cfg.xml鏂囦欢锛屽叾涓厤缃暟鎹簱杩炴帴淇℃伅鍜屾柟瑷�绛夛紝杩樿涓烘瘡涓疄浣撻厤缃浉搴旂殑hbm.xml鏂囦欢锛宧ibernate.cfg.xml鏂囦欢涓渶瑕佺櫥璁版瘡涓猦bm.xml鏂囦欢銆�

6.鍦ㄥ簲鐢℉ibernate鏃讹紝閲嶇偣瑕佷簡瑙ession鐨勭紦瀛樺師鐞嗭紝绾ц仈锛屽欢杩熷姞杞藉拰hql鏌ヨ銆�

3銆丄OP鐨勪綔鐢ㄣ��

4銆佷綘瀵筍pring鐨勭悊瑙c��

1.Spring瀹炵幇浜嗗伐鍘傛ā寮忕殑宸ュ巶绫伙紙鍦ㄨ繖閲屾湁蹇呰瑙i噴娓呮浠�涔堟槸宸ュ巶妯″紡锛夛紝杩欎釜绫诲悕涓築eanFactory锛堝疄闄呬笂鏄竴涓帴鍙o級锛屽湪绋嬪簭涓�氬父BeanFactory鐨勫瓙绫籄pplicationContext銆係pring鐩稿綋浜庝竴涓ぇ鐨勫伐鍘傜被锛屽湪鍏堕厤缃枃浠朵腑閫氳繃鍏冪礌閰嶇疆鐢ㄤ簬鍒涘缓瀹炰緥瀵硅薄鐨勭被鍚嶅拰瀹炰緥瀵硅薄鐨勫睘鎬с��

2. Spring鎻愪緵浜嗗IOC鑹ソ鏀寔锛孖OC鏄竴绉嶇紪绋嬫�濇兂锛屾槸涓�绉嶆灦鏋勮壓鏈紝鍒╃敤杩欑鎬濇兂鍙互寰堝ソ鍦板疄鐜版ā鍧椾箣闂寸殑瑙h�︺�侷OC涔熺О涓篋I锛圖epency Injection锛夛紝浠�涔堝彨渚濊禆娉ㄥ叆鍛紵

璀锛孋lass Programmer

{

Computer computer =null;

public void code()

{

//Computercomputer = new IBMComputer();

//Computercomputer = beanfacotry.getComputer();

computer.write();

}

public voidsetComputer(Computer computer)

{

this.computer= computer;

}

}

鍙﹀涓ょ鏂瑰紡閮界敱渚濊禆锛岀涓�涓洿鎺ヤ緷璧栦簬鐩爣绫伙紝绗簩涓妸渚濊禆杞Щ鍒板伐鍘備笂锛岀涓変釜褰诲簳涓庣洰鏍囧拰宸ュ巶瑙h�︿簡銆傚湪spring鐨勯厤缃枃浠朵腑閰嶇疆鐗囨濡備笅锛�

3. Spring鎻愪緵浜嗗AOP鎶�鏈殑鑹ソ灏佽锛� AOP绉颁负闈㈠悜鍒囬潰缂栫▼锛屽氨鏄郴缁熶腑鏈夊緢澶氬悇涓嶇浉骞茬殑绫荤殑鏂规硶锛屽湪杩欎簺浼楀鏂规硶涓鍔犲叆鏌愮绯荤粺鍔熻兘鐨勪唬鐮侊紝渚嬪锛屽姞鍏ユ棩蹇楋紝鍔犲叆鏉冮檺鍒ゆ柇锛屽姞鍏ュ紓甯稿鐞嗭紝杩欑搴旂敤绉颁负AOP銆傚疄鐜癆OP鍔熻兘閲囩敤鐨勬槸浠g悊鎶�鏈紝瀹㈡埛绔▼搴忎笉鍐嶈皟鐢ㄧ洰鏍囷紝鑰岃皟鐢ㄤ唬鐞嗙被锛屼唬鐞嗙被涓庣洰鏍囩被瀵瑰鍏锋湁鐩稿悓鐨勬柟娉曞0鏄庯紝鏈変袱绉嶆柟寮忓彲浠ュ疄鐜扮浉鍚岀殑鏂规硶澹版槑锛屼竴鏄疄鐜扮浉鍚岀殑鎺ュ彛锛屼簩鏄綔涓虹洰鏍囩殑瀛愮被鍦紝JDK涓噰鐢≒roxy绫讳骇鐢熷姩鎬佷唬鐞嗙殑鏂瑰紡涓烘煇涓帴鍙g敓鎴愬疄鐜扮被锛屽鏋滆涓烘煇涓被鐢熸垚瀛愮被锛屽垯鍙互鐢–GLI B銆傚湪鐢熸垚鐨勪唬鐞嗙被鐨勬柟娉曚腑鍔犲叆绯荤粺鍔熻兘鍜岃皟鐢ㄧ洰鏍囩被鐨勭浉搴旀柟娉曪紝绯荤粺鍔熻兘鐨勪唬鐞嗕互Advice瀵硅薄杩涜鎻愪緵锛屾樉鐒惰鍒涘缓鍑轰唬鐞嗗璞★紝鑷冲皯闇�瑕佺洰鏍囩被鍜孉dvice绫汇�俿pring鎻愪緵浜嗚繖绉嶆敮鎸侊紝鍙渶瑕佸湪spring閰嶇疆鏂囦欢涓厤缃繖涓や釜鍏冪礌鍗冲彲瀹炵幇浠g悊鍜宎op鍔熻兘锛屼緥濡傦紝

5銆佽皥璋圫truts涓殑Action servlet銆�

6銆丼truts浼樼己鐐�

浼樼偣锛�

1.瀹炵幇MVC妯″紡锛岀粨鏋勬竻鏅�,浣垮紑鍙戣�呭彧鍏虫敞涓氬姟閫昏緫鐨勫疄鐜�.

2锛庢湁涓板瘜鐨則ag鍙互鐢� ,Struts鐨勬爣璁板簱(Taglib)锛屽鑳界伒娲诲姩鐢紝鍒欒兘澶уぇ鎻愰珮寮�鍙戞晥鐜�

3. 椤甸潰瀵艰埅

浣跨郴缁熺殑鑴夌粶鏇村姞娓呮櫚銆傞�氳繃涓�涓厤缃枃浠讹紝鍗冲彲鎶婃彙鏁翠釜绯荤粺鍚勯儴鍒嗕箣闂寸殑鑱旂郴锛岃繖瀵逛簬鍚庢湡鐨勭淮鎶ゆ湁鐫�鑾ぇ鐨勫ソ澶勩�傚挨鍏舵槸褰撳彟涓�鎵瑰紑鍙戣�呮帴鎵嬭繖涓」鐩椂锛岃繖绉嶄紭鍔夸綋鐜板緱鏇村姞鏄庢樉銆�

4. 鎻愪緵Exception澶勭悊鏈哄埗 .

5. 鏁版嵁搴撻摼鎺ユ睜绠$悊

6. 鏀寔I18N

缂虹偣

涓�銆� 聽 杞埌灞曠ず灞傛椂锛岄渶瑕侀厤缃甪orward锛屽鏋滄湁鍗佷釜灞曠ず灞傜殑jsp锛岄渶瑕侀厤缃崄娆truts锛岃�屼笖杩樹笉鍖呮嫭鏈夋椂鍊欑洰褰曘�佹枃浠跺彉鏇达紝闇�瑕侀噸鏂颁慨鏀筬orward锛屾敞鎰忥紝姣忔淇敼閰嶇疆涔嬪悗锛岃姹傞噸鏂伴儴缃叉暣涓」鐩紝鑰宼omcate杩欐牱鐨勬湇鍔″櫒锛岃繕蹇呴』閲嶆柊鍚姩鏈嶅姟鍣�

浜屻�� 聽 浜屻�� Struts鐨凙ction蹇呴渶鏄痶hread锛峴afe鏂瑰紡锛屽畠浠呬粎鍏佽涓�涓疄渚嬪幓澶勭悊鎵�鏈夌殑璇锋眰銆傛墍浠ction鐢ㄥ埌鐨勬墍鏈夌殑璧勬簮閮藉繀闇�缁熶竴鍚屾锛岃繖涓氨寮曡捣浜嗙嚎绋嬪畨鍏ㄧ殑闂銆�

涓夈�� 聽 聽娴嬭瘯涓嶆柟渚�. Struts鐨勬瘡涓狝ction閮藉悓Web灞傝�﹀悎鍦ㄤ竴璧凤紝杩欐牱瀹冪殑娴嬭瘯渚濊禆浜嶹eb瀹瑰櫒锛屽崟鍏冩祴璇曚篃寰堥毦瀹炵幇銆備笉杩囨湁涓�涓狫unit鐨勬墿灞曞伐鍏稴truts TestCase鍙互瀹炵幇瀹冪殑鍗曞厓娴嬭瘯銆�

鍥涖�� 聽 聽绫诲瀷鐨勮浆鎹�. Struts鐨凢ormBean鎶婃墍鏈夌殑鏁版嵁閮戒綔涓篠tring绫诲瀷锛屽畠鍙互浣跨敤宸ュ叿Commons-Beanutils杩涜绫诲瀷杞寲銆備絾瀹冪殑杞寲閮芥槸鍦–lass绾у埆锛岃�屼笖杞寲鐨勭被鍨嬫槸涓嶅彲閰嶇疆鐨勩�傜被鍨嬭浆鍖栨椂鐨勯敊璇俊鎭繑鍥炵粰鐢ㄦ埛涔熸槸闈炲父鍥伴毦鐨勩��

浜斻�� 聽 瀵筍ervlet鐨勪緷璧栨�ц繃寮�. Struts澶勭悊Action鏃跺繀闇�瑕佷緷璧朣ervletRequest鍜孲ervletResponse锛屾墍鏈夊畠鎽嗚劚涓嶄簡Servlet瀹瑰櫒銆�

鍏�� 聽 聽鍓嶇琛ㄨ揪寮忚瑷�鏂归潰.Struts闆嗘垚浜咼STL锛屾墍浠ュ畠涓昏浣跨敤JSTL鐨勮〃杈惧紡璇█鏉ヨ幏鍙栨暟鎹�傚彲鏄疛STL鐨勮〃杈惧紡璇█鍦–ollection鍜岀储寮曞睘鎬ф柟闈㈠鐞嗘樉寰楀緢寮便��

涓冦�� 聽 聽瀵笰ction鎵ц鐨勬帶鍒跺洶闅�. Struts鍒涘缓涓�涓狝ction锛屽鏋滄兂鎺у埗瀹冪殑鎵ц椤哄簭灏嗕細闈炲父鍥伴毦銆傜敋鑷充綘瑕侀噸鏂板幓鍐橲ervlet鏉ュ疄鐜颁綘鐨勮繖涓姛鑳介渶姹傘��

鍏�� 聽 聽瀵笰ction鎵ц鍓嶅拰鍚庣殑澶勭悊. Struts澶勭悊Action鐨勬椂鍊欐槸鍩轰簬class鐨刪ierarchies锛屽緢闅惧湪action澶勭悊鍓嶅拰鍚庤繘琛屾搷浣溿��

涔濄�� 聽 聽瀵逛簨浠舵敮鎸佷笉澶�.鍦╯truts涓紝瀹為檯鏄竴涓〃鍗旻orm瀵瑰簲涓�涓狝ction绫�(鎴朌ispatchAction)锛屾崲涓�鍙ヨ瘽璇达細鍦⊿truts涓疄闄呮槸涓�涓〃鍗曞彧鑳藉搴斾竴涓簨浠讹紝struts杩欑浜嬩欢鏂瑰紡绉颁负application event锛宎pplication event鍜宑omponent event鐩告瘮鏄竴绉嶇矖绮掑害鐨勪簨浠�

7銆丼TRUTS鐨勫簲鐢�(濡係TRUTS鏋舵瀯)

Struts鏄噰鐢↗ava Servlet/JavaServer Pages鎶�鏈紝寮�鍙慦eb搴旂敤绋嬪簭鐨勫紑鏀炬簮鐮佺殑framework銆傞噰鐢⊿truts鑳藉紑鍙戝嚭鍩轰簬MVC(Model-View-Controller)璁捐妯″紡鐨勫簲鐢ㄦ瀯鏋躲�� Struts鏈夊涓嬬殑涓昏鍔熻兘锛氫竴.鍖呭惈涓�涓猚ontroller servlet锛岃兘灏嗙敤鎴风殑璇锋眰鍙戦�佸埌鐩稿簲鐨凙ction瀵硅薄銆備簩.JSP鑷敱tag搴擄紝骞朵笖鍦╟ontroller servlet涓彁渚涘叧鑱旀敮鎸侊紝甯姪寮�鍙戝憳鍒涘缓浜や簰寮忚〃鍗曞簲鐢ㄣ�備笁.鎻愪緵浜嗕竴绯诲垪瀹炵敤瀵硅薄锛歑ML澶勭悊銆侀�氳繃Java reflection APIs鑷姩澶勭悊JavaBeans灞炴�с�佸浗闄呭寲鐨勬彁绀哄拰娑堟伅銆�

8銆佽璇磗truts1涓巗truts2鐨勫尯鍒��

1.閮芥槸MVC鐨刉EB妗嗘灦,

2 struts1鐨勮�佺墝妗嗘灦锛屽簲鐢ㄥ緢骞挎硾锛屾湁寰堝ソ鐨勭兢浼楀熀纭�锛屼娇鐢ㄥ畠寮�鍙戦闄╁緢灏忥紝鎴愭湰鏇翠綆锛乻truts2铏界劧鍩轰簬杩欎釜妗嗘灦锛屼絾鏄簲鐢ㄧ兢浼楀苟澶氾紝鐩稿涓嶆垚鐔燂紝鏈煡鐨勯闄╁拰鍙樺寲寰堝锛屽紑鍙戜汉鍛樼浉瀵逛笉濂芥嫑锛屼娇鐢ㄥ畠寮�鍙戦」鐩殑椋庨櫓绯绘暟鏇村ぇ锛岀敤浜烘垚鏈洿楂橈紒

3.struts2姣曠珶鏄珯鍦ㄥ墠杈堢殑鍩虹璁捐鍑烘潵锛屽畠浼氭敼鍠勫拰瀹屽杽struts1涓殑涓�浜涚己闄凤紝struts1涓竴浜涙偓鑰屾湭鍐抽棶棰樺湪struts2寰楀埌浜嗚В鍐炽��

4.struts1鐨勫墠绔帶鍒跺櫒鏄竴涓猄ervlet锛屽悕绉颁负ActionServlet锛宻truts2鐨勫墠绔帶鍒跺櫒鏄竴涓猣ilter锛屽湪struts2.0涓彨FilterDispatcher锛屽湪struts2.1涓彨StrutsPrepareAndExecuteFilter銆�

5.struts1鐨刟ction闇�瑕佺户鎵緼ction绫伙紝struts2鐨刟ction鍙互涓嶇户鎵夸换浣曠被锛泂truts1瀵瑰悓涓�涓矾寰勭殑鎵�鏈夎姹傚叡浜竴涓狝ction瀹炰緥锛宻truts2瀵瑰悓涓�涓矾寰勭殑姣忎釜璇锋眰鍒嗗埆浣跨敤涓�涓嫭绔婣ction瀹炰緥瀵硅薄锛屾墍鏈夊浜巗truts2鐨凙ction涓嶇敤鑰冭檻绾跨▼瀹夊叏闂銆�

6.鍦╯truts1涓娇鐢╢ormbean灏佽璇锋眰鍙傛暟锛屽湪struts2涓洿鎺ヤ娇鐢╝ction鐨勫睘鎬ф潵灏佽璇锋眰鍙傛暟銆�

7.struts1涓殑澶氫釜涓氬姟鏂规硶鏀惧湪涓�涓狝ction涓椂锛堝嵆缁ф壙DispatchAction鏃讹級锛岃涔堥兘鏍¢獙锛岃涔堥兘涓嶆牎楠岋紱瀵逛簬struts2锛屽彲浠ユ寚瀹氬彧瀵规煇涓柟娉曡繘琛屾牎楠岋紝褰撲竴涓狝ction缁ф壙浜咥ctionSupport涓斿湪杩欎釜绫讳腑鍙紪鍐欎簡validateXxx()鏂规硶锛岄偅涔堝垯鍙Xxx()鏂规硶杩涜鏍¢獙銆�

锛堜竴涓姹傛潵浜嗙殑鎵ц娴佺▼杩涜鍒嗘瀽锛宻truts2鏄嚜鍔ㄦ敮鎸佸垎妯″潡寮�鍙戯紝骞跺彲浠ヤ笉鍚屾ā鍧楄缃笉鍚岀殑url鍓嶇紑锛岃繖鏄�氳繃package鐨刵amespace鏉ュ疄鐜扮殑锛泂truts2鏄敮鎸佸绉嶇被鍨嬬殑瑙嗗浘锛泂truts2鐨勮鍥惧湴鍧�鍙互鏄姩鎬佺殑锛屽嵆瑙嗗浘鐨勫悕绉版槸鏀寔鍙橀噺鏂瑰紡鐨勶紝涓句緥锛岃鍧涘彂甯栧け璐ュ悗鍥炴潵杩樿浼犻�抌oardid銆傝鍥惧唴瀹规樉绀烘柟闈細瀹冪殑鏍囩鐢╫gnl锛岃el寮哄ぇ寰堝锛屽湪鍥介檯鍖栨柟闈㈡敮鎸佸垎妯″潡绠$悊锛屼袱涓ā鍧楃敤鍒板悓鏍风殑key锛屽搴斾笉鍚岀殑娑堟伅锛涳級

涓嶴truts1涓嶅悓锛孲truts2瀵圭敤鎴风殑姣忎竴娆¤姹傞兘浼氬垱寤轰竴涓狝ction锛屾墍浠truts2涓殑Action鏄嚎绋嬪畨鍏ㄧ殑銆�

缁欐垜鍗拌薄鏈�娣卞埢鐨勬槸锛歴truts閰嶇疆鏂囦欢涓殑redirect瑙嗗浘鐨剈rl涓嶈兘鎺ュ彈鍙傛暟锛岃�宻truts2閰嶇疆鏂囦欢涓殑redirect瑙嗗浘鍙互鎺ュ彈鍙傛暟銆�

9銆乭ibernate涓殑update()鍜宻aveOrUpdate()鐨勫尯鍒紝session鐨刲oad()鍜実et()鐨勫尯鍒��

10銆佺畝杩� Hibernate鍜� JDBC鐨勪紭缂虹偣?濡備綍涔﹀啓涓�涓� one to many閰嶇疆鏂囦欢.

11銆乮Batis涓嶩ibernate鏈変粈涔堜笉鍚�?

鐩稿悓鐐癸細灞忚斀jdbc api鐨勫簳灞傝闂粏鑺傦紝浣跨敤鎴戜滑涓嶇敤涓巎dbc api鎵撲氦閬擄紝灏卞彲浠ヨ闂暟鎹��

jdbc api缂栫▼娴佺▼鍥哄畾锛岃繕灏唖ql璇彞涓巎ava浠g爜娣锋潅鍦ㄤ簡涓�璧凤紝缁忓父闇�瑕佹嫾鍑憇ql璇彞锛岀粏鑺傚緢绻佺悙銆�

ibatis鐨勫ソ澶勶細灞忚斀jdbc api鐨勫簳灞傝闂粏鑺傦紱灏唖ql璇彞涓巎ava浠g爜杩涜鍒嗙;鎻愪緵浜嗗皢缁撴灉闆嗚嚜鍔ㄥ皝瑁呯О涓哄疄浣撳璞″拰瀵硅薄鐨勯泦鍚堢殑鍔熻兘锛宷ueryForList杩斿洖瀵硅薄闆嗗悎锛岀敤queryForObject杩斿洖鍗曚釜瀵硅薄锛涙彁渚涗簡鑷姩灏嗗疄浣撳璞$殑灞炴�т紶閫掔粰sql璇彞鐨勫弬鏁般��

Hibernate鏄竴涓叏鑷姩鐨刼rm鏄犲皠宸ュ叿锛屽畠鍙互鑷姩鐢熸垚sql璇彞,ibatis闇�瑕佹垜浠嚜宸卞湪xml閰嶇疆鏂囦欢涓啓sql璇彞锛宧ibernate瑕佹瘮ibatis鍔熻兘璐熻矗鍜屽己澶у緢澶氥�傚洜涓篽ibernate鑷姩鐢熸垚sql璇彞锛屾垜浠棤娉曟帶鍒惰璇彞锛屾垜浠氨鏃犳硶鍘诲啓鐗瑰畾鐨勯珮鏁堢巼鐨剆ql銆傚浜庝竴浜涗笉澶鏉傜殑sql鏌ヨ锛宧ibernate鍙互寰堝ソ甯垜浠畬鎴愶紝浣嗘槸锛屽浜庣壒鍒鏉傜殑鏌ヨ锛宧ibernate灏卞緢闅鹃�傚簲浜嗭紝杩欐椂鍊欑敤ibatis灏辨槸涓嶉敊鐨勯�夋嫨锛屽洜涓篿batis杩樻槸鐢辨垜浠嚜宸卞啓sql璇彞銆�

12銆佸啓Hibernate鐨勪竴瀵瑰鍜屽瀵逛竴鍙屽悜鍏宠仈鐨刼rm閰嶇疆?

9銆乭ibernate鐨刬nverse灞炴�х殑浣滅敤?

瑙e喅鏂规涓�锛屾寜鐓bject[]鏁版嵁鍙栧嚭鏁版嵁锛岀劧鍚庤嚜宸辩粍bean

瑙e喅鏂规浜岋紝瀵规瘡涓〃鐨刡ean鍐欐瀯閫犲嚱鏁帮紝姣斿琛ㄤ竴瑕佹煡鍑篺ield1,field2涓や釜瀛楁锛岄偅涔堟湁涓�涓瀯閫犲嚱鏁板氨鏄疊ean(type1filed1,type2

field2) 锛岀劧鍚庡湪hql閲岄潰灏卞彲浠ョ洿鎺ョ敓鎴愯繖涓猙ean浜嗐��

13銆佸湪DAO涓浣曚綋鐜癉AO璁捐妯″紡?

瑙e喅鏂规涓�锛屾寜鐓bject[]鏁版嵁鍙栧嚭鏁版嵁锛岀劧鍚庤嚜宸辩粍bean

瑙e喅鏂规浜岋紝瀵规瘡涓〃鐨刡ean鍐欐瀯閫犲嚱鏁帮紝姣斿琛ㄤ竴瑕佹煡鍑篺ield1,field2涓や釜瀛楁锛岄偅涔堟湁涓�涓瀯閫犲嚱鏁板氨鏄疊ean(type1filed1,type2

field2) 锛岀劧鍚庡湪hql閲岄潰灏卞彲浠ョ洿鎺ョ敓鎴愯繖涓猙ean浜嗐��

14銆乻pring+Hibernate涓鎵樻柟妗堟�庝箞閰嶇疆?

瑙e喅鏂规涓�锛屾寜鐓bject[]鏁版嵁鍙栧嚭鏁版嵁锛岀劧鍚庤嚜宸辩粍bean

瑙e喅鏂规浜岋紝瀵规瘡涓〃鐨刡ean鍐欐瀯閫犲嚱鏁帮紝姣斿琛ㄤ竴瑕佹煡鍑篺ield1,field2涓や釜瀛楁锛岄偅涔堟湁涓�涓瀯閫犲嚱鏁板氨鏄疊ean(type1filed1,type2

field2) 锛岀劧鍚庡湪hql閲岄潰灏卞彲浠ョ洿鎺ョ敓鎴愯繖涓猙ean浜嗐��

15銆乻pring+Hibernate涓鎵樻柟妗堟�庝箞閰嶇疆?

瑙e喅鏂规涓�锛屾寜鐓bject[]鏁版嵁鍙栧嚭鏁版嵁锛岀劧鍚庤嚜宸辩粍bean

瑙e喅鏂规浜岋紝瀵规瘡涓〃鐨刡ean鍐欐瀯閫犲嚱鏁帮紝姣斿琛ㄤ竴瑕佹煡鍑篺ield1,field2涓や釜瀛楁锛岄偅涔堟湁涓�涓瀯閫犲嚱鏁板氨鏄疊ean(type1filed1,type2

field2) 锛岀劧鍚庡湪hql閲岄潰灏卞彲浠ョ洿鎺ョ敓鎴愯繖涓猙ean浜嗐��

16. hibernate杩涜澶氳〃鏌ヨ姣忎釜琛ㄤ腑鍚勫彇鍑犱釜瀛楁锛屼篃灏辨槸璇存煡璇㈠嚭鏉ョ殑缁撴灉闆嗘病鏈変竴涓疄浣撶被涓庝箣瀵瑰簲濡備綍瑙e喅锛�

瑙e喅鏂规涓�锛屾寜鐓bject[]鏁版嵁鍙栧嚭鏁版嵁锛岀劧鍚庤嚜宸辩粍bean

瑙e喅鏂规浜岋紝瀵规瘡涓〃鐨刡ean鍐欐瀯閫犲嚱鏁帮紝姣斿琛ㄤ竴瑕佹煡鍑篺ield1,field2涓や釜瀛楁锛岄偅涔堟湁涓�涓瀯閫犲嚱鏁板氨鏄疊ean(type1filed1,type2

field2) 锛岀劧鍚庡湪hql閲岄潰灏卞彲浠ョ洿鎺ョ敓鎴愯繖涓猙ean浜嗐��

17.浠嬬粛涓�涓婬ibernate鐨勪簩绾х紦瀛�

鎸夌収浠ヤ笅鎬濊矾鏉ュ洖绛旓細锛�1锛夐鍏堣娓呮浠�涔堟槸缂撳瓨锛岋紙2锛夊啀璇存湁浜唄ibernate鐨凷ession灏辨槸涓�绾х紦瀛橈紝鍗虫湁浜嗕竴绾х紦瀛橈紝涓轰粈涔堣繕瑕佹湁浜岀骇缂撳瓨锛岋紙3锛夋渶鍚庡啀璇村浣曢厤缃瓾ibernate鐨勪簩绾х紦瀛樸��

锛�1锛夌紦瀛樺氨鏄妸浠ュ墠浠庢暟鎹簱涓煡璇㈠嚭鏉ュ拰浣跨敤杩囩殑瀵硅薄淇濆瓨鍦ㄥ唴瀛樹腑锛堜竴涓暟鎹粨鏋勪腑锛夛紝杩欎釜鏁版嵁缁撴瀯閫氬父鏄垨绫讳技Hashmap锛屽綋浠ュ悗瑕佷娇鐢ㄦ煇涓璞℃椂锛屽厛鏌ヨ缂撳瓨涓槸鍚︽湁杩欎釜瀵硅薄锛屽鏋滄湁鍒欎娇鐢ㄧ紦瀛樹腑鐨勫璞★紝濡傛灉娌℃湁鍒欏幓鏌ヨ鏁版嵁搴擄紝骞跺皢鏌ヨ鍑烘潵鐨勫璞′繚瀛樺湪缂撳瓨涓紝浠ヤ究涓嬫浣跨敤銆備笅闈㈡槸缂撳瓨鐨勪吉浠g爜锛�

寮曞嚭hibernate鐨勭浜岀骇缂撳瓨锛岀敤涓嬮潰鐨勪吉浠g爜鍒嗘瀽浜咰ache鐨勫疄鐜板師鐞�

Dao

{

hashmap map = newmap();

User getUser(integerid)

{

User user =map.get(id)

if(user ==null)

{

user =session.get(id);

map.put(id,user);

}

return user;

}

}

Dao

{

Cache cache = null

setCache(Cachecache)

{

this.cache =cache

}

User getUser(int id)

{

if(cache!=null)

{

Useruser = cache.get(id);

if(user==null)

{

user= session.get(id);

cache.put(id,user);

}

returnuser;

}

returnsession.get(id);

}

}

锛�2锛塇ibernate鐨凷ession灏辨槸涓�绉嶇紦瀛橈紝鎴戜滑閫氬父灏嗕箣绉颁负Hibernate鐨勪竴绾х紦瀛橈紝褰撴兂浣跨敤session浠庢暟鎹簱涓煡璇㈠嚭涓�涓璞℃椂锛孲ession涔熸槸鍏堜粠鑷繁鍐呴儴鏌ョ湅鏄惁瀛樺湪杩欎釜瀵硅薄锛屽瓨鍦ㄥ垯鐩存帴杩斿洖锛屼笉瀛樺湪鎵嶅幓璁块棶鏁版嵁搴擄紝骞跺皢鏌ヨ鐨勭粨鏋滀繚瀛樺湪鑷繁鍐呴儴銆傜敱浜嶴ession浠h〃涓�娆′細璇濊繃绋嬶紝涓�涓猄ession涓庝竴涓暟鎹簱杩炴帴鐩稿叧杩烇紝鎵�浠ession鏈�濂戒笉瑕侀暱鏃堕棿淇濇寔鎵撳紑锛岄�氬父浠呯敤浜庝竴涓簨鍔″綋涓紝鍦ㄤ簨鍔$粨鏉熸椂灏卞簲鍏抽棴銆傚苟涓擲ession鏄嚎绋嬩笉瀹夊叏鐨勶紝琚涓嚎绋嬪叡浜椂瀹规槗鍑虹幇闂銆傞�氬父鍙湁閭g鍏ㄥ眬鎰忎箟涓婄殑缂撳瓨鎵嶆槸鐪熸鐨勭紦瀛樺簲鐢紝鎵嶆湁杈冨ぇ鐨勭紦瀛樹环鍊硷紝鍥犳锛孒ibernate鐨凷ession杩欎竴绾х紦瀛樼殑缂撳瓨浣滅敤骞朵笉鏄庢樉锛屽簲鐢ㄤ环鍊间笉澶с�侶ibernate鐨勪簩绾х紦瀛樺氨鏄涓篐ibernate閰嶇疆涓�绉嶅叏灞�缂撳瓨锛岃澶氫釜绾跨▼鍜屽涓簨鍔¢兘鍙互鍏变韩杩欎釜缂撳瓨銆傛垜浠笇鏈涚殑鏄竴涓汉浣跨敤杩囷紝鍏朵粬浜轰篃鍙互浣跨敤锛宻ession娌℃湁杩欑鏁堟灉銆�

锛�3锛変簩绾х紦瀛樻槸鐙珛浜嶩ibernate鐨勮蒋浠堕儴浠讹紝灞炰簬绗笁鏂圭殑浜у搧锛屽涓巶鍟嗗拰缁勭粐閮芥彁渚涙湁缂撳瓨浜у搧锛屼緥濡傦紝EHCache鍜孫SCache绛夌瓑銆傚湪Hibernate涓娇鐢ㄤ簩绾х紦瀛橈紝棣栧厛灏辫鍦╤ibernate.cfg.xml閰嶇疆鏂囦欢涓厤缃娇鐢ㄥ摢涓巶瀹剁殑缂撳瓨浜у搧锛屾帴鐫�闇�瑕侀厤缃缂撳瓨浜у搧鑷繁鐨勯厤缃枃浠讹紝鏈�鍚庤閰嶇疆Hibernate涓殑鍝簺瀹炰綋瀵硅薄瑕佺撼鍏ュ埌浜岀骇缂撳瓨鐨勭鐞嗕腑銆傛槑鐧戒簡浜岀骇缂撳瓨鍘熺悊鍜屾湁浜嗚繖涓�濊矾鍚庯紝寰堝鏄撻厤缃捣Hibernate鐨勪簩绾х紦瀛樸�傛墿灞曠煡璇嗭細涓�涓猄essionFactory鍙互鍏宠仈涓�涓簩绾х紦瀛橈紝涔熷嵆涓�涓簩绾х紦瀛樺彧鑳借礋璐g紦瀛樹竴涓暟鎹簱涓殑鏁版嵁锛屽綋浣跨敤Hibernate鐨勪簩绾х紦瀛樺悗锛屾敞鎰忎笉瑕佹湁鍏朵粬鐨勫簲鐢ㄦ垨SessionFactory鏉ユ洿鏀瑰綋鍓嶆暟鎹簱涓殑鏁版嵁锛岃繖鏍风紦瀛樼殑鏁版嵁灏变細涓庢暟鎹簱涓殑瀹為檯鏁版嵁涓嶄竴鑷淬��

18銆丼pring鐨勪緷璧栨敞鍏ユ槸浠�涔堟剰鎬�?缁欎竴涓� Bean 鐨� message灞炴��,瀛楃涓茬被鍨�,娉ㄥ叆鍊间负"Hello"鐨� XML閰嶇疆鏂囦欢璇ユ�庝箞鍐�?

19銆丣do鏄粈涔�?

JDO鏄疛ava瀵硅薄鎸佷箙鍖栫殑鏂扮殑瑙勮寖锛屼负java data object鐨勭畝绉�,涔熸槸涓�涓敤浜庡瓨鍙栨煇绉嶆暟鎹粨搴撲腑鐨勫璞$殑鏍囧噯鍖朅PI銆侸DO鎻愪緵浜嗛�忔槑鐨勫璞″瓨鍌紝鍥犳瀵瑰紑鍙戜汉鍛樻潵璇达紝瀛樺偍鏁版嵁瀵硅薄瀹屽叏涓嶉渶瑕侀澶栫殑浠g爜锛堝JDBC API鐨勪娇鐢級銆傝繖浜涚箒鐞愮殑渚嬭宸ヤ綔宸茬粡杞Щ鍒癑DO浜у搧鎻愪緵鍟嗚韩涓婏紝浣垮紑鍙戜汉鍛樿В鑴卞嚭鏉ワ紝浠庤�岄泦涓椂闂村拰绮惧姏鍦ㄤ笟鍔¢�昏緫涓娿�傚彟澶栵紝JDO寰堢伒娲伙紝鍥犱负瀹冨彲浠ュ湪浠讳綍鏁版嵁搴曞眰涓婅繍琛屻�侸DBC鍙槸闈㈠悜鍏崇郴鏁版嵁搴擄紙RDBMS锛塉DO鏇撮�氱敤锛屾彁渚涘埌浠讳綍鏁版嵁搴曞眰鐨勫瓨鍌ㄥ姛鑳斤紝姣斿鍏崇郴鏁版嵁搴撱�佹枃浠躲�乆ML浠ュ強瀵硅薄鏁版嵁搴擄紙ODBMS锛夌瓑绛夛紝浣垮緱搴旂敤鍙Щ妞嶆�ф洿寮恒��

20銆佷粈涔堟槸spring鐨処OCAOP

21銆丼TRUTS鐨勫伐浣滄祦绋嬶紒

22銆乻pring涓嶦JB鐨勫尯鍒紒锛�

鍏�.杞欢宸ョ▼涓庤璁℃ā寮�

1銆乁ML鏂归潰

鏍囧噯寤烘ā璇█UML銆傜敤渚嬪浘,闈欐�佸浘(鍖呮嫭绫诲浘銆佸璞″浘鍜屽寘鍥�),琛屼负鍥�,浜や簰鍥�(椤哄簭鍥�,鍚堜綔鍥�),瀹炵幇鍥俱��

2銆乯2ee甯哥敤鐨勮璁℃ā寮忥紵璇存槑宸ュ巶妯″紡銆�

鎬诲叡23绉嶏紝鍒嗕负涓夊ぇ绫伙細鍒涘缓鍨嬶紝缁撴瀯鍨嬶紝琛屼负鍨�

鎴戝彧璁板緱鍏朵腑甯哥敤鐨�6銆�7绉嶏紝鍒嗗埆鏄細

鍒涘缓鍨嬶紙宸ュ巶銆佸伐鍘傛柟娉曘�佹娊璞″伐鍘傘�佸崟渚嬶級

缁撴瀯鍨嬶紙鍖呰銆侀�傞厤鍣紝缁勫悎锛屼唬鐞嗭級

琛屼负锛堣瀵熻�咃紝妯$増锛岀瓥鐣ワ級

鐒跺悗鍐嶉拡瀵逛綘鐔熸倝鐨勬ā寮忚皥璋堜綘鐨勭悊瑙e嵆鍙��

Java涓殑23绉嶈璁℃ā寮忥細

Factory锛堝伐鍘傛ā寮忥級锛孊uilder锛堝缓閫犳ā寮忥級锛孎actory Method锛堝伐鍘傛柟娉曟ā寮忥級锛�

Prototype锛堝師濮嬫ā鍨嬫ā寮忥級锛孲ingleton锛堝崟渚嬫ā寮忥級锛孎acade锛堥棬闈㈡ā寮忥級锛�

Adapter锛堥�傞厤鍣ㄦā寮忥級锛孊ridge锛堟ˉ姊佹ā寮忥級锛孋omposite锛堝悎鎴愭ā寮忥級锛�

Decorator锛堣楗版ā寮忥級锛孎lyweight锛堜韩鍏冩ā寮忥級锛孭roxy锛堜唬鐞嗘ā寮忥級锛�

Command锛堝懡浠ゆā寮忥級锛孖nterpreter锛堣В閲婂櫒妯″紡锛夛紝 Visitor锛堣闂�呮ā寮忥級锛�

Iterator锛堣凯浠e瓙妯″紡锛夛紝Mediator锛堣皟鍋滆�呮ā寮忥級锛孧emento锛堝蹇樺綍妯″紡锛夛紝

Observer锛堣瀵熻�呮ā寮忥級锛孲tate锛堢姸鎬佹ā寮忥級锛孲trategy锛堢瓥鐣ユā寮忥級锛�

Template Method锛堟ā鏉挎柟娉曟ā寮忥級锛� Chain Of Responsibleity锛堣矗浠婚摼妯″紡锛�

宸ュ巶妯″紡锛氬伐鍘傛ā寮忔槸涓�绉嶇粡甯歌浣跨敤鍒扮殑妯″紡锛屾牴鎹伐鍘傛ā寮忓疄鐜扮殑绫诲彲浠ユ牴鎹彁渚涚殑鏁版嵁鐢熸垚涓�缁勭被涓煇涓�涓被鐨勫疄渚嬶紝閫氬父杩欎竴缁勭被鏈変竴涓叕鍏辩殑鎶借薄鐖剁被骞朵笖瀹炵幇浜嗙浉鍚岀殑鏂规硶锛屼絾鏄繖浜涙柟娉曢拡瀵逛笉鍚岀殑鏁版嵁杩涜浜嗕笉鍚岀殑鎿嶄綔銆傞鍏堥渶瑕佸畾涔変竴涓熀绫伙紝璇ョ被鐨勫瓙绫婚�氳繃涓嶅悓鐨勬柟娉曞疄鐜颁簡鍩虹被涓殑鏂规硶銆傜劧鍚庨渶瑕佸畾涔変竴涓伐鍘傜被锛屽伐鍘傜被鍙互鏍规嵁鏉′欢鐢熸垚涓嶅悓鐨勫瓙绫诲疄渚嬨�傚綋寰楀埌瀛愮被鐨勫疄渚嬪悗锛屽紑鍙戜汉鍛樺彲浠ヨ皟鐢ㄥ熀绫讳腑鐨勬柟娉曡�屼笉蹇呰�冭檻鍒板簳杩斿洖鐨勬槸鍝竴涓瓙绫荤殑瀹炰緥銆�

3銆佸紑鍙戜腑閮界敤鍒颁簡閭d簺璁捐妯″紡?鐢ㄥ湪浠�涔堝満鍚�?

姣忎釜妯″紡閮芥弿杩颁簡涓�涓湪鎴戜滑鐨勭幆澧冧腑涓嶆柇鍑虹幇鐨勯棶棰橈紝鐒跺悗鎻忚堪浜嗚闂鐨勮В鍐虫柟妗堢殑鏍稿績銆傞�氳繃杩欑鏂瑰紡锛屼綘鍙互鏃犳暟娆″湴浣跨敤閭d簺宸叉湁鐨勮В鍐虫柟妗堬紝鏃犻渶鍦ㄩ噸澶嶇浉鍚岀殑宸ヤ綔銆備富瑕佺敤鍒颁簡MVC鐨勮璁℃ā寮忋�傜敤鏉ュ紑鍙慗SP/Servlet鎴栬�匤2EE鐨勭浉鍏冲簲鐢ㄣ�傜畝鍗曞伐鍘傛ā寮忕瓑銆�

涔�. j2ee閮ㄥ垎

1銆丅S涓嶤S鐨勮仈绯讳笌鍖哄埆銆�

C/S鏄疌lient/Server鐨勭缉鍐欍�傛湇鍔″櫒閫氬父閲囩敤楂樻�ц兘鐨凱C銆佸伐浣滅珯鎴栧皬鍨嬫満锛屽苟閲囩敤澶у瀷鏁版嵁搴撶郴缁燂紝濡侽racle銆丼ybase銆両nFORMix鎴� SQL Server銆傚鎴风闇�瑕佸畨瑁呬笓鐢ㄧ殑瀹㈡埛绔蒋浠躲��

B/锛虫槸Brower/Server鐨勭缉鍐欙紝瀹㈡埛鏈轰笂鍙瀹夎涓�涓祻瑙堝櫒锛圔rowser锛夛紝濡侼etscape Navigator鎴朓nternet Explorer锛屾湇鍔″櫒瀹夎Oracle銆丼ybase銆両nFORMix鎴� SQL Server绛夋暟鎹簱銆傚湪杩欑缁撴瀯涓嬶紝鐢ㄦ埛鐣岄潰瀹屽叏閫氳繃WWW娴忚鍣ㄥ疄鐜帮紝涓�閮ㄥ垎浜嬪姟閫昏緫鍦ㄥ墠绔疄鐜帮紝浣嗘槸涓昏浜嬪姟閫昏緫鍦ㄦ湇鍔″櫒绔疄鐜般�傛祻瑙堝櫒閫氳繃锛積b Server鍚屾暟鎹簱杩涜鏁版嵁浜や簰銆�

C/S 涓� B/S鍖哄埆锛�

锛戯紟纭欢鐜涓嶅悓:

C/S 涓�鑸缓绔嬪湪涓撶敤鐨勭綉缁滀笂,灏忚寖鍥撮噷鐨勭綉缁滅幆澧�,灞�鍩熺綉涔嬮棿鍐嶉�氳繃涓撻棬鏈嶅姟鍣ㄦ彁渚涜繛鎺ュ拰鏁版嵁浜ゆ崲鏈嶅姟.

B/S 寤虹珛鍦ㄥ箍鍩熺綉涔嬩笂鐨�,涓嶅繀鏄笓闂ㄧ殑缃戠粶纭欢鐜,渚嬩笌鐢佃瘽涓婄綉,绉熺敤璁惧.淇℃伅鑷繁绠$悊.鏈夋瘮C/S鏇村己鐨勯�傚簲鑼冨洿,涓�鑸彧瑕佹湁鎿嶄綔绯荤粺鍜屾祻瑙堝櫒灏辫

锛掞紟瀵瑰畨鍏ㄨ姹備笉鍚�

C/S 涓�鑸潰鍚戠浉瀵瑰浐瀹氱殑鐢ㄦ埛缇�,瀵逛俊鎭畨鍏ㄧ殑鎺у埗鑳藉姏寰堝己.涓�鑸珮搴︽満瀵嗙殑淇℃伅绯荤粺閲囩敤C/S缁撴瀯閫傚疁.鍙互閫氳繃B/S鍙戝竷閮ㄥ垎鍙叕寮�淇℃伅.

B/S 寤虹珛鍦ㄥ箍鍩熺綉涔嬩笂,瀵瑰畨鍏ㄧ殑鎺у埗鑳藉姏鐩稿寮�,鍙兘闈㈠悜涓嶅彲鐭ョ殑鐢ㄦ埛銆�

锛擄紟瀵圭▼搴忔灦鏋勪笉鍚�

C/S 绋嬪簭鍙互鏇村姞娉ㄩ噸娴佺▼,鍙互瀵规潈闄愬灞傛鏍¢獙,瀵圭郴缁熻繍琛岄�熷害鍙互杈冨皯鑰冭檻.

B/S 瀵瑰畨鍏ㄤ互鍙婅闂�熷害鐨勫閲嶇殑鑰冭檻,寤虹珛鍦ㄩ渶瑕佹洿鍔犱紭鍖栫殑鍩虹涔嬩笂.姣擟/S鏈夋洿楂樼殑瑕佹眰 B/S缁撴瀯鐨勭▼搴忔灦鏋勬槸鍙戝睍鐨勮秼鍔�,浠嶮S鐨�.Net绯诲垪鐨凚izTalk 2000Exchange 2000绛�,鍏ㄩ潰鏀寔缃戠粶鐨勬瀯浠舵惌寤虹殑绯荤粺. SUN鍜孖BM鎺ㄧ殑JavaBean鏋勪欢鎶�鏈瓑,浣� B/S鏇村姞鎴愮啛.

锛旓紟杞欢閲嶇敤涓嶅悓

C/S 绋嬪簭鍙互涓嶅彲閬垮厤鐨勬暣浣撴�ц�冭檻,鏋勪欢鐨勯噸鐢ㄦ�т笉濡傚湪B/S瑕佹眰涓嬬殑鏋勪欢鐨勯噸鐢ㄦ�уソ.

B/S 瀵圭殑澶氶噸缁撴瀯,瑕佹眰鏋勪欢鐩稿鐙珛鐨勫姛鑳�.鑳藉鐩稿杈冨ソ鐨勯噸鐢�.灏卞叆涔版潵鐨勯妗屽彲浠ュ啀鍒╃敤,鑰屼笉鏄仛鍦ㄥ涓婄殑鐭冲ご妗屽瓙

锛曪紟绯荤粺缁存姢涓嶅悓

C/S 绋嬪簭鐢变簬鏁翠綋鎬�,蹇呴』鏁翠綋鑰冨療,澶勭悊鍑虹幇鐨勯棶棰樹互鍙婄郴缁熷崌绾�.鍗囩骇闅�.鍙兘鏄啀鍋氫竴涓叏鏂扮殑绯荤粺

B/S 鏋勪欢缁勬垚,鏂归潰鏋勪欢涓埆鐨勬洿鎹�,瀹炵幇绯荤粺鐨勬棤缂濆崌绾�.绯荤粺缁存姢寮�閿�鍑忓埌鏈�灏�.鐢ㄦ埛浠庣綉涓婅嚜宸变笅杞藉畨瑁呭氨鍙互瀹炵幇鍗囩骇.

锛栵紟澶勭悊闂涓嶅悓

C/S 绋嬪簭鍙互澶勭悊鐢ㄦ埛闈㈠浐瀹�,骞朵笖鍦ㄧ浉鍚屽尯鍩�,瀹夊叏瑕佹眰楂橀渶姹�,涓庢搷浣滅郴缁熺浉鍏�.搴旇閮芥槸鐩稿悓鐨勭郴缁�

B/S 寤虹珛鍦ㄥ箍鍩熺綉涓�,闈㈠悜涓嶅悓鐨勭敤鎴风兢,鍒嗘暎鍦板煙,杩欐槸C/S鏃犳硶浣滃埌鐨�.涓庢搷浣滅郴缁熷钩鍙板叧绯绘渶灏�.

锛楋紟鐢ㄦ埛鎺ュ彛涓嶅悓

C/S 澶氭槸寤虹珛鐨刉indow骞冲彴涓�,琛ㄧ幇鏂规硶鏈夐檺,瀵圭▼搴忓憳鏅亶瑕佹眰杈冮珮

B/S 寤虹珛鍦ㄦ祻瑙堝櫒涓�,鏈夋洿鍔犱赴瀵屽拰鐢熷姩鐨勮〃鐜版柟寮忎笌鐢ㄦ埛浜ゆ祦.骞朵笖澶ч儴鍒嗛毦搴﹀噺浣�,鍑忎綆寮�鍙戞垚鏈�.

锛橈紟淇℃伅娴佷笉鍚�

C/S 绋嬪簭涓�鑸槸鍏稿瀷鐨勪腑澶泦鏉冪殑鏈烘寮忓鐞�,浜や簰鎬х浉瀵逛綆

B/S 淇℃伅娴佸悜鍙彉鍖�, B-B B-C B-G绛変俊鎭�佹祦鍚戠殑鍙樺寲,鏇村儚浜ゆ槗涓績銆�

2銆佸簲鐢ㄦ湇鍔″櫒涓嶹EB SERVER鐨勫尯鍒紵

搴旂敤鏈嶅姟鍣細Weblogic銆乀omcat銆丣boss

WEB SERVER锛欼IS銆� Apache

3銆佸簲鐢ㄦ湇鍔″櫒鏈夐偅浜涳紵

BEA WebLogic Server锛孖BM WebSphere Application Server锛孫racle9i ApplicationServer锛宩Boss锛孴omcat

4銆丣2EE鏄粈涔堬紵

绛�:Je22鏄疭un鍏徃鎻愬嚭鐨勫灞�(multi-diered),鍒嗗竷寮�(distributed),鍩轰簬缁勪欢(component-base)鐨勪紒涓氱骇搴旂敤妯″瀷(enterpriese applicationmodel).鍦ㄨ繖鏍风殑涓�涓簲鐢ㄧ郴缁熶腑锛屽彲鎸夌収鍔熻兘鍒掑垎涓轰笉鍚岀殑缁勪欢锛岃繖浜涚粍浠跺張鍙湪涓嶅悓璁$畻鏈轰笂锛屽苟涓斿浜庣浉搴旂殑灞傛(tier)涓�傛墍灞炲眰娆″寘鎷鎴峰眰(clietn tier)缁勪欢,web灞傚拰缁勪欢,Business灞傚拰缁勪欢,浼佷笟淇℃伅绯荤粺(EIS)灞傘��

涓�涓彟绫荤殑鍥炵瓟锛歫2ee灏辨槸澧炲垹鏀规煡銆�

5銆丣2EE鏄妧鏈繕鏄钩鍙拌繕鏄鏋讹紵浠�涔堟槸J2EE

J2EE鏈韩鏄竴涓爣鍑嗭紝涓�涓负浼佷笟鍒嗗竷寮忓簲鐢ㄧ殑寮�鍙戞彁渚涚殑鏍囧噯骞冲彴銆�

J2EE涔熸槸涓�涓鏋讹紝鍖呮嫭JDBC銆丣NDI銆丷MI銆丣MS銆丒JB銆丣TA绛夋妧鏈��

6銆佽瀵逛互涓嬪湪J2EE涓父鐢ㄧ殑鍚嶈瘝杩涜瑙i噴(鎴栫畝鍗曟弿杩�)

web瀹瑰櫒锛氱粰澶勪簬鍏朵腑鐨勫簲鐢ㄧ▼搴忕粍浠讹紙JSP锛孲ERVLET锛夋彁渚涗竴涓幆澧冿紝浣縅SP,SERVLET鐩存帴鏇村鍣ㄤ腑鐨勭幆澧冨彉閲忔帴鍙d氦浜掞紝涓嶅繀鍏虫敞鍏跺畠绯荤粺闂銆備富瑕佹湁WEB鏈嶅姟鍣ㄦ潵瀹炵幇銆備緥濡傦細TOMCAT,WEBLOGIC,WEBSPHERE绛夈�傝瀹瑰櫒鎻愪緵鐨勬帴鍙d弗鏍奸伒瀹圝2EE瑙勮寖涓殑WEB APPLICATION 鏍囧噯銆傛垜浠妸閬靛畧浠ヤ笂鏍囧噯鐨刉EB鏈嶅姟鍣ㄥ氨鍙仛J2EE涓殑WEB瀹瑰櫒銆�

EJB瀹瑰櫒锛欵nterprise java bean瀹瑰櫒銆傛洿鍏锋湁琛屼笟棰嗗煙鐗硅壊銆備粬鎻愪緵缁欒繍琛屽湪鍏朵腑鐨勭粍浠禘JB鍚勭绠$悊鍔熻兘銆傚彧瑕佹弧瓒矹2EE瑙勮寖鐨凟JB鏀惧叆璇ュ鍣紝椹笂灏变細琚鍣ㄨ繘琛岄珮鏁堢巼鐨勭鐞嗐�傚苟涓斿彲浠ラ�氳繃鐜版垚鐨勬帴鍙f潵鑾峰緱绯荤粺绾у埆鐨勬湇鍔°�備緥濡傞偖浠舵湇鍔°�佷簨鍔$鐞嗐��

JNDI锛氾紙Java Naming & Directory Interface锛塉AVA鍛藉悕鐩綍鏈嶅姟銆備富瑕佹彁渚涚殑鍔熻兘鏄細鎻愪緵涓�涓洰褰曠郴缁燂紝璁╁叾瀹冨悇鍦扮殑搴旂敤绋嬪簭鍦ㄥ叾涓婇潰鐣欎笅鑷繁鐨勭储寮曪紝浠庤�屾弧瓒冲揩閫熸煡鎵惧拰瀹氫綅鍒嗗竷寮忓簲鐢ㄧ▼搴忕殑鍔熻兘銆�

JMS锛氾紙Java Message Service锛塉AVA娑堟伅鏈嶅姟銆備富瑕佸疄鐜板悇涓簲鐢ㄧ▼搴忎箣闂寸殑閫氳銆傚寘鎷偣瀵圭偣鍜屽箍鎾��

JTA锛氾紙Java Transaction API锛塉AVA浜嬪姟鏈嶅姟銆傛彁渚涘悇绉嶅垎甯冨紡浜嬪姟鏈嶅姟銆傚簲鐢ㄧ▼搴忓彧闇�璋冪敤鍏舵彁渚涚殑鎺ュ彛鍗冲彲銆�

JAF锛氾紙Java Action FrameWork锛塉AVA瀹夊叏璁よ瘉妗嗘灦銆傛彁渚涗竴浜涘畨鍏ㄦ帶鍒舵柟闈㈢殑妗嗘灦銆傝寮�鍙戣�呴�氳繃鍚勭閮ㄧ讲鍜岃嚜瀹氫箟瀹炵幇鑷繁鐨勪釜鎬у畨鍏ㄦ帶鍒剁瓥鐣ャ��

RMI/IIOP:锛圧emote Method Invocation /internet瀵硅薄璇锋眰涓粙鍗忚锛変粬浠富瑕佺敤浜庨�氳繃杩滅▼璋冪敤鏈嶅姟銆備緥濡傦紝杩滅▼鏈変竴鍙拌绠楁満涓婅繍琛屼竴涓▼搴忥紝瀹冩彁渚涜偂绁ㄥ垎鏋愭湇鍔★紝鎴戜滑鍙互鍦ㄦ湰鍦拌绠楁満涓婂疄鐜板鍏剁洿鎺ヨ皟鐢ㄣ�傚綋鐒惰繖鏄閫氳繃涓�瀹氱殑瑙勮寖鎵嶈兘鍦ㄥ紓鏋勭殑绯荤粺涔嬮棿杩涜閫氫俊銆俁MI鏄疛AVA鐗规湁鐨勩��

7銆佸浣曠粰weblogic鎸囧畾澶у皬鐨勫唴瀛�?

锛堣繖涓棶棰樹笉浣滃叿浣撳洖绛旓紝鍒楀嚭鏉ュ彧鏄憡璇夎鑰呭彲鑳戒細閬囧埌浠�涔堥棶棰橈紝浣犱笉闇�瑕侀潰闈勘鍒帮紝浠�涔堥兘绮鹃�氥�傦級

鍦ㄥ惎鍔╓eblogic鐨勮剼鏈腑锛堜綅浜庢墍鍦―omian瀵瑰簲鏈嶅姟鍣ㄧ洰褰曚笅鐨剆tartServerName锛夛紝澧炲姞set MEM_ARGS=-Xms32m-Xmx200m锛屽彲浠ヨ皟鏁存渶灏忓唴瀛樹负32M锛屾渶澶�200M

8銆佸浣曡瀹氱殑weblogic鐨勭儹鍚姩妯″紡(寮�鍙戞ā寮�)涓庝骇鍝佸彂甯冩ā寮�?

鍙互鍦ㄧ鐞嗘帶鍒跺彴涓慨鏀瑰搴旀湇鍔″櫒鐨勫惎鍔ㄦā寮忎负寮�鍙戞垨浜у搧妯″紡涔嬩竴銆傛垨鑰呬慨鏀规湇鍔$殑鍚姩鏂囦欢鎴栬�卌ommenv鏂囦欢锛屽鍔爏etPRODUCTION_MODE=true銆�

9銆佸浣曞惎鍔ㄦ椂涓嶉渶杈撳叆鐢ㄦ埛鍚嶄笌瀵嗙爜?

淇敼鏈嶅姟鍚姩鏂囦欢锛屽鍔� WLS_USER鍜學LS_PW椤广�備篃鍙互鍦╞oot.properties鏂囦欢涓鍔犲姞瀵嗚繃鐨勭敤鎴峰悕鍜屽瘑鐮�.

10銆佸湪weblogic绠$悊鍒跺彴涓涓�涓簲鐢ㄥ煙(鎴栬�呰鏄竴涓綉绔�,Domain)杩涜jms鍙奺jb鎴栬繛鎺ユ睜绛夌浉鍏充俊鎭繘琛岄厤缃悗,瀹為檯淇濆瓨鍦ㄤ粈涔堟枃浠朵腑?

淇濆瓨鍦ㄦDomain鐨刢onfig.xml鏂囦欢涓紝瀹冩槸鏈嶅姟鍣ㄧ殑鏍稿績閰嶇疆鏂囦欢銆�

11銆佽璇磜eblogic涓竴涓狣omain鐨勭己鐪佺洰褰曠粨鏋�?姣斿瑕佸皢涓�涓畝鍗曠殑helloWorld.jsp鏀惧叆浣曠洰褰曚笅,鐒剁殑鍦ㄦ祻瑙堝櫒涓婂氨鍙墦鍏ttp://涓绘満:绔彛鍙�//helloword.jsp灏卞彲浠ョ湅鍒拌繍琛岀粨鏋滀簡?鍙堟瘮濡傝繖鍏朵腑鐢ㄥ埌浜嗕竴涓嚜宸卞啓鐨刯avaBean璇ュ浣曞姙?

Domain鐩綍鏈嶅姟鍣ㄧ洰褰昦pplications锛屽皢搴旂敤鐩綍鏀惧湪姝ょ洰褰曚笅灏嗗彲浠ヤ綔涓哄簲鐢ㄨ闂紝濡傛灉鏄疻eb搴旂敤锛屽簲鐢ㄧ洰褰曢渶瑕佹弧瓒砏eb搴旂敤鐩綍瑕佹眰锛宩sp鏂囦欢鍙互鐩存帴鏀惧湪搴旂敤鐩綍涓紝Javabean闇�瑕佹斁鍦ㄥ簲鐢ㄧ洰褰曠殑WEB-INF鐩綍鐨刢lasses鐩綍涓紝璁剧疆鏈嶅姟鍣ㄧ殑缂虹渷搴旂敤灏嗗彲浠ュ疄鐜板湪娴忚鍣ㄤ笂鏃犻渶杈撳叆搴旂敤鍚嶃��

12銆佸湪weblogic涓彂甯僥jb闇�娑夊強鍒板摢浜涢厤缃枃浠�

涓嶅悓绫诲瀷鐨凟JB娑夊強鐨勯厤缃枃浠朵笉鍚岋紝閮芥秹鍙婂埌鐨勯厤缃枃浠跺寘鎷琫jb-jar.xml,weblogic-ejb-jar.xmlCMP瀹炰綋Bean涓�鑸繕闇�瑕亀eblogic-cmp-rdbms-jar.xml

13銆佸浣曞湪weblogic涓繘琛宻sl閰嶇疆涓庡鎴风鐨勮璇侀厤缃垨璇磋j2ee(鏍囧噯)杩涜ssl鐨勯厤缃�?

缂虹渷瀹夎涓娇鐢―emoIdentity.jks鍜孌emoTrust.jksKeyStore瀹炵幇SSL锛岄渶瑕侀厤缃湇鍔″櫒浣跨敤Enable SSL锛岄厤缃叾绔彛锛屽湪浜у搧妯″紡涓嬮渶瑕佷粠CA鑾峰彇绉佹湁瀵嗛挜鍜屾暟瀛楄瘉涔︼紝鍒涘缓identity鍜宼rust keystore锛岃杞借幏寰楃殑瀵嗛挜鍜屾暟瀛楄瘉涔︺�傚彲浠ラ厤缃SSL杩炴帴鏄崟鍚戣繕鏄弻鍚戠殑銆�

14銆佸浣曟煡鐪嬪湪weblogic涓凡缁忓彂甯冪殑EJB?

鍙互浣跨敤绠$悊鎺у埗鍙帮紝鍦ㄥ畠鐨凞eployment涓彲浠ユ煡鐪嬫墍鏈夊凡鍙戝竷鐨凟JB

鍗�. EBJ閮ㄥ垎

1銆丒JB鏄熀浜庡摢浜涙妧鏈疄鐜扮殑锛熷苟璇村嚭SessionBean鍜孍ntityBean鐨勫尯鍒紝StatefulBean鍜孲tatelessBean鐨勫尯鍒��

EJB鍖呮嫭Session Bean銆丒ntity Bean銆丮essage Driven Bean锛屽熀浜嶫NDI銆丷MI銆丣AT绛夋妧鏈疄鐜般��

SessionBean鍦↗2EE搴旂敤绋嬪簭涓鐢ㄦ潵瀹屾垚涓�浜涙湇鍔″櫒绔殑涓氬姟鎿嶄綔锛屼緥濡傝闂暟鎹簱銆佽皟鐢ㄥ叾浠朎JB缁勪欢銆侲ntityBean琚敤鏉ヤ唬琛ㄥ簲鐢ㄧ郴缁熶腑鐢ㄥ埌鐨勬暟鎹��

瀵逛簬瀹㈡埛鏈猴紝SessionBean鏄竴绉嶉潪鎸佷箙鎬у璞★紝瀹冨疄鐜版煇浜涘湪鏈嶅姟鍣ㄤ笂杩愯鐨勪笟鍔¢�昏緫銆�

瀵逛簬瀹㈡埛鏈猴紝EntityBean鏄竴绉嶆寔涔呮�у璞★紝瀹冧唬琛ㄤ竴涓瓨鍌ㄥ湪鎸佷箙鎬у瓨鍌ㄥ櫒涓殑瀹炰綋鐨勫璞¤鍥撅紝鎴栨槸涓�涓敱鐜版湁浼佷笟搴旂敤绋嬪簭瀹炵幇鐨勫疄浣撱��

Session Bean 杩樺彲浠ュ啀缁嗗垎涓� Stateful Session Bean涓� Stateless SessionBean锛岃繖涓ょ鐨� Session Bean閮藉彲浠ュ皢绯荤粺閫昏緫鏀惧湪 method涔嬩腑鎵ц锛屼笉鍚岀殑鏄� Stateful SessionBean鍙互璁板綍鍛煎彨鑰呯殑鐘舵�侊紝鍥犳閫氬父鏉ヨ锛屼竴涓娇鐢ㄨ�呬細鏈変竴涓浉瀵瑰簲鐨� Stateful SessionBean鐨勫疄浣撱�係tateless SessionBean铏界劧涔熸槸閫昏緫缁勪欢锛屼絾鏄粬鍗翠笉璐熻矗璁板綍浣跨敤鑰呯姸鎬侊紝涔熷氨鏄褰撲娇鐢ㄨ�呭懠鍙� Stateless SessionBean鐨勬椂鍊欙紝EJB Container骞朵笉浼氭壘瀵荤壒瀹氱殑 Stateless SessionBean鐨勫疄浣撴潵鎵ц杩欎釜 method銆傛崲瑷�涔嬶紝寰堝彲鑳芥暟涓娇鐢ㄨ�呭湪鎵ц鏌愪釜 Stateless SessionBean鐨� methods鏃讹紝浼氭槸鍚屼竴涓� Bean鐨� Instance鍦ㄦ墽琛屻�備粠鍐呭瓨鏂归潰鏉ョ湅锛� Stateful SessionBean涓嶴tateless SessionBean姣旇緝锛� Stateful SessionBean浼氭秷鑰� J2EE Server杈冨鐨勫唴瀛橈紝鐒惰�� Stateful SessionBean鐨勪紭鍔垮嵈鍦ㄤ簬浠栧彲浠ョ淮鎸佷娇鐢ㄨ�呯殑鐘舵�併��

2銆佺畝瑕佽涓�涓� EJB鐨� 7涓� Transaction Level?

3銆丒JB涓嶫AVA BEAN鐨勫尯鍒紵

Java Bean 鏄彲澶嶇敤鐨勭粍浠讹紝瀵笿ava Bean骞舵病鏈変弗鏍肩殑瑙勮寖锛岀悊璁轰笂璁诧紝浠讳綍涓�涓狫ava绫婚兘鍙互鏄竴涓狟ean銆備絾閫氬父鎯呭喌涓嬶紝鐢变簬Java Bean鏄瀹瑰櫒鎵�鍒涘缓锛堝Tomcat锛夌殑锛屾墍浠ava Bean搴斿叿鏈変竴涓棤鍙傜殑鏋勯�犲櫒锛屽彟澶栵紝閫氬父Java Bean杩樿瀹炵幇Serializable鎺ュ彛鐢ㄤ簬瀹炵幇Bean鐨勬寔涔呮�с�侸ava Bean瀹為檯涓婄浉褰撲簬寰蒋COM妯″瀷涓殑鏈湴杩涚▼鍐匔OM缁勪欢锛屽畠鏄笉鑳借璺ㄨ繘绋嬭闂殑銆侲nterprise Java Bean鐩稿綋浜嶥COM锛屽嵆鍒嗗竷寮忕粍浠躲�傚畠鏄熀浜嶫ava鐨勮繙绋嬫柟娉曡皟鐢紙RMI锛夋妧鏈殑锛屾墍浠JB鍙互琚繙绋嬭闂紙璺ㄨ繘绋嬨�佽法璁$畻鏈猴級銆備絾EJB蹇呴』琚竷缃插湪璇稿Webspere銆乄ebLogic杩欐牱鐨勫鍣ㄤ腑锛孍JB瀹㈡埛浠庝笉鐩存帴璁块棶鐪熸鐨凟JB缁勪欢锛岃�屾槸閫氳繃鍏跺鍣ㄨ闂�侲JB瀹瑰櫒鏄疎JB缁勪欢鐨勪唬鐞嗭紝EJB缁勪欢鐢卞鍣ㄦ墍鍒涘缓鍜岀鐞嗐�傚鎴烽�氳繃瀹瑰櫒鏉ヨ闂湡姝g殑EJB缁勪欢銆�

4銆丒JB鍖呮嫭锛圫essionBean,EntityBean锛夎鍑轰粬浠殑鐢熷懡鍛ㄦ湡锛屽強濡備綍绠$悊浜嬪姟鐨勶紵

SessionBean锛歋tateless Session Bean鐨勭敓鍛藉懆鏈熸槸鐢卞鍣ㄥ喅瀹氱殑锛屽綋瀹㈡埛鏈哄彂鍑鸿姹傝寤虹珛涓�涓狟ean鐨勫疄渚嬫椂锛孍JB瀹瑰櫒涓嶄竴瀹氳鍒涘缓涓�涓柊鐨凚ean鐨勫疄渚嬩緵瀹㈡埛鏈鸿皟鐢紝鑰屾槸闅忎究鎵句竴涓幇鏈夌殑瀹炰緥鎻愪緵缁欏鎴锋満銆傚綋瀹㈡埛鏈虹涓�娆¤皟鐢ㄤ竴涓猄tateful SessionBean 鏃讹紝瀹瑰櫒蹇呴』绔嬪嵆鍦ㄦ湇鍔″櫒涓垱寤轰竴涓柊鐨凚ean瀹炰緥锛屽苟鍏宠仈鍒板鎴锋満涓婏紝浠ュ悗姝ゅ鎴锋満璋冪敤Stateful SessionBean鐨勬柟娉曟椂瀹瑰櫒浼氭妸璋冪敤鍒嗘淳鍒颁笌姝ゅ鎴锋満鐩稿叧鑱旂殑Bean瀹炰緥銆�

EntityBean锛欵ntity Beans鑳藉瓨娲荤浉瀵硅緝闀跨殑鏃堕棿锛屽苟涓旂姸鎬佹槸鎸佺画鐨勩�傚彧瑕佹暟鎹簱涓殑鏁版嵁瀛樺湪锛孍ntity beans灏变竴鐩村瓨娲汇�傝�屼笉鏄寜鐓у簲鐢ㄧ▼搴忔垨鑰呮湇鍔¤繘绋嬫潵璇寸殑銆傚嵆浣縀JB瀹瑰櫒宕╂簝浜嗭紝Entity beans涔熸槸瀛樻椿鐨勩�侲ntity Beans鐢熷懡鍛ㄦ湡鑳藉琚鍣ㄦ垨鑰� Beans鑷繁绠$悊銆�

EJB閫氳繃浠ヤ笅鎶�鏈鐞嗗疄鍔★細瀵硅薄绠$悊缁勭粐锛圤MG锛夌殑瀵硅薄瀹炲姟鏈嶅姟锛圤TS锛夛紝Sun Microsystems鐨凾ransaction Service锛圝TS锛夈�丣ava Transaction API锛圝TA锛夛紝寮�鍙戠粍锛圶/Open锛夌殑XA鎺ュ彛銆�

5銆丒JB瀹瑰櫒鎻愪緵鐨勬湇鍔�

涓昏鎻愪緵澹版槑鍛ㄦ湡绠$悊銆佷唬鐮佷骇鐢熴�佹寔缁�х鐞嗐�佸畨鍏ㄣ�佷簨鍔$鐞嗐�侀攣鍜屽苟鍙戣绠$悊绛夋湇鍔°��

6銆丒JB鐨勬縺娲绘満鍒�

浠tateful Session Bean涓轰緥锛氬叾Cache澶у皬鍐冲畾浜嗗唴瀛樹腑鍙互鍚屾椂瀛樺湪鐨凚ean瀹炰緥鐨勬暟閲忥紝鏍规嵁MRU鎴朜RU绠楁硶锛屽疄渚嬪湪婵�娲诲拰鍘绘縺娲荤姸鎬佷箣闂磋縼绉伙紝婵�娲绘満鍒舵槸褰撳鎴风璋冪敤鏌愪釜EJB瀹炰緥涓氬姟鏂规硶鏃讹紝濡傛灉瀵瑰簲EJB Object鍙戠幇鑷繁娌℃湁缁戝畾瀵瑰簲鐨凚ean瀹炰緥鍒欎粠鍏跺幓婵�娲籅ean瀛樺偍涓紙閫氳繃搴忓垪鍖栨満鍒跺瓨鍌ㄥ疄渚嬶級鍥炲锛堟縺娲伙級姝ゅ疄渚嬨�傜姸鎬佸彉杩佸墠浼氳皟鐢ㄥ搴旂殑ejbActive鍜宔jbPassivate鏂规硶銆�

7銆丒JB鐨勫嚑绉嶇被鍨�

浼氳瘽锛圫ession锛塀ean锛屽疄浣擄紙Entity锛塀ean娑堟伅椹卞姩鐨勶紙Message Driven锛塀ean

浼氳瘽Bean鍙堝彲鍒嗕负鏈夌姸鎬侊紙Stateful锛夊拰鏃犵姸鎬侊紙Stateless锛変袱绉�

瀹炰綋Bean鍙垎涓築ean绠$悊鐨勬寔缁�э紙BMP锛夊拰瀹瑰櫒绠$悊鐨勬寔缁�э紙CMP锛変袱绉�

8銆佸鏈嶇璋冪敤EJB瀵硅薄鐨勫嚑涓熀鏈楠�

璁剧疆JNDI鏈嶅姟宸ュ巶浠ュ強JNDI鏈嶅姟鍦板潃绯荤粺灞炴�э紝鏌ユ壘Home鎺ュ彛锛屼粠Home鎺ュ彛璋冪敤Create鏂规硶鍒涘缓Remote鎺ュ彛锛岄�氳繃Remote鎺ュ彛璋冪敤鍏朵笟鍔℃柟娉曘��

鍗佷竴. webservice閮ㄥ垎

1銆乄EB SERVICE鍚嶈瘝瑙i噴銆侸SWDL寮�鍙戝寘鐨勪粙缁嶃�侸AXP銆丣AXM鐨勮В閲娿�係OAP銆乁DDI,WSDL瑙i噴銆�

Web ServiceWeb Service鏄熀浜庣綉缁滅殑銆佸垎甯冨紡鐨勬ā鍧楀寲缁勪欢锛屽畠鎵ц鐗瑰畾鐨勪换鍔★紝閬靛畧鍏蜂綋鐨勬妧鏈鑼冿紝杩欎簺瑙勮寖浣垮緱Web Service鑳戒笌鍏朵粬鍏煎鐨勭粍浠惰繘琛屼簰鎿嶄綔銆�

JAXP(Java API for XML Parsing) 瀹氫箟浜嗗湪Java涓娇鐢―OM, SAX, XSLT鐨勯�氱敤鐨勬帴鍙c�傝繖鏍峰湪浣犵殑绋嬪簭涓綘鍙浣跨敤杩欎簺閫氱敤鐨勬帴鍙o紝褰撲綘闇�瑕佹敼鍙樺叿浣撶殑瀹炵幇鏃跺�欎篃涓嶉渶瑕佷慨鏀逛唬鐮併��

JAXM(Java API for XML Messaging) 鏄负SOAP閫氫俊鎻愪緵璁块棶鏂规硶鍜屼紶杈撴満鍒剁殑API銆�

WSDL鏄竴绉� XML鏍煎紡锛岀敤浜庡皢缃戠粶鏈嶅姟鎻忚堪涓轰竴缁勭鐐癸紝杩欎簺绔偣瀵瑰寘鍚潰鍚戞枃妗d俊鎭垨闈㈠悜杩囩▼淇℃伅鐨勬秷鎭繘琛屾搷浣溿�傝繖绉嶆牸寮忛鍏堝鎿嶄綔鍜屾秷鎭繘琛屾娊璞℃弿杩帮紝鐒跺悗灏嗗叾缁戝畾鍒板叿浣撶殑缃戠粶鍗忚鍜屾秷鎭牸寮忎笂浠ュ畾涔夌鐐广�傜浉鍏崇殑鍏蜂綋绔偣鍗崇粍鍚堟垚涓烘娊璞$鐐癸紙鏈嶅姟锛夈��

SOAP鍗崇畝鍗曞璞¤闂崗璁�(Simple Object Access Protocol)锛屽畠鏄敤浜庝氦鎹ML缂栫爜淇℃伅鐨勮交閲忕骇鍗忚銆�

UDDI 鐨勭洰鐨勬槸涓虹數瀛愬晢鍔″缓绔嬫爣鍑嗭紱UDDI鏄竴濂楀熀浜嶹eb鐨勩�佸垎甯冨紡鐨勩�佷负Web Service鎻愪緵鐨勩�佷俊鎭敞鍐屼腑蹇冪殑瀹炵幇鏍囧噯瑙勮寖锛屽悓鏃朵篃鍖呭惈涓�缁勪娇浼佷笟鑳藉皢鑷韩鎻愪緵鐨刉eb Service娉ㄥ唽锛屼互浣垮埆鐨勪紒涓氳兘澶熷彂鐜扮殑璁块棶鍗忚鐨勫疄鐜版爣鍑嗐��

2銆丆ORBA鏄粈涔�?鐢ㄩ�旀槸浠�涔�?

CORBA 鏍囧噯鏄叕鍏卞璞¤姹備唬鐞嗙粨鏋�(Common Object Request Broker Architecture)锛岀敱瀵硅薄绠$悊缁勭粐 (Object ManagementGroup锛岀缉鍐欎负 OMG)鏍囧噯鍖栥�傚畠鐨勭粍鎴愭槸鎺ュ彛瀹氫箟璇█(IDL),璇█缁戝畾(binding:涔熻瘧涓鸿仈缂�)鍜屽厑璁稿簲鐢ㄧ▼搴忛棿浜掓搷浣滅殑鍗忚銆傚叾鐩殑涓猴細鐢ㄤ笉鍚岀殑绋嬪簭璁捐璇█涔﹀啓鍦ㄤ笉鍚岀殑杩涚▼涓繍琛岋紝涓轰笉鍚岀殑鎿嶄綔绯荤粺寮�鍙戙��

3. Linux

4銆丩INUX涓嬬嚎绋嬶紝GDI绫荤殑瑙i噴銆�

LINUX瀹炵幇鐨勫氨鏄熀浜庢牳蹇冭交閲忕骇杩涚▼鐨�"涓�瀵逛竴"绾跨▼妯″瀷锛屼竴涓嚎绋嬪疄浣撳搴斾竴涓牳蹇冭交閲忕骇杩涚▼锛岃�岀嚎绋嬩箣闂寸殑绠$悊鍦ㄦ牳澶栧嚱鏁板簱涓疄鐜般��

GDI绫讳负鍥惧儚璁惧缂栫▼鎺ュ彛绫诲簱銆�

5. 闂緱绋�閲岀硦娑傜殑棰�

6銆佸洓绉嶄細璇濊窡韪妧鏈�

浼氳瘽浣滅敤鍩烻ervletsJSP椤甸潰鎻忚堪

page鍚︽槸浠h〃涓庝竴涓〉闈㈢浉鍏崇殑瀵硅薄鍜屽睘鎬с�備竴涓〉闈㈢敱涓�涓紪璇戝ソ鐨� Java servlet绫伙紙鍙互甯︽湁浠讳綍鐨刬nclude鎸囦护锛屼絾鏄病鏈� include鍔ㄤ綔锛夎〃绀恒�傝繖鏃㈠寘鎷� servlet鍙堝寘鎷缂栬瘧鎴� servlet鐨� JSP椤甸潰

request鏄槸浠h〃涓� Web瀹㈡埛鏈哄彂鍑虹殑涓�涓姹傜浉鍏崇殑瀵硅薄鍜屽睘鎬с�備竴涓姹傚彲鑳借法瓒婂涓〉闈紝娑夊強澶氫釜Web缁勪欢锛堢敱浜� forward鎸囦护鍜� include鍔ㄤ綔鐨勫叧绯伙級

session鏄槸浠h〃涓庣敤浜庢煇涓� Web瀹㈡埛鏈虹殑涓�涓敤鎴蜂綋楠岀浉鍏崇殑瀵硅薄鍜屽睘鎬с�備竴涓� Web浼氳瘽鍙互涔熺粡甯镐細璺ㄨ秺澶氫釜瀹㈡埛鏈鸿姹�

application鏄槸浠h〃涓庢暣涓� Web搴旂敤绋嬪簭鐩稿叧鐨勫璞″拰灞炴�с�傝繖瀹炶川涓婃槸璺ㄨ秺鏁翠釜 Web搴旂敤绋嬪簭锛屽寘鎷涓〉闈€�佽姹傚拰浼氳瘽鐨勪竴涓叏灞�浣滅敤鍩�

7銆佺畝杩伴�昏緫鎿嶄綔(&,|,^)涓庢潯浠舵搷浣�(&&,||)鐨勫尯鍒��

鍖哄埆涓昏绛斾袱鐐癸細a.鏉′欢鎿嶄綔鍙兘鎿嶄綔甯冨皵鍨嬬殑,鑰岄�昏緫鎿嶄綔涓嶄粎鍙互鎿嶄綔甯冨皵鍨�,鑰屼笖鍙互鎿嶄綔鏁板�煎瀷

b.閫昏緫鎿嶄綔涓嶄細浜х敓鐭矾

鍗佷簩.鍏朵粬

1銆佽鐢ㄨ嫳鏂囩畝鍗曚粙缁嶄竴涓嬭嚜宸�.

4銆乄EB SERVICE鍚嶈瘝瑙i噴銆侸SWDL寮�鍙戝寘鐨勪粙缁嶃�侸AXP銆丣AXM鐨勮В閲娿�係OAP銆乁DDI,WSDL瑙i噴銆�

2銆佽鎶�http://tomcat.apache.org/棣栭〉鐨勮繖涓�娈佃瘽鐢ㄤ腑鏂囩炕璇戜竴涓�?

Apache Tomcat is the servlet container that is used in the officialReference Implementation for theJava ServletandJavaServerPagestechnologies. The Java Servlet and JavaServer Pagesspecifications are developed by Sun under theJavaCommunity Process.

Apache Tomcat is developed in an open andparticipatory environment and released under theApacheSoftware License. Apache Tomcat is intended to be a collaboration ofthe best-of-breed developers from around the world. We invite you toparticipate in this open development project. To learn more about gettinginvolved,click here.

ApacheTomcat powers numerous large-scale, mission-critical web applications across adiverse range of industries and organizations. Some of these users and theirstories are listed on thePoweredBywiki page.

3銆�缇庤祫杞欢鍏徃JAVA宸ョ▼甯堢數璇濋潰璇曢鐩�

1. Talk about overriding, overloading.

2. Talk about JAVA design patterns you known.

3. Talk about the difference between LinkList, ArrayList and Victor.

4. Talk about the difference between an Abstract class and an Interface.

5. Class a = new Class(); Class b = new Class();

if(a == b) returns true or false, why?

6. Why we use StringBuffer when concatenating strings?

7. Try to explain Singleton to us? Is it thread safe? If no, how to make itthread safe?

8. Try to explain Ioc?

9. How to set many-to-many relationship in Hibernate?

10. Talk about the difference between INNER JOIN and LFET JOIN.

11. Why we use index in database? How many indexes is the maximum in one tableas your suggestion?

12. When 鈥楩inal鈥� is used in class, method and property, what dose it mean?

13. Do you have any experience on XML? Talk about any XML tool youused ,e.g. JAXB, JAXG.

14. Do you have any experience on Linux?

15. In OOD what is the reason when you create a Sequence diagram?

Administrator 10:34:20

1锛屽爢鍜屾爤鐨勫尯鍒紝鏈変竴涓�64k鐨勫瓧绗︿覆锛屾槸鏀惧埌鍫嗕笂锛岃繕鏄斁鍒版爤涓婏紝涓轰粈涔堬紵

2锛屼粈涔堟椂鍊欑敤鍒版帴鍙o紝浠�涔堟椂鍊欑敤鍒版娊璞$被锛屼簩鑰呭尯鍒�

3锛屾湁涓�涓�100涓囩殑鏁扮粍锛岄噷杈规湁涓や釜甯傞噸澶嶇殑锛屽浣曡璁$畻娉曟壘鍒般��

4锛岃璁℃暟鎹簱鏃讹紝n缁达紝濡備綍璁捐銆�

渚嬪[鐪佷唤][鍩庡競][缃戝惂]锛岃繖鏄笁缁村叧绯伙紝瀹冪殑琛ㄤ篃搴旇鏈変笁涓紝缃戝惂鏈夊閿紩鐢ㄥ煄甯傦紝鍩庡競鏈夊閿簲鐢ㄧ渷浠斤紝杩欎釜瑙勫緥灏辨槸涓嬪眰鐨勮鏈変竴澶栭敭鍘诲紩鐢ㄤ笂灞傘��

你可能感兴趣的:(Java鍒濈骇闈㈣瘯棰�)