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:
- Java has no lambda.
- You must know and write down the type of every Java variable in advance.
- List manipulation is difficult in Java. (No filter, no (list 1 2 3 4), etc.)
- You must use object-oriented programming in Java, packaging state with methods.
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); } |
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); } |
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); } |
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) } |
Credits: This guide was
created by Westley Weimer in Spring 2009.
|