以下是一些Java常用功能代碼的示例:
System.out.println("Hello, World!");
int[] arr = {1, 2, 3, 4, 5};
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
int num = 10;
if (num > 0) {
System.out.println("Positive");
} else if (num < 0) {
System.out.println("Negative");
} else {
System.out.println("Zero");
}
public static int add(int a, int b) {
return a + b;
}
int sum = add(3, 4);
System.out.println(sum);
public class Person {
private String name;
public Person(String name) {
this.name = name;
}
public void sayHello() {
System.out.println("Hello, " + this.name + "!");
}
}
Person person = new Person("John");
person.sayHello();
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
List<String> list = new ArrayList<>();
list.add("apple");
list.add("banana");
list.add("orange");
for (String fruit : list) {
System.out.println(fruit);
}
Map<String, Integer> map = new HashMap<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("orange", 3);
int quantity = map.get("apple");
System.out.println(quantity);
這些只是一些常見的功能示例,Java是一門非常強大和靈活的編程語言,可以實現許多更復雜的功能。