Java 學習筆記
把會重複的動作合併,把方法定義出來
函示在物件導向中隸屬於某一類別(Class): 如下方的 MainClass,故稱為成員函式(member function),又稱為方法(method)
通常一個功能由多種函式組成
public class MainClass {
public static viod main(String[] args) {
Scanner scan = new Scanner(System.in);
int a;
a = scan.nextInt();
int b = scan.nextInt();
}
}
可以方便設計程式、實作、運算
public static int[] f(int a, int b) {
return a + 1;
}
public class MainClass {
public static viod main(String[] args) {
int x = f(5);
System.out.println(x);
}
public static int f(int a) {
return a + 1;
}
}
會回傳 6
public class MainClass {
public static viod main(String[] args) {
int arr[] = new int[5];
for (int = 0; int < arr.length; i++) {
arr[i] = i + 1;
}
swap(arr, 1, 3); // 陣列位置1, 3交換位置
for (int = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
public static void swap(int[] arr, int index1, int index2) {
int tmp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = tmp;
}
}
public class MainClass {
public static viod main(String[] args) {
int a = 5;
a = pow(a, 3);
System.out.println(a);
}
public static int pow(int a, int b) {
int result = a;
for (int i = 0; i < b - 1; i++) {
result *= a;
}
return result;
}
}
序列化程序會將物件轉換成位元組資料流,以將物件儲存或傳輸到記憶體、資料庫或檔案。 其主要目的是儲存物件的狀態,這樣就能在需要時重新建立該物件。 反向的程序則稱為還原序列化。 圖片來源: 微軟文件 The object is serialized to a stream that carries the data. The stream may also have information about the object's type, such as its version, culture, and assembly name. From that stream, the object can be stored in a database, a file, or memory. 序列化的用法 序列化可讓開發人員儲存物件的狀態,並視需要重新建立物件,以提供物件和資料交換的儲存體。 透過序列化,開發人員可以執行下列動作: 使用 web 服務將物件傳送到遠端應用程式
Mar 4, 2022官方文件 提供類別,用來傳送 HTTP 要求,以及從 URI 所識別的資源接收 HTTP 回應。 // HttpClient is intended to be instantiated once per application, rather than per-use. See Remarks. static readonly HttpClient client = new HttpClient(); static async Task Main() { // Call asynchronous network methods in a try/catch block to handle exceptions. try
Feb 22, 2022字串中的每個字元都是由 Unicode 純量值所定義,也稱為 Unicode 程式碼點或 Unicode 字元的序數 (數值) 值。 每個程式碼點都是使用 UTF-16 編碼進行編碼,而編碼的每個專案的數值會以物件來表示 Char 。 字串是用來表示文字的連續字元集合。 String記憶體中物件的大小上限為 2 GB 或大約1000000000個字元。 字串的操作通常考量以下兩點: 封裝次數 記憶體消耗
Feb 21, 2022要完整的複製整棵樹 :evergreen_tree: 必須要完整走訪過每個節點 Node 要在判斷內 new,不然新複製的數每個最底下的分支會多一個裡面沒東西的 Node public Node CopyNode(Node originalNode) { Node clonedNode = null; if (originalNode != null) { clonedNode = new Node();
Feb 17, 2022or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up