Modified version from the original at http://ece.vuse.vanderbilt.edu/multimedia/CLASSWORK/java-intro.txt 1. Our first Java application example: >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World"); } // main } // HelloWorld <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Notes: - naming conventions .java and .class - JDK: compiler: javac - creates .class files from .java files interpreter : java - runs .class files - command line arguments - main's parameters array of Strings args.length tells how many strings Note for C/C++ programmers: program name not included in args 2. Our first Java applet example: >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> import java.applet.*; import java.awt.*; public class HelloWorldApplet extends Applet { public void paint(Graphics g) { g.drawString("Hello World",25,50); } // paint } // HelloWorldApplet <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Corresponding html file: >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> <H1>Hello World Applet Test Page</H1> Notes: - appletviewer (best for debugging) or Netscape 3. Name space - No global variables - every class is part of a package - fully qualified name: java.lang.Math.PI - package statement first line of source code otherwise: default unnamed package - import statement: convenience top of file (after package statement) three forms: - import package; - import package.class; - import package.*; examples: import java.util; // access: util.Hashtable import java.util.Hashtable; // access: Hashtable import java.util.* // access: Hashtable // plus all other class in package default: java.lang.* (all .java files get this automatically) access restrictions: public private protected private protected package (default) 4. Local variables (declared in methods) - no global namespace - there are no global variables - declare anywhere in the method code (MY STYLE - declare them first) - inside for loop ( ) (this is the only exception) 5. Comments: /* comment */ // comment to end of line /** JavaDoc comment **/ 6. No prepocessor - constants:final public final class Math { ... public static final double PI = 3.14159; ... } no name collision convention: capital letters - no macros - no include files directory and file structure no variable and function declaration Java file: interface definition and implementation for a class 7. Unicode characters: 16 bit, lower 8-bit is ASCII 8. Primitive data types - fixed platform-independent length - all numerical types are signed - default values (0 or null) - types: type size boolean 1 char 16 byte 8 short 16 int 32 long 64 float 32 double 64 - boolean: true or false conditional must be of boolean type: int i =10; while(i--) // WON'T WORK in Java, you C/C++ programmers ...; 9. Reference data types - objects and arrays - different variables may refer to the same object: Button a,b; a = new Button(); b = a; a.setLabel("OK"); String s = b.getLabel(); // s contains "OK" this is not true of simple types: int i = 3; int j = i; i = 2; // i == 2 and j == 3 - equality: == tests whether two objects refer to the same object, NOT whether they have the same values - pointers: object instances are similar to pointers, but: - cannot be cast to integers - no pointer arithmetic - no sizeof operator reasons: - simpler, easier - security - default value: null (reserved keyword) 10. Objects - creating objects: new (class name) : call to contructor method Button b; b = new Button(); ComplexNumber c = new ComplexNumber(1.1,2.2); - deleting objects: automatic: garbage collection - accessing object's variables and methods: . operator ComplexNumber c = new ComplexNumber(); c.x = 1.1; c.y = 2.2; double magnitude d = c.magnitude(); 11. Arrays - similar to objects : - by reference - created by new - garbage collected - creating an array does not create the objects! Button buttons[]; // buttons == null buttons = new Button[12]; // buttons[0] == null buttons[0] = new Button(); // buttons[0] refers to a valid Button - static initializer: int powers_of_two[] = { 1, 2, 4, 8, 16, 32 }; - multidimensional byte two_dim[][] = new byte[256][16]; int test[][] = new int[12][]; Button bad[][] = new Button[][5]; // ILLEGAL! int triangle[][] = { { 1 }, { 2, 1 }, { 3, 2, 1}, { 4, 3, 2, 1}}; - accessing elements: int square[] = new int[32]; for(int i = 0; i < square.length; i++) square[i] = i * i; - special syntax: char[] buffer; int[] twodim[]; 12. Strings - not null-terminated - immutable: Sister class: StringBuffer - initialization: String s = "This is a String"; - concatenation: String s2 = "Does this work?" + " Yepp"; - some methods: length(), charAt(), equals(), and many more! Check the API 13. Operators - same as C, except MINUS: - dereference * - address of & - -> - sizeof PLUS: - string concatenation + - instanceof - right shift with zero extension >>> - logical and & , or | : always evaluate both operands 14. Statements - SAME AS IN C: - if/else - while - do/while - for - switch - labeled break and continue - 10 POINTS OFF IF YOU USE THESE!!!! (except in switch statements) - no label: same as C - label: (made-up example) >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> test: if(checkit == 0) { outer: for(int i = 0 ; i < 10; i++) { inner: for(j = 0; j < 5; j++) { if(j == i) continue outer; if(a[i][j] == null) break test; a[i][j].do_something(); } } } <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< - labeled break: any enclosing statement - labeled continue: enclosing loop - package, import - LATER: - synchronized - try, catch, finally, throw, throws 15. Miscellaneous - forward references except for variable initialization - method overloading signature: number, type and order of parameters class exmp { ... void print_me(int i); // OK void print_me(int i, int j); // OK void print_me(double d); //OK int print_me(double oops); // CONFLICT! ... } - Modifiers: - final - static - native - synchronized - volatile - NO: (for those of you who know C or C++) - structures (simulate with class with no methods) - unions - enumerated types (simulate with static final variables) - bitfields - typedef (built in!) - variable length argument list (sumulate with array of Object parameter)