/ ** returns two values larger integer data variables * / public static int max ( int num1, int num2) { int Result; IF (num1> num2) Result = num1; the else Result = num2; return Result; }
public class TestMax { / ** master methods * / public static void main (String [] args) { int I =. 5 ; int J = 2 ; int K = max (I, J); System.out.println (I + "and" + j + "comparison, the maximum value is:" + K); } / ** return two integers larger the value of variable * / public static int max ( int num1, int num2) { int Result; IF ( num1> num2) Result = num1; else result = num2; return result; } }
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'); } } }
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 ( "num1 value before the exchange:" + num1 + ", the value num2: "+ num2); // call the method swap swap (num1, num2); System.out.println ( " num1 values after an exchange: "+ num1 +", the value num2: "+ num2); } / * * the method of switching two variables * / public static void the swap ( int N1,int n2) { System.out.println ( "\ T into the swap method" ); System.out.println ( "\ T \ T n1 is a value before the exchange:" + n1 + ", the value of n2:" + n2 ); // exchange values of n1 and n2 int TEMP = n1; n1 = n2; n2 = TEMP; System.out.println ( "\ t \ t after the exchange value n1" + n1 + ", the value of n2: "+ N2); } }
// a simple constructor class MyClass { int X; // The following is the constructor MyClass () { X = 10 ; } }
// a simple constructor class MyClass { int X; // The 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); } }
public class VarargsDemo { public static void main(String 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); } }
public class FinalizationDemo { public static void main(String[] args) { Cake c1 = new Cake(1); Cake c2 = new Cake(2); Cake c3 = new Cake(3); c2 = c3 = null; System.gc(); //调用Java垃圾收集器 } } class Cake extends Object { private int id; public Cake(int id) { this.id = id; System.out.println("Cake Object " + id + "is created"); } protected void finalize() throws java.lang.Throwable { super.finalize(); System.out.println("Cake Object " + id + "is disposed"); } }