cs150  Spring 2009

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


Instructor
Westley Weimer

Teaching Assistants
Zak Fry
Paul DiOrio
Rachel Lathbury

Email Address
cs150-staff@cs.virginia.edu

Class Meetings
Mondays and Wednesdays, 3:30-4:45pm in MEC 341
Structured Lab Hours
Wednesdays, 7:00-8:00pm and 8:00-9:00pm in OLS 001
Staffed Lab Hours
(Small Hall Lab)

Monday 5:00-6:00 (Zak)
Tuesday 3:15-4:15 (Rachel)
Thursday 5:00-6:00 (Paul)
Sunday 3:00-4:00 (on request)
Office & Lab Hours
(Small Hall Lab)

Monday 2:00-3:00 (Rachel)
Tuesday 11:00-12:00 (Wes in Olsson 219)
Tuesday 3:00-4:00 (Zak)
Wednesday 1:00-2:00 (Paul)

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.

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

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.
cs150: Computer Science
University of Virginia
weimer@virginia.edu
Using these Materials