Java Lecture 1
Java Lecture 1
Administrivia
prerequisites
assignments
1st assignment is posted now due one week after class, midnight
what is java?
java is
a programming environment
a large set of libraries (java API)
a philosophy
java philosophy
programs should be easy to write and understand programs should be as efficient as possible
java compiler (javac) converts (compiles) source code into "bytecode" (files ending in ".class")
example: Foo.class (may compile other files too if "Foo.java" depends on them)
% javac Foo.java
% java Foo
libraries
input/output
http://java.sun.com/j2se/1.4.2/docs/api/index.html
"object oriented"
object: data + functions acting on that data class: template for building objects; includes
data (fields) that every object contains functions (methods) that can act on the object
char
boolean
strongly typed: all data has a type statically typed: all types must be declared before use
type declarations can occur anywhere in source code // foo has type int
int foo;
methods have
a name a set of arguments with their types a return type some optional modifiers
in file "HelloWorld.java":
}
}
class definition:
}
}
method definition:
}
}
method name:
}
}
method arguments:
}
}
}
}
method modifiers:
}
}
method body:
}
}
compile:
run:
data types
int integers
float single precision floating point double double precision floating point char Unicode characters (16 bit) boolean true or false (not 0 or 1) byte 8 bits; "raw data" String character strings
operators
like in C:
+ - * / % = ++ -- += -= etc.
precedence:
comments
three kinds:
// This comment goes to the end of the line. /* This comment can span * multiple lines. */
/**
* This comment is for documentation. */
conditionals
int i = 10; if (i < 20) { System.out.println("less than 20"); } else if (i == 20) { System.out.println("equal to 20"); } else { System.out.println("greater than 20"); }
loops (1)
int i; for (i = 0; i < 10; i++) { // do something with i } while (i < 20) { // do something with i // increment i }
loops (2)
now "i" only usable inside the loop judgment call; usually the right thing to do