* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Today • Class structure • Java Overview • Hello, World! • Java on
Survey
Document related concepts
Class (computer programming) wikipedia , lookup
Object-oriented programming wikipedia , lookup
Control flow wikipedia , lookup
Name mangling wikipedia , lookup
Covariance and contravariance (computer science) wikipedia , lookup
Go (programming language) wikipedia , lookup
Java syntax wikipedia , lookup
C Sharp syntax wikipedia , lookup
Scala (programming language) wikipedia , lookup
Java (programming language) wikipedia , lookup
Transcript
Today Class Structure • Class structure • MTWR 8-10PM through the 22nd • Java Overview • No class next monday (holiday) • Hello, World! • Suggested assigments • Java on Athena • Email: [email protected] • “Procedural Java” • http://www.mit.edu/iap/java/ 1 Prerequisites 2 Goals • Minimal programming clue • Teach basics of Java language • Some knowledge of Athena • Write Java programs on Athena • Access to Athena or any computer • Cover useful parts of the Java library • Time 3 4 Things this class isn’t Useful references • How to approach programming problems (that’s 6.001) • Java in a Nutshell • http://java.sun.com/docs/ • Discipline / theory / form (that’s 6.170) • Mission-critial applications (that’s 6.033) • Lots of O’Reilly books on http://safari.oreilly.com/ for free from campus • Network applications • JavaScript 5 Buzzwords ahoy! 6 A brief history of Java • Imperative • In the beginning, there was C • Object-oriented • And lo, there was OO: C++ • Garbage-collected • And the Sun came into the land: Java • A programming language (Turing-complete!) • An execution environment 7 8 Java vs. C++ Hello, World! • Garbage collection: no malloc() or free() • No parameterized types • No obvious pointers or references • Native comilation vs. bytecode public class Hello { /** * Prints a greeting message * * @param args Command line arguments */ public static void main(String[] args) { System.out.println("Hello, world!"); } } 10 9 Running Hello, World! What’s in the program? (classes) athena% cd ~/java class Hello { ... } athena% ls Hello.java • Declares a Java class athena% javac Hello.java athena% ls Hello.class • Just using it now as an entry point Hello.java • Will do spiffier stuff later athena% java Hello Hello, world! 11 12 What’s in the program? (methods) public static void main(String[] args) { ... } • Declares a method named main What’s in the program? (functions) System.out.println("Hello, world!"); • Calls a function in System.out named println • println just prints a line • Returns nothing (void) • Doesn’t need an instance (static) • Anybody can call it (public) • Takes one parameter named args 14 13 What’s in the program? (comments) Java on Athena (setup) /* Comments provide extra information * This is one style */ • Add to .environment file: add java setenv CLASSPATH $HOME/java // Or two slashes for just // one line at a time • Also, just once, create your java directory: cd; mkdir java 15 16 Java on Athena (compiling) Variables Variables are storage locations for data: • Java compiles to “bytecode” • javac takes a .java and makes it a .class • java runs classes; finds them in the CLASSPATH int i = 17; System.out.println(i); int j = i * 2; System.out.println(j); i = i + 1; System.out.println(i); • In this case, Hello class is in /̃java • java Hello calls Hello’s main 17 Primitive types 18 Functions • Everything has a type • Package up some amount of computation • Integer types (int, short, long) • Small playpen to contain local variables (“scoping”) • Floating-point types (float, double) • Take parameters, return some value (void means “no return value”) • Truth values (boolean) • Object types (more than we can count; later) 19 • return statement immediately goes back to where we were before 20 Fibonacci (a favorite) Variables, assignments, and expressions <type> <name>; // Create a new variable named // <name> of type <type> public static int fib(int i) { if (i < 2) return i; <type> <name> = <expr>; // Ditto, but set its initial // value to <expr> return fib(i - 1) + fib(i - 2); } <name> = <expr>; // Update value of pre-existing // variable named <name> // to be value of <expr> • One declaration per variable (in one scope) • Assign whenever and whenever (if defined in scope) • { } denote scope 22 21 Primitive arguments are pass by value Expressions void i_three(int i) { i = 3; } fib(2) i % 2 i + 42 // subroutine calls // modulo (remainder) // usual +, -, *, / int j = 1; i_three(j); System.out.println(j); // Prints 1 a && b d || a !m // boolean and // boolean or // boolean not n << 3 x & mask x | mask ~k // // // // 23 left shift bitwise and bitwise or bitwise negation 24 Rational operators (making booleans) // For any type: a == b // Equality; not just one = a != b // Inequality Other useful expressions a = b // assignment returns left-hand side! x ? y : z /* conditional, exactly like: * if (x) * y; * else * z; * ..except as an expression */ // Numeric types: a < b a <= b a > b a >= b 26 25 = vs. == a++ and a– • Be careful! • a++ returns a, then increments it by one • Both are expressions • ++a increments a, and returns new value • a = b is assignment • Similarly with a-- and --a • a == b is comparison • Generally only used alone on a line, or in loops • No silly := thingy 27 28 Literals "foo" ’X’ true, false 42 6.170 null // // // // // // There Is More Than One Way To Do It String char booleans int double "not-object" boolean b; b = x > 17; if (b) c = 5; else c = 6; // Or.. c = (x > 17) ? 5 : 6; 29 30 Conditionals If/then pitfalls (1) if (<cond>) <stmt>; else <stmt>; if (a) if (b) // ... else // ... if (cond) { <stmt>; <stmt>; // ... } • else belongs to closest (most recent) if • Can leave off else clause 31 32 If/then pitfalls (1) If/then pitfalls (2) if (a) { if (b) { // ... } } else { // ... } if (a > 6) display(a); a = 6; • Use braces to make clearer 33 If/then pitfalls (2) 34 While loops if (a > 6) { display(a); a = 6; } while (<bool expr>) <stmt>; while (<bool expr>) { <stmt>; <stml>; // ... } • Again, braces to the rescue • Repeats as long as boolean expression evaluates to true • May not even run once • Same pitfalls as with if 35 36 For loops for (<stmt>; <cond>; <stmt>) { <stmt>; <stmt>; // ... } For loops as while loops // Done at start // Tested each time // Update done each time for (int i = 0; i < 10; i++) { // ... } // Is the same as: int i = 0; while (i < 10) { // ... i++; } for (int i = 0; i < 10; i++) { // ... } 37 Control flow (continue) 38 Control flow (break) for (int i = 0; i < 10; i++) { if (i % 2 == 0) continue; System.out.println(i); } • continue moves on to the next iteration immediately int i = 0; while (true) { if (i >= 10) break; if (i % 2 == 0) { // The following line was missing // from the code earlier, which // would have caused an infinite // loop! Think about why.. i++; continue; } System.out.println(i); i++; } • break jumps out of a loop immediately 39 40 Return statements • return statement stops the current function • Takes a value if the return type is not void • Can have more than one return in a function, but only one will be called • Not needed if the function has return type void 41