鍦ㄩ�� Stack Overflow 鐨勬椂鍊欙紝鍙戠幇浜嗕竴浜涜闂噺鍍忛樋灏斿崙鏂北涓�鏍烽珮鐨勯棶棰橈紝姣斿璇磋繖涓細Java 鍒板簳鏄�间紶閫掕繕鏄紩鐢ㄤ紶閫掞紵璁块棶閲忚冻瓒虫湁 188涓�+锛岃繖涓嶅緱浜嗗晩锛佽鏄庢湁寰堝寰堝鐨勭▼搴忓憳琚繖涓棶棰樺洶鎵拌繃銆傚疄璇濆疄璇村惂锛�鎴�灏辨槸鍏朵腑涔嬩竴銆�
鏉ュ洖椤句竴涓嬫彁闂�呯殑闂锛�
鎴戜竴鐩磋涓� Java 鏄寜寮曠敤浼犻�掔殑锛屼絾鏄垜鐪嬩竴浜涘崥瀹笂璇翠笉鏄殑銆傛垜灏辩撼闂蜂簡锛孞ava 鍒板簳鏄�间紶閫掕繕鏄紩鐢ㄤ紶閫掞紵鍊间紶閫掑拰寮曠敤浼犻�掓湁浠�涔堝尯鍒憿锛�
濡傛灉浣犱篃鏇捐杩欎釜闂鍥版壈杩囷紝鎴栬�呮鍦ㄨ鍥版壈锛屽氨璇烽殢鎴戜竴璧锋潵姊崇悊涓�涓嬮棶棰樼殑绛旀銆傛墦鎬繘闃跺柦锛�
01銆佸�间紶閫掑拰寮曠敤浼犻��
浠�涔堟槸鍊间紶閫掞紝浠�涔堟槸寮曠敤浼犻�掞紵鎴戜滑闇�瑕佸厛鎶婅繖涓や釜瀹氫箟鎼炴竻妤氾紝鎵嶈兘鎼炴竻妤� Java 鏄寜鍊间紶閫掕繕鏄寜寮曠敤浼犻�掋��
鍊间紶閫掞紙pass by value锛夋槸鎸囧湪璋冪敤鏂规硶鏃跺皢瀹炲弬澶嶅埗涓�浠戒紶閫掑埌鏂规硶涓紝杩欐牱褰撴柟娉曞褰㈠弬杩涜淇敼鏃朵笉浼氬奖鍝嶅埌瀹炲弬銆�
寮曠敤浼犻�掞紙pass by reference锛夋槸鎸囧湪璋冪敤鏂规硶鏃跺皢瀹炲弬鐨勫湴鍧�鐩存帴浼犻�掑埌鏂规硶涓紝閭d箞鍦ㄦ柟娉曚腑瀵瑰舰鍙傛墍杩涜鐨勪慨鏀癸紝灏嗗奖鍝嶅埌瀹炲弬銆�
涓婇潰鏄瘮杈冨畼鏂圭殑瀹氫箟锛岃璧锋潵涓嶅厤鐢熺‖銆傚湪鎴戠湅鏉ワ紝鍊间紶閫掑拰寮曠敤浼犻�掔殑鍏抽敭鍖哄埆鏈変袱鐐癸細
1锛夎皟鐢ㄦ柟娉曟椂鏈夋病鏈夊瀹炲弬杩涜澶嶅埗銆�
2锛夋柟娉曞唴瀵瑰舰鍙傜殑淇敼浼氫笉浼氬奖鍝嶅埌瀹炲弬銆�
what锛熷�间紶閫掑拰寮曠敤浼犻�掕繕娌℃湁鎼炴竻妤氾紝鍙堟潵涓や釜鏂板悕璇嶏細瀹炲弬鍜屽舰鍙傘�傚埆鎬ワ紝鍒�ャ��
02銆佸疄鍙傚拰褰㈠弬
瀹炲弬鍜屽舰鍙傜悊瑙h捣鏉ユ瘮鍊间紶閫掑拰寮曠敤浼犻�掑鏄撶殑澶氾紝鍓嶈�呭氨濂藉儚鏄竴鍏冧竴娆℃柟绋嬶紝鍚庤�呭氨鍍忔槸涓�鍏冧簩娆℃柟绋嬨��
褰㈠弬锛氬畾涔夋柟娉曞悕鍜屾柟娉曚綋鐨勬椂鍊欎娇鐢ㄧ殑鍙傛暟锛岀洰鐨勬槸鐢ㄦ潵鎺ユ敹璋冪敤璇ユ柟娉曟椂浼犲叆鐨勫弬鏁般��
瀹炲弬锛氬湪璋冪敤鏈夊弬鏂规硶鏃朵紶鍏ョ殑鍙傛暟锛屾柟娉曞悕鍚庨潰鐨勬嫭鍙蜂腑鐨勫弬鏁伴�氬父琚О涓衡�滃疄鍙傗�濄��
澶у搴旇閮藉啓杩団�渉ello world鈥濈▼搴忎簡锛屽氨鍍忎笅闈㈣繖鏍枫��
public class Cmower {
public static void main(String[] args) {
System.out.println("hello world");
}
}
鍏朵腑 args
灏辩浉褰撲簬鏄舰鍙傦紝鑰屽瓧绗︿覆鈥渉ello world鈥濆氨鐩稿綋浜庢槸瀹炲弬銆傚鏋滆寰楄繖涓緥瀛愪笉瀹规槗鐞嗚В锛岄偅鍐嶆潵鐪嬩竴涓��
public class Cmower {
public static void main(String[] args) {
Cmower cmower = new Cmower();
cmower.sop("娌夐粯鐜嬩簩");
}
public void sop(String name) {
System.out.println("hello " + name);
}
}
鍏朵腑鈥滄矇榛樼帇浜屸�濅负瀹炲弬锛涙湁鍙傛柟娉� sop(String name)
涓殑 name 涓哄舰鍙傘�傚舰鍙傚氨濂藉儚瀹炲弬涓庤璋冪敤鏂规硶涔嬮棿鐨勪竴涓ˉ姊侊紝鍚﹀垯璋冪敤鑰呮病娉曚紶閫掑弬鏁帮紝琚皟鐢ㄧ殑鏂规硶鏃犳硶鎺ユ敹鍙傛暟銆�
03銆佸熀鏈被鍨嬫槸鍊间紶閫掔殑
Java 涓殑鏁版嵁绫诲瀷鍙互鍒嗕负涓ょ锛屼竴绉嶆槸鍩烘湰绫诲瀷锛屼竴绉嶆槸寮曠敤绫诲瀷銆傛垜鐩镐俊澶у鍦ㄧ湅鏈瘒鏂囩珷涔嬪墠锛屽氨鑳藉杈炬垚杩欐牱涓�涓叡璇嗭細鍩烘湰绫诲瀷鏄�间紶閫掔殑銆傝繖涓�鐐规鏃犵枒闂��
public class Cmower {
public static void main(String[] args) {
Cmower cmower = new Cmower();
int age = 18;
cmower.sop(age);
System.out.println("main 涓殑 age " + age);
}
public void sop(int age) {
age = 28;
System.out.println("sop 涓殑 age " + age);
}
}
涓婇潰杩欐浠g爜涓紝sop()
鏂规硶鐨勫疄鍙� age 涓� 18锛屽敖绠� sop()
鏂规硶鐨勫舰鍙傝淇敼涓� 28锛屼絾骞朵笉浼氬奖鍝嶅疄鍙傜殑鍊笺�傝繖涓�鐐瑰彲浠ヤ粠杈撳嚭缁撴灉涓姞浠ヨ瘉鏄庛��
sop 涓殑 age 28
main 涓殑 age 18
鍏蜂綋鐨勬墽琛岃繃绋嬪涓嬪浘鎵�绀恒��
04銆佸紩鐢ㄧ被鍨嬫槸鍊间紶閫掑悧锛�
澶у涔嬫墍浠ヤ笉纭畾 Java 鏄�间紶閫掔殑杩樻槸寮曠敤浼犻�掔殑锛屽師鍥犲氨鍑哄湪杩欎釜寮曠敤绫诲瀷涓婇潰銆傚崟浠庡瓧闈㈢殑鎰忔�濅笂灏卞鏄撴悶娣凤細寮曠敤绫诲瀷涓嶆槸寮曠敤浼犻�掗毦閬撹繕鏄�间紶閫掞紵
public class Cmower {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static void main(String[] args) {
Cmower cmower = new Cmower();
cmower.setName("娌夐粯鐜嬩簩");
cmower.sop(cmower);
System.out.println("main 涓殑 cmower " + cmower.getName());
}
public void sop(Cmower cmower) {
cmower.setName("娌夐粯鐜嬩笁");
System.out.println("sop 涓殑 cmower " + cmower.getName());
}
}
鍦� main()
鏂规硶涓紝鎴戜滑閫氳繃 new 鍏抽敭瀛楀垱寤轰簡涓�涓璞� cmower锛屽苟灏嗗叾 name 灞炴�ц缃负鈥滄矇榛樼帇浜屸�濓紱鐒跺悗灏嗗疄鍙� cmower 浼犻�掔粰 sop()
鏂规硶锛屽湪 sop()
鏂规硶涓皢褰㈠弬 cmower 鐨� name 灞炴�т慨鏀逛负鈥滄矇榛樼帇涓夆�濄�傝緭鍑虹粨鏋滄槸浠�涔堟牱瀛愬憿锛�
sop 涓殑 cmower 娌夐粯鐜嬩笁
main 涓殑 cmower 娌夐粯鐜嬩笁
鍛�锛佸疄鍙� cmower 鐨勫睘鎬� name 绔熺劧涓嶆槸鈥滄矇榛樼帇浜屸�濊�屾槸鈥滄矇榛樼帇涓夆�濅簡锛佺湅鐪嬶紝鐪嬬湅锛孞ava 涓嶆槸鍊间紶閫掑惂锛�
鍒�ュ埆鎬ャ�傛垜浠湪 main 鏂规硶涓拷鍔犲嚑琛屼唬鐮併��
Cmower cmower = new Cmower();
cmower.setName("娌夐粯鐜嬩簩");
Cmower old = cmower;
cmower.sop(cmower);
System.out.println("main 涓殑 cmower " + cmower.getName());
System.out.println(old == cmower);
old == cmower
浼氭槸 true 杩樻槸 false 鍛紵闂笂鐪肩潧鎯充竴鎯炽�傚鏋滃疄鍦ㄦ槸鎯充笉鍑猴紝鎶涗竴鏋氱‖甯佸惂锛屽弽姝d笉鏄� true 灏辨槸 false銆傚亣濡傚紩鐢ㄧ被鍨嬫槸寮曠敤浼犻�掔殑锛屾牴鎹紩鐢ㄤ紶閫掔殑瀹氫箟锛堝舰鍙傜殑淇敼灏嗕細褰卞搷鍒板疄鍙傦級锛岄偅涔堢粨鏋滀竴瀹氬氨鏄� false銆�
鎴戜滑鏉ョ湅涓�涓嬭緭鍑虹粨鏋滐細
sop 涓殑 cmower 娌夐粯鐜嬩笁
main 涓殑 cmower 娌夐粯鐜嬩笁
true
true锛熷紑浠�涔堢帺绗戯紵
涓嶅ソ鎰忔�濓紝娌℃湁寮�鐜╃瑧銆侸ava 鐨勭‘鏄�间紶閫掔殑銆傚彧涓嶈繃锛屽紩鐢ㄧ被鍨嬪湪璋冪敤鏈夊弬鏂规硶鐨勬椂鍊欙紝浼犻�掔殑鏄璞$殑寮曠敤锛屽苟涓嶆槸瀵硅薄鏈韩銆傝�屽璞$殑寮曠敤鍦ㄤ紶閫掔殑杩囩▼涓苟娌℃湁鍙戠敓鏀瑰彉锛岃櫧鐒跺璞℃湰韬彂鐢熶簡鍙樺寲銆傚彲浠ラ�氳繃涓嬮潰杩欏箙鍥炬劅鍙椾竴涓嬨��
杩欎笅鐞嗚В浜嗗惂锛�
05銆佹�荤粨
鏉ョ湅涓嬮潰杩欐浠g爜銆�
int age = 18;
String name = "娌夐粯鐜嬩簩";
age 鏄熀鏈被鍨嬶紝鎵�浠ュ�煎氨鐩存帴淇濆瓨鍦ㄥ彉閲忎腑锛涜�� name 鏄紩鐢ㄧ被鍨嬶紝鍙橀噺涓繚瀛樼殑鍙槸瀵硅薄鐨勫唴瀛樺湴鍧�锛岃繖绉嶅彉閲忎竴鑸О涔嬩负瀵硅薄鐨勫紩鐢ㄣ��
鍩烘湰绫诲瀷浣滀负鍙傛暟琚紶閫掓椂鑲畾鏄�间紶閫掞紱寮曠敤绫诲瀷浣滀负鍙傛暟琚紶閫掓椂涔熸槸鍊间紶閫掞紝鍙笉杩団�滃�尖�濅负瀵瑰簲鐨勫紩鐢ㄣ��
濂戒簡鍚勪綅璇昏�呮湅鍙嬩滑锛屼互涓婂氨鏄湰鏂囩殑鍏ㄩ儴鍐呭浜嗐��鑳界湅鍒拌繖閲岀殑閮芥槸鏈�浼樼鐨勭▼搴忓憳锛屾垜蹇呴』瑕佷负澶у鐐逛釜璧�馃憤銆傚鏋滆寰椾笉杩囩樉锛岃繕鎯崇湅鍒版洿澶氾紝鎴戝啀鎺ㄨ崘鍑犵瘒缁欏ぇ瀹躲��
370W+绋嬪簭鍛樺叧娉ㄨ繃鐨勯棶棰橈細濡備綍姣旇緝 Java 鐨勫瓧绗︿覆锛�
250W+绋嬪簭鍛樺叧娉ㄨ繃鐨勯棶棰橈細浠�涔堟槸 NullPointerException锛�
50W+绋嬪簭鍛樺叧娉ㄨ繃鐨勯棶棰橈細涓轰粈涔堜細鍙戠敓ArrayIndexOutOfBoundsException锛�
鏈夋敹鑾凤紵灏辩偣璧炪�佺暀瑷�锛岃鏇村鐨勪汉鐪嬪埌杩欑瘒鏂囩珷銆�
濡傛灉鎯宠绗竴鏃堕棿鐪嬪埌鎴戞洿鏂扮殑鏂囩珷锛屽彲浠ュ井淇℃悳绱€��娌夐粯鐜嬩簩銆嶏紝鍏虫敞鎴戠殑鍏紬鍙凤紝鍥炲銆�java銆嶅啀閫佷綘涓�浠�绮鹃�夌數瀛愪功澶хぜ鍖�锛屽寘鍚繖鍗佸勾鏉ユ垜璇昏繃鐨勬渶浼樿川鐨� Java 涔︾睄銆