編寫方法名時,我們應該遵循駝峰命名法,即第一個單詞的第一個字母應小寫,其餘(後面)單詞的第一個字母應大寫。例如 public class Test { public void sampleMethod() { System.out.println("This is sample method"); } public void demoMethod() { System.out.println("This is demo method"); } public static void main(String args[]) { Test obj = new Test(); obj.sample(); obj.demo(); } } 輸出 This is sample method This is demo method
Java 中的有效識別符號 – 必須以字母(A 到 Z 或 a 到 z)、貨幣字元 ($) 或下劃線 (_) 開頭。第一個字元之後可以是任何字元組合。不能是關鍵字。例如以下示例顯示了在 Java 中宣告變數時使用的各種可能的識別符號。即時演示 public class VariableTest { public static void main(String args[]) { // 宣告一個名為 num 的變數 int num = 1; ... 閱讀更多
多行註釋 (/* */) 用於註釋原始碼中的多行。例如即時演示 public class CommentsExample { /* 以下是此處的主方法,我們建立一個名為 num 的變數。並列印其值 * */ public static void main(String args[]) { //宣告一個名為 num 的變數 int num = 1; ... 閱讀更多
要註釋特定行,只需在該行之前放置“雙反斜槓 (//)”即可,如下所示。// Hello this line is commented 例如以下示例演示了在 Java 中使用單行註釋。即時演示 public class CommentsExample { public static void main(String args[]) { //宣告一個名為 num 的變數 int num = 1; //列印變數 num 的值 System.out.println("value if the variable num: "+num); } } 輸出 value if the variable num: 1
java.lang 包是 Java 中的預設包,預設情況下會匯入它。因此,無需顯式匯入此包。即無需匯入即可訪問此包的類。例如如果您觀察以下示例,這裡我們沒有顯式匯入 lang 包,但是,我們仍然能夠使用 java.lang.Math 類的 sqrt() 方法計算數字的平方根。即時演示 public class LangTest { public static void main(String args[]) { int num = 100; ... 閱讀更多