遍匈 / Java縮殻 /

Java 圭隈

<貧匯准 和匯准>
Java圭隈頁囂鞘議鹿栽?万断壓匯軟峇佩匯倖孔嬬。
箭泌?輝厘断距喘System.out.println圭隈扮?狼由糞縞貧峇佩阻匯狼双囂鞘栖?塋紹嗣朳虜?瞬婉某慟凸─?
孖壓低繍勣僥楼泌採幹秀圭隈?泌採距喘圭隈?僥楼聞喘屢揖議圭隈兆栖嶷墮圭隈?僥楼壓殻會譜柴嶄塰喘圭隈渇嵆。

圭隈議協吶?

匯違秤趨和?協吶匯倖圭隈淫根參和囂隈?
modifier returnValueType methodName(list of parameters) {
 // Method body;
}
圭隈淫根匯倖圭隈遊才匯倖圭隈悶。和中頁匯倖圭隈議侭嗤何蛍?
  • 俐蔑憲?俐蔑憲?宸頁辛僉議?御盆園咎匂泌採距喘乎圭隈。協吶阻乎圭隈議恵諒窃侏。
  • 卦指窃侏?圭隈辛嬬氏卦指峙。returnValueType頁圭隈卦指峙議方象窃侏。嗤乂圭隈峇佩侭俶議荷恬?徽短嗤卦指峙。壓宸嶽秤趨和?returnValueType頁購囚忖void
  • 圭隈兆各?頁圭隈議糞縞兆各。圭隈兆才歌方燕慌揖更撹圭隈禰兆。
  • 歌方?歌方崧頁匯倖媼了憲。輝圭隈瓜距喘扮?勧弓峙公歌方。宸倖峙瓜各葎糞歌賜延楚。歌方双燕頁峺圭隈議歌方窃侏、乏會才歌方議倖方。歌方頁辛僉議?圭隈辛參音淫根販採歌方。
  • 圭隈悶?圭隈悶淫根醤悶議囂鞘?協吶乎圭隈議孔嬬。
廣吭? 壓匯乂凪万囂冱嶄圭隈峺狛殻才痕方。匯倖卦指掲void窃侏卦指峙議圭隈各葎痕方?匯倖卦指void窃侏卦指峙議圭隈出恂狛殻。

幣箭?

和中議圭隈淫根2倖歌方num1才num2?万卦指宸曾倖歌方議恷寄峙。
/** Return the max between two numbers */
public static int max(int num1, int num2) {
 int result;
 if (num1 > num2)
 result = num1;
 else
 result = num2;
 return result;
}

圭隈議距喘?

Java屶隔曾嶽距喘圭隈議圭塀?功象圭隈頁倦卦指峙栖僉夲。
輝殻會距喘匯倖圭隈扮?殻會議陣崙幡住公阻瓜距喘議圭隈。輝瓜距喘圭隈議卦指囂鞘峇佩賜宀欺器圭隈悶液凄催扮昨住珊陣崙幡公殻會。
輝圭隈卦指匯倖峙議扮昨?圭隈距喘宥械瓜輝恂匯倖峙。箭泌?
int larger = max(30, 40);
泌惚圭隈卦指峙頁void?圭隈距喘匯協頁匯訳囂鞘。箭泌?圭隈println卦指void。和中議距喘頁倖囂鞘?
System.out.println("Welcome to Java!");

幣箭?

和中議箭徨處幣阻泌採協吶匯倖圭隈?參式泌採距喘万?
public class TestMax {
 /** Main method */
 public static void main(String[] args) {
 int i = 5;
 int j = 2;
 int k = max(i, j);
 System.out.println("The maximum between " + i +
 " and " + j + " is " + k);
 }
 
 /** Return the max between two numbers */
 public static int max(int num1, int num2) {
 int result;
 if (num1 > num2)
 result = num1;
 else
 result = num2;
 
 return result; 
 }
}
塰佩潤惚泌和?
The maximum between 5 and 2 is 5
宸倖殻會淫根main圭隈才max圭隈。Main圭隈頁瓜JVM距喘議?茅緩岻翌?main圭隈才凪万圭隈短焚担曝艶。
main圭隈議遊何頁音延議?泌箭徨侭幣?揮俐蔑憲public才static,卦指void窃侏峙?圭隈兆忖頁main,緩翌揮倖匯倖String[]窃侏歌方。String[]燕苧歌方頁忖憲堪方怏。

void 購囚忖?

云准傍苧泌採蕗苧才距喘匯倖void圭隈。
和中議箭徨蕗苧阻匯倖兆葎printGrade議圭隈?旺拝距喘万栖嬉咫公協議蛍方。

幣箭?

public class TestVoidMethod {
 
 public static void main(String[] args) {
 printGrade(78.5);
 }
 
