Java function & static method
Math.random(),randomnumber
Integer.parseInt(),transform into Int
A function can be available
○ create a name
○ declare type and name of parameter(s)
○ specify type for return value
○ implement body of method
○ finish with return statement(s)
○ Char toUpperCase(char c) , to uppercase
○ Char toLowerCase(char c) , to lowercase
○ String toString(char c) , to a String object consist of one character
String.substring(A,B) 第A个数到第B个数的字符串(从0开始)
chatAt
37.Scop
Global variables and local variables
public static void printInts(int[] nums) {
System.out.print("[");
for (int i = 0; i < nums.length; i++) {
if (i != nums.length-1)
System.out.print(nums[i] + ", ");
else
System.out.print(nums[i]);
}
System.out.println("]");
}
public static void main(String[] args) {
int[] arr = {1, 2, 3};
printInts(arr);
}
Notice the scop of “nums” “i” “args” “arr”
Use global variables sparingly
38.Method Signature
Definition: Two of the components of a method declaration comprise the method signature—the method's name and the parameter types.
方法声明的两个组件构成了方法签名 - 方法的名称和参数类型。
public double calculateAnswer(double wingSpan,
int numberOfEngines,double length, double grossTons) {
//do the calculation here
}
Comments NOTHING