INFO1113×Week1
Lecture - Java Fundamentals
- Compilation, Syntax, Types and Ifs
I/O
1. Output
System.out.println("Hello World!"); // print on the next line
System.out.print("Hello World!"); // print on the same line
2. Input
① Scanner
import java.util.Scanner;
Scanner sc = new Scanner(System.in); // create a scanner object
String a = sc.nextline(); // catch next line
String b = sc.next(); // catch next word
int c = sc.nextInt(); // catch next integer
② Command Line Arguments
- If not having inputs, will raise ArrayIndexOutOfBoundsException.
String s = args[0];
int n = Integer.parseInt(args[0]);
// length of command line arguments
int length = args.length;
Types
- String: reference type
- Prefer double than float
Boolean
- Cannot recognize 0 or 1 as boolean type.
a && b // and
a || b // or
!a // not
If statements
if (true){
// only execute if true
}
else if (true){