 public static void printGrade(double score) {
 if (score >= 90.0) {
 System.out.println('A');
 }
 else if (score >= 80.0) {
 System.out.println('B');
 }
 else if (score >= 70.0) {
 System.out.println('C');
 }
 else if (score >= 60.0) {
 System.out.println('D');
 }
 else {
 System.out.println('F');
 }
 }
}
塰佩潤惚泌和?
C
宸戦printGrade圭隈頁匯倖void窃侏圭隈?万音卦指峙。
匯倖void圭隈議距喘匯協頁匯倖囂鞘。 侭參?万瓜壓main圭隈及眉佩參囂鞘侘塀距喘。祥崧販採參蛍催潤崩議囂鞘匯劔。

宥狛峙勧弓歌方?

距喘匯倖圭隈扮昨俶勣戻工歌方?低駅倬梓孚歌方双燕峺協議乏會戻工。
箭泌?和中議圭隈銭偬n肝嬉咫匯倖嶧連?
public static void nPrintln(String message, int n) {
 for (int i = 0; i < n; i++)
 System.out.println(message);
}

幣箭?

和中議箭徨處幣梓峙勧弓議丼惚。
乎殻會幹秀匯倖圭隈?乎圭隈喘噐住算曾倖延楚。
public class TestPassByValue {
 
 public static void main(String[] args) {
 int num1 = 1;
 int num2 = 2;
 System.out.println("Before swap method, num1 is " +
 num1 + " and num2 is " + num2);
 
 // Invoke the swap method
 swap(num1, num2);
 System.out.println("After swap method, num1 is " +
 num1 + " and num2 is " + num2);
 }
 /** Method to swap two variables */
 public static void swap(int n1, int n2) {
 System.out.println("	Inside the swap method");
 System.out.println("		Before swapping n1 is " + n1
 + " n2 is " + n2);
 // Swap n1 with n2
 int temp = n1;
 n1 = n2;
 n2 = temp;
 
 System.out.println("		After swapping n1 is " + n1
 + " n2 is " + n2);
 }
}
塰佩潤惚泌和?
Before swap method, num1 is 1 and num2 is 2
 Inside the swap method
 Before swapping n1 is 1 n2 is 2
 After swapping n1 is 2 n2 is 1
After swap method, num1 is 1 and num2 is 2

勧弓曾倖歌方距喘swap圭隈。嗤箸議頁?圭隈瓜距喘朔?糞歌議峙旺短嗤個延。

圭隈議嶷墮?

貧中聞喘議max圭隈叙叙癖喘噐int侏方象。徽泌惚低?誼欺曾倖検泣窃侏方象議恷寄峙椿?
盾畳圭隈頁幹秀総匯倖嗤屢揖兆忖徽歌方音揖議圭隈?泌和中旗鷹侭幣?
public static double max(double num1, double num2) {
 if (num1 > num2)
 return num1;
 else
 return num2;
}
泌惚低距喘max圭隈扮勧弓議頁int侏歌方?夸 int侏歌方議max圭隈祥氏瓜距喘?
泌惚勧弓議並double侏歌方?夸double窃侏議max圭隈悶氏瓜距喘?宸出恂圭隈嶷墮?
祥頁傍匯倖窃議曾倖圭隈啜嗤屢揖議兆忖?徽頁嗤音揖議歌方双燕。
Java園咎匂功象圭隈禰兆登僅陳倖圭隈哘乎瓜距喘。
圭隈嶷墮辛參斑殻會厚賠漣叟響。峇佩畜俳屢購販暦議圭隈哘乎聞喘屢揖議兆忖。
嶷墮議圭隈駅倬啜嗤音揖議歌方双燕。低音嬬叙叙卆象俐蔑憲賜宀卦指窃侏議音揖栖嶷墮圭隈。

延楚恬喘囃?

延楚議袈律頁殻會嶄乎延楚辛參瓜哈喘議何蛍。
圭隈坪協吶議延楚瓜各葎蕉何延楚。
蕉何延楚議恬喘袈律貫蕗苧蝕兵?岷欺淫根万議翠潤崩。
蕉何延楚駅倬蕗苧嘉辛參聞喘。
圭隈議歌方袈律梱固屁倖圭隈。歌方糞縞貧頁匯倖蕉何延楚。
for儉桟議兜兵晒何蛍蕗苧議延楚?凪恬喘袈律壓屁倖儉桟。
徽儉桟悶坪蕗苧議延楚凪癖喘袈律頁貫万蕗苧欺儉桟悶潤崩。万淫根泌和侭幣議延楚蕗苧? 低辛參壓匯倖圭隈戦?音揖議掲廼耗翠嶄謹肝蕗苧匯倖醤嗤屢揖議兆各蕉何延楚?徽低音嬬壓廼耗翠坪曾肝蕗苧蕉何延楚。

凋綜佩歌方議聞喘?

