cs1120  Fall 2010

cs1120: Computer Science
from Ada and Euclid to Quantum Computing and the World Wide Web


Instructor
Westley Weimer

Email Address
cs1120-staff@cs.virginia.edu

Class Meetings
Mondays and Wednesdays, 3:30-4:45pm in CHEM 005
Office & Lab Hours
Mo 14:30-15:00 Wes
Mo 17:00-19:00 Evan
Fr 15:00-16:00 Nathan
Su 13:00-14:00 Evan Yang
Su 14:00-15:00 Evan
Su 15:00-17:00 Jon


Located in Thornton Stacks
or OLS 001 (We 11:00-14:00 + Su)
or OLS 219 (Mo/We 14:30-15:00)

Schemer's Guide to Java

Java is an object-oriented, structured, imperative programming language invented in 1995 by James Gosling and associated with Bill Joy and Guy Steel (of inventing-Scheme fame). It was originally named Oak after a tree outside the window.

The purpose of this document is to give an undergraduate student who is familiar with Scheme and Python an introduction to reading Java.

Summary

Python is a universal programming language — this means every process we could describe using a Turing Machine (or using Scheme or Python), we can also describe in Java. The syntax of Java is very different from Scheme — it is more similar to Python.

Key differences with Java, for the purposes of this class:

More recent versions of Java lift some of these restrictions, but for standard Java you are likely to encounter they are all basically true. (If you already know enough about Java to suggest anonymous inner classes as a weak substitute for lambda, this guide is not for you.)

Glossy Comparison

Property Scheme Python Java
Universal Yes Yes Yes
Source
Code
Succinct Succinct Verbose
Functional
Programming
Easy Easy Harder
Object-Oriented
Programming
If You Want If You Want Basically Required
State Updates Discouraged
set!
Encouraged
=
Basically Required
=
Write Types
In Source
No No Yes
Catch Type
Errors Early
No No Yes
Block Structured If You Want
(usually No)
If You Want
(usually Yes)
Always Yes
Operation Ordering Prefix Infix Infix

Expressions

Scheme Python Java
(+ age (sqrt 3)) age + sqrt(3) age + sqrt(3)

Variable Declaration and Assignment

Scheme Python Java
(let ((x 1)) 
  (set! x (+ x 5))
  (print x) 
) 
  x = 1
  x = x + 5
  print x
{
  int x = 1;
  x = x + 5;
  println(x); 
} 
In the Java example, int is the type of the variable x. Note that Java requires a semicolon after every statement.

Blocks and Conditionals

Scheme Python Java
(if (eq? x 0)
  (begin
    (print x)
    (print y)
  )
  (print z)
) 
if x == 0:
  print x
  print y
else:
  print z 
if (x == 0) { 
  println(x); 
  println(y); 
} else {
  println(z); 
} 
Python uses tabbing to show block structure. Java uses { curly braces }.

Functions

Scheme Python Java
(define (fib n)
  (if (< n 2)
    n
    (+ (fib (- n 1))
       (fib (- n 2)))))
def fib(n):
  if n < 2:
    return n
  else:
    return fib(n-1) + fib(n-2)
int fib(int n) {
  if (n < 2) 
    return n;
  else 
    return fib(n-1) + fib(n-2);
}
In the Java example, int fib(...) means that fib is a function that returns something of type int. Similarly, int n means the argument must be of type int.

Numeric Iteration

Scheme Python Java
(map (lambda (x)
  (print (* x 3))
) (intsto 10))
for x in range(1,10):
  print (x * 3)
for (int x=1; x <= 10; x=x+1) {
  print (x*3)
} 
The Java for loop construct has three parts: the initialization, the loop guard, and the increment.

Credits: This guide was created by Westley Weimer in Spring 2009.