CS61B-Lab 00

javac, java, Git

Posted by Tianxiang Gao on June 22, 2015

A. Java Compilation & Development

Why Compilation?

Compilers are quite helpful for reasons:

  • Compilers can check for errors such as type errors and syntax errors.

  • Compilers can help speed up programs since translate high level language into lower or machine language and saves the instruction in a file called binary.

  • Compilers can verify access rights to classes and methods.

  • Others introudced in CS61C.

Compiling Java Programs

We will be using javac as compiler which is included in Oracle’s Java Development Kit(JDK).

$ javac (-g) File.java

Where flag “-g” is helpful for generatting debugging information

Running Java programs

Compiling your program give you .class files which contain bytecode.

$ java File

B. Writing Java Programs

Java is Object-Oriented

import java.io.*;

class LeapYear {
	public static void main(String[] args) throws Exception {
		BufferedReader keybd = new BufferedReader(new InputStreamReader(System.in));
		int year = Integer.parseInt(keybd.readLine());
		if (year % 400 == 0) {
			System.out.println(year + " is a leap year.");
		} else if (year % 100 != 0 && year % 4 == 0) {
			System.out.println(year + " is a leap year.");
		} else {
			System.out.println(year + " is not a leap year.");
		}
	}
}