Java

System.out.println()

  • println()
  • System
  • out

System out println()

Java

  • 1.
  • 2.
  • 3.
  • 4.

  • 1.addPerson
  • 2. JUnit test<MethodUnderTest>_<state> testPop_emptyStack

( ){ ... ... return ; }

  • returnValueType returnValueType void

D53C92B3-9643-4871-8A72-33D491299653.jpg [D53C92B3-9643-4871-8A72-33D491299653.jpg]

public static int age(int birthday){...}

static float interest(float principal, int year){...}

voidvoid

2 num1 num2

/** */ public static int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; return result; }

public static int max(int num1, int num2) { return num1 > num2 ? num1 : num2; }

Java

int larger = max(30, 40);

voidprintlnvoid

System.out.println("");

TestMax.java

public class TestMax { /** */ public static void main(String[] args) { int i = 5; int j = 2; int k = max(i, j); System.out.println( i + " " + j + " " + k); } /** */ public static int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; return result; } }

5  2 5

main max main JVM main

main public static, void main, String[] String[]


void

void

printGrade

TestVoidMethod.java

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'); } } }

C

printGradevoid

void main


n

TestVoidMethod.java

public static void nPrintln(String message, int n) { for (int i = 0; i < n; i++) { System.out.println(message); } }

TestPassByValue.java

public class TestPassByValue { public static void main(String[] args) { int num1 = 1; int num2 = 2; System.out.println(" num1 " + num1 + " num2 " + num2); // swap swap(num1, num2); System.out.println(" num1 " + num1 + " num2 " + num2); } /** */ public static void swap(int n1, int n2) { System.out.println("\t swap "); System.out.println("\t\t n1 " + n1 + "n2 " + n2); // n1 n2 int temp = n1; n1 = n2; n2 = temp; System.out.println("\t\t n1 " + n1 + "n2 " + n2); } }

 num1 1 num2 2
     swap 
         n1 1n2 2
         n1  2n2 1
 num1 1 num2 2

swap


maxint

public static double max(double num1, double num2) { if (num1 > num2) return num1; else return num2; }

maxint intmax

doubledoublemax

Java


for

12-130Q1221013F0.jpg [12-130Q1221013F0.jpg]

main()

CommandLine.java

public class CommandLine { public static void main(String[] args){ for(int i=0; i<args.length; i++){ System.out.println("args[" + i + "]: " + args[i]); } } }

$ javac CommandLine.java 
$ java CommandLine this is a command line 200 -100
args[0]: this
args[1]: is
args[2]: a
args[3]: command
args[4]: line
args[5]: 200
args[6]: -100

Constructor new

  • void
  • new

Java ( public public protected protected)

// class MyClass { int x; // MyClass(int i ) { x = i; } }

ConsDemo.java

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); } }

10 20

Java

JDK 1.5 Java

typeName... parameterName

(...)

VarargsDemo.java

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); } }

The max value is 56.5
The max value is 3.0

finalize()

finalize Java 9 Java try-with-resources java.lang.ref.Cleaner

Java () finalize( )

finalize()

finalize()

finalize()

protected void finalize() { // }

protected finalize()

Java JVM

FinalizationDemo.java

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"); } }

$ javac FinalizationDemo.java 
$ java FinalizationDemo
Cake Object 1is created
Cake Object 2is created
Cake Object 3is created
Cake Object 3is disposed
Cake Object 2is disposed

AutoCloseable try-with-resources

try (Resource res = new Resource()) {
    // 
} catch (Exception e) {
    e.printStackTrace();
}