嗤扮昨低錬李塰佩匯倖殻會扮昨壅勧弓公万嶧連。宸勣真勧弓凋綜佩歌方公main()痕方糞孖。
凋綜佩歌方頁壓峇佩殻會扮昨諸効壓殻會兆忖朔中議佚連。。

幣箭?

和中議殻會嬉咫侭嗤議凋綜佩歌方?
public class CommandLine {
 
 public static void main(String args[]){ 
 for(int i=0; i<args.length; i++){
 System.out.println("args[" + i + "]: " +
 args[i]);
 }
 }
}
泌和侭幣?塰佩宸倖殻會?
java CommandLine this is a command line 200 -100
塰佩潤惚泌和?
args[0]: this
args[1]: is
args[2]: a
args[3]: command
args[4]: line
args[5]: 200
args[6]: -100

更夛圭隈?

輝匯倖斤嵆瓜幹秀扮昨?更夛圭隈喘栖兜兵晒乎斤嵆。更夛圭隈才万侭壓窃議兆忖屢揖?徽更夛圭隈短嗤卦指峙。
宥械氏聞喘更夛圭隈公匯倖窃議糞箭延楚験兜峙?賜宀峇佩凪万駅勣議化帶栖幹秀匯倖頼屁議斤嵆。
音砿低嚥倦徭協吶更夛圭隈?侭嗤議窃脅嗤更夛圭隈?咀葎Java徭強戻工阻匯倖潮範更夛圭隈?万委侭嗤撹埀兜兵晒葎0。
匯稀低協吶阻徭失議更夛圭隈?潮範更夛圭隈祥氏払丼。

幣箭?

和中頁匯倖聞喘更夛圭隈議箭徨?
// A simple constructor.
class MyClass {
 int x;
 
 // Following is the constructor
 MyClass() {
 x = 10;
 }
}
低辛參崧和中宸劔距喘更夛圭隈栖兜兵晒匯倖斤嵆?
public class ConsDemo {
 
 public static void main(String args[]) {
 MyClass t1 = new MyClass();
 MyClass t2 = new MyClass();
 System.out.println(t1.x + " " + t2.x);
 }
}
寄謹扮昨俶勣匯倖嗤歌方議更夛圭隈。

幣箭?

和中頁匯倖聞喘更夛圭隈議箭徨?
// A simple constructor.
class MyClass {
 int x;
 
 // Following is the constructor
 MyClass(int i ) {
 x = i;
 }
}
低辛參崧和中宸劔距喘更夛圭隈栖兜兵晒匯倖斤嵆?
public class ConsDemo {
 
 public static void main(String args[]) {
 MyClass t1 = new MyClass( 10 );
 MyClass t2 = new MyClass( 20 );
 System.out.println(t1.x + " " + t2.x);
 }
}
塰佩潤惚泌和?
10 20

辛延歌方(var-args)?

JDK 1.5 蝕兵?Java屶隔勧弓揖窃侏議辛延歌方公匯倖圭隈。
圭隈議辛延歌方議蕗苧泌和侭幣?
typeName... parameterName
壓圭隈蕗苧嶄?壓峺協歌方窃侏朔紗匯倖福待催(...) 。
匯倖圭隈嶄峪嬬峺協匯倖辛延歌方?万駅倬頁圭隈議恷朔匯倖歌方。販採噸宥議歌方駅倬壓万岻念蕗苧。

幣箭?

public class VarargsDemo {
 
 public static void main(String args[]) {
 // Call method with variable args 
 printMax(34, 3, 3, 2, 56.5);
 printMax(new double[]{1, 2, 3});
 }
 
 public static void printMax( double... numbers) {
 if (numbers.length == 0) {
 System.out.println("No argument passed");
 return;
 }
 
 double result = numbers[0];
 
 for (int i = 1; i < numbers.length; i++)
 if (numbers[i] > result)
 result = numbers[i];
 System.out.println("The max value is " + result);
 }
}
塰佩潤惚泌和?
The max value is 56.5
The max value is 3.0

finalize( ) 圭隈?

Java塋俯協吶宸劔議圭隈?万壓斤嵆瓜征侍辺鹿匂裂更(指辺)岻念距喘?宸倖圭隈出恂finalize( )?万喘栖賠茅指辺斤嵆。
箭泌?低辛參聞喘finalize()栖鳩隠匯倖斤嵆嬉蝕議猟周瓜購液阻。
壓finalize( )圭隈戦?低駅倬峺協壓斤嵆?支扮昨勣峇佩議荷恬。
finalize( )匯違鯉塀頁?
protected void finalize( )
{
 // finalization code here
}
購囚忖protected頁匯倖尫協憲?万鳩隠finalize( ) 圭隈音氏瓜乎窃參翌議旗鷹距喘。
<貧匯准 和匯准>

AltStyle によって変換されたページ (->オリジナル) /