Computer Science 15-110, Spring 2010
Class Notes: Getting Started
- Installation
- Hello World
- Basic Console Output
- print vs println
- Printing Numbers (not Strings)
- Comments
- End-of-Line Comments
- Block Comments
- Errors
- Syntax Errors (Compile-Time Errors)
- Runtime Errors ("Crash")
- Logical Errors (Compiles and Runs, but
is Wrong!)
- Variables and Expressions
- Basic Console Input
Getting Started
- Installation
See the notes on installing Java,
DrJava, and JCreator.
- Hello World
class
HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
- Basic Console Output
- print vs println
class MyCode {
public static void main(String[] args) {
System.out.print("This is");
System.out.print("a test");
System.out.println("that will run, albeit");
System.out.println();
System.out.println("with some problems...");
}
}
- Printing numbers (not Strings)
class MyCode {
public static void main(String[] args) {
System.out.println(1);
System.out.println(1 + 2);
}
}
- Comments
- End-Of-Line Comments
class MyCode {
public static void main(String[] args) {
System.out.println("Howdy!"); // this is an end-of-line comment
// (but not a very good one!)
// The next line is commented out, so it is ignored!
// System.out.println("This will be ignored");
System.out.println("And howdy on line #2");
}
}
- Block Comments
class MyCode {
public static void main(String[] args) {
System.out.println("Howdy!");
/* The preceding comments end at the end of the line, but this
is a block comment, started with a slash-star, which will continue
across new lines until a star-slash (on the next line):
*/
System.out.println("And howdy on line #2"); /* another block comment! */
}
}
- Errors
- Syntax Errors (Compile-Time Errors)
class MyCode {
public static void main(String[] args) {
System.out.println("Uh oh!); // ERROR! missing close-quote
}
}
Compiler output:
MyCode.java:3: unclosed string literal
System.out.println("Uh oh!); // ERROR! missing close-quote
- Runtime Errors ("Crash")
class MyCode {
public static void main(String[] args) {
System.out.println(1/0); // ERROR! Division by zero!
}
}
Runtime (Console) output:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at MyCode.main(Foo.java:3)
- Logical Errors (Compiles and Runs, but is Wrong!)
class MyCode {
public static void main(String[] args) {
System.out.println("2+2=5"); // ERROR! Untrue!!!
}
}
Runtime (Console) output:
2+2=5
- Variables and Expressions
class MyCode {
public static void main(String[] args) {
int x = 5;
int y = 3;
int sum = x + y;
System.out.print(x);
System.out.print("+");
System.out.print(y);
System.out.print("=");
System.out.println(sum);
}
}
Once again, but more concisely:
class MyCode {
public
static void main(String[] args) {
int x = 5;
int y = 3;
int sum = x + y;
System.out.println(x + "+" + y + "=" + sum);
}
}
- Basic Console Input
import java.util.Scanner;
class MyCode {
public
static void main(String[] args) {
Scanner scanner = new
Scanner(System.in);
int x =
scanner.nextInt();
int y =
scanner.nextInt();
int sum = x + y;
System.out.println(x + "+" + y + "=" + sum);
}
}
Once again, but with nicer prompts and more operators:
import java.util.Scanner;
class MyCode {
public
static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an integer: ");
int x = scanner.nextInt();
System.out.print("Enter another integer: ");
int y = scanner.nextInt();
int sum = x + y;
System.out.println(x + "+" + y + "=" + sum);
int difference = x - y;
System.out.println(x + "-" + y + "=" + difference);
int product = x * y;
System.out.println(x + "*" + y + "=" + product);
int quotient = x / y;
System.out.println(x + "/" + y + "=" + quotient);
int remainder = x % y;
System.out.println(x + "%" + y + "=" + remainder);
}
}
carpe diem -
carpe diem - carpe diem - carpe diem
- carpe diem - carpe diem -
carpe diem - carpe diem - carpe
diem