* language * Java * comment * // Java notes:  // operators: o[o] o.o o(o,o) // array access, member, method call o++ o-- // post-increment, -decrement !o ~o ++o --o +o -o (T)o // not, compl, incr, decr, plus, negate, cast o*o o/o o%o // multiply, divide, modulo o+o o-o // add, substract o<>o o>>>o // shifts: left, signed right, unsigned right oo o>=o o instanceof T // compare lt,le, gt, ge; RTTI-check o==o o!=o // equals, differs o&o // bit-and o^o // bit-xor o|o // bit-or o&&o // short-circuit boolean andif o||o // short-circuit boolean orelse o?o:o // short-circuit if-then-else expression o=o o+=o o-=o o*=o o/=o o%=o o&=o o|=o o^=o o<<=o o>>=o o>>>=o // assignments  // execution control: else matches closest preceding if (as usual) if (bool_expr) statement else statement  for (statement_expr,statement_expr; bool_expr; statement_expr,statement_expr) statement  while (bool_expr) statement  do statement while (bool_expr);  switch (integral_type_expr) { // switch ALWAYS has a block case value1: // a switch-block MUST start with a case/default statements // and a statement or be empty break; // fall-through allowed case value2: // sequence of distinct cases allowed case value3: // a switch-block MUST start with a case/default default: statements break LABEL; case endValue: // no statement required after case/default }  // labelled breaks (break out of multiple levels): LABEL: loop_head { break; break LABEL; // LABEL can mark any ENCLOSING statement continue; continue LABEL; // LABEL must mark an enclosing loop } loop_end  try block catch (ExceptType var) block // optional catch (ExceptType var) block // optional finally block synchronized (object_expr) block // uses lock of object evaluated throw expr; // expr must derive from RuntimeException return expr; return;  // basic types: byte, short, int, long, float, double, char, boolean void // method "return type" only // array declaration: type_name[] variable; type_name variable[]; type_name[] variable = new type_name [size]; type_name[] variable = { val, val, val, ... }; // anonymous array creation (eg for arguments to methods) new type_name [size] { val, val, val, ... };  // packages package package_path.package_name; // export classes to this package import package_path.package_name.class; // import this class import package_path.package_name.*; // import all classes in this package  // class declaration (*keywords only for nested classes) public abstract {...} *protected *static class class_name extends C, C {...} *private final implements I, I {...} strictfp {...}  // interface declaration (*keywords only for member interfaces) public abstract {...} *protected *static interface interface_class extends I, I {...} *private strictfp {...}  // method definition (abstract and native have no code block) public abstract ; static {...} protected final synchronized return_type method(arg_type arg, ...) throws C, C {...} private native {...} strictfp ; // arg_type can be qualified "final" // interface method declaration: no code-block, always public abstract  // instance variables (only final is allowed for code-local variables): public transient type_name variable = val; // assigned when object created type_name variable; // default is package visibility static type_name[] variable; protected final type_name variable[]; private volatile type_name variable = val; // volatile for multithread access // class variables: mark as "static"; constant "variables": mark as "final" // instance variables not for serializing: mark as "transient"  // constructor forwarding: ClassName(type arg) { // constructor does not return a value this(arg, "a value"); // first statement: calls ClassName(type arg, String s) // rest of constructor } ClassName(type arg) { super(arg, "a value"); // invokes parent class's constructor ... // rest of constructor } // construction: // initialize instance members with default or class-defined values // execute initializer blocks (undecorated statement blocks in class) in order // execute constructor's this forwarded constructor if any // execute (rest of) constructor body // static initializer block: "static {...}" executed on class code loading ClassName var = new ClassName(constructor-args);  // finalize method - default is effectively: protected void finalize() throws Throwable { super.finalize(); } // can be called as any other method; also called by garbage collection  // values: false, true, null // reserved: const, goto * alias * operators [ ] . ( ) () ++ -- ! ~ * / % + - << >> >>> < > <= >= instanceof == != & ^ | && || ? : ?: = += -= *= /= %= &= |= ^= <<= >>= >>>= operators Operators: o[o] o.o o(o,o) // array access, member, method call o++ o-- // post-increment, -decrement !o ~o ++o --o +o -o (T)o // not, compl, incr, decr, plus, negate, cast o*o o/o o%o // multiply, divide, modulo o+o o-o // add/concatenate, substract o<>o o>>>o // shifts: left, signed right, unsigned right oo o>=o o instanceof T // compare lt,le, gt, ge; RTTI-check o==o o!=o // equals, differs o&o // bit-and o^o // bit-xor o|o // bit-or o&&o // short-circuit boolean and-if o||o // short-circuit boolean or-else o?o:o // short-circuit if-then-else expression o=o o+=o o-=o o*=o o/=o o%=o o&=o o|=o o^=o o<<=o o>>=o o>>>=o // assignments statements ; // empty statement local_variable_decl; local_class_decl expr_statement; // assign, pre/post-incr/decr, method-call, instance-creation assert bool_expr [ : msg_expr ]; // throws an AssertionError if !bool_expr { [ statement; ... ] } // block label: statement if (bool_expr) statement [ else statement ] for (init_statement_list; bool_expr; iter_statement_list) statement for (modifiers ContainedType var : IterableType_or_array_expr) statement while (bool_expr) statement do statement while (bool_expr); switch (int_expr) { [ case int_const | default ]: statement ... } break [ label ]; continue [ label ]; try block [ catch (exceptionType var) block ... ] [ finally block ] throw expression; return [ expression ]; synchronized ( object_expr ) block if if (bool_expr) statement  if (bool_expr) statement else statement for for (statement_expr,statement_expr; bool_expr; statement_expr,statement_expr) statement for ( [variable_modifiers ] Type var : IteratorType_or_array_expr) statement  Label: for (statement_expr,statement_expr;bool_expr;statement_expr,statement_expr) { break Label; continue Label; statement ... }  "Enhanced" for: for ( [ variable_modifiers ] Type var : IteratorType_or_array_expr) statement is equivalent to for (I #tmp_i = IteratorType_expr.iterator; #tmp_i.hasNext(); ) { [ variable_modifiers ] Type var = #tmp_i.next(); statement } or #tmp_a = array_expr; for (int #tmp_i = 0; #tmp_i < #tmp_a.length(); #tmp_i++) { [ variable_modifiers ] Type var = #tmp_a[#tmp_i]; statement } while while (bool_expr) statement  Label: while (bool_expr) { break Label; continue Label; statement ... } do do statement while (bool_expr);  Label: do { break Label; continue Label; statement ... } while (bool_expr); switch switch (integral_type_expr) { // switch ALWAYS has a block case value1: // a switch-block MUST start with a case/default statements // and a statement or be empty break; // fall-through allowed case value2: // sequence of distinct cases allowed case value3: // a switch-block MUST start with a case/default default: statements break LABEL; case endValue: // no statement required after case/default } * alias * break or continue break continue break or continue // break: break out of a loop or switch // continue: skip rest of loop statement until next iteration  LABEL: loop_head { break; break LABEL; // LABEL can mark any ENCLOSING statement continue; continue LABEL; // LABEL must mark an enclosing loop } loop_end * alias * try, catch, finally try catch finally try, catch, finally try { statements } catch (ExceptionType var) { // optional statements } ... // more catch blocks finally { // optional statements } throw throw expression; // expression must derive from RuntimeException return return; return expression; synchronized // statement synchronized (object_expr) { // uses lock of object evaluated statements }  // method declaration - see calltip "method" * alias * basic types byte short int long float double char boolean void true false basic types byte // 8 bit signed 2s-complement integer short // 16 bit signed 2s-complement integer int // 32 bit signed 2s-complement integer long // 64 bit signed 2s-complement integer float // 32 bit IEEE double // 64 bit IEEE char // 16 bit Unicode boolean // two values: true, false void // no value - method "return type" only * alias * array [] array // array declaration: type_name[] variable; type_name variable[]; type_name[] variable = new type_name [size]; type_name[] variable = { val, val, val, ... }; // anonymous array creation expression (eg for arguments to methods) new type_name [size] { val, val, val, ... } * alias * packages and importing package import packages and importing package package_path.package_name; // export classes to this package import package_path.package_name.class; // import this class import package_path.package_name.*; // import all classes in this package class // class declaration (¹keywords only for nested classes) class class_name scope: [ public | protected¹ | private¹ ] // default is package-scope attributes: { abstract | static¹ | final | strictfp } class-inheritance: [ extends BaseClass ] interfaces: [ implements Interface { , Interface } ] content: { method | property }  public abstract {...} protected¹ static¹ class class_name extends C {...} private¹ final implements I, I {...} strictfp {...} interface // interface declaration (¹keywords only for member interfaces) interface interface_class scope: [ public | protected¹ | private¹ ] // default is package-scope attributes: { abstract | static¹ | strictfp } interfaces: [ extends Interface { , Interface } ] content: { method | property }  public abstract {...} protected¹ static¹ interface interface_class extends I, I {...} private¹ strictfp {...} method // method definition (abstract and native have no code block) public abstract ; static {...} protected final synchronized return_type method(arg_type arg, ...) throws C, C {...} private native {...} strictfp ; // no return_type for a constructor (which has the same name as its class) // arg_type can be qualified "final" // interface method declaration: no code-block, always public abstract  Special methods: constructor: has same name as class, can be overloaded, called at instantiation. finalize: has name "finalize", called by garbage collector on an object no longer reachable from program * alias * variable field variable // fields or local variables (only final is allowed for code-local variables): public transient type_name variable = val; // assigned when object created type_name variable; // default is package visibility static type_name[] variable; protected final type_name variable[]; private volatile type_name variable = val; // volatile for multithread access // class variables: mark as "static"; constant "variables": mark as "final" // fields not for serializing: mark as "transient" * alias * access specifier public protected private access specifier access specifier public // available to all protected // available to the class and any derived classes private // available only to the class  Used in class, interface, method or instance variable declarations Default is no specifier - equivalent to public within owning package * alias * modifier abstract static final strictfp synchronized native transient static volatile modifier modifiers for classes, interfaces, methods, fields, local variables abstract - class, method static - class, interface, method final - class, interface, method, field, local variable (must be initialized) strictfp - class, interface, method synchronized - method native - method transient - field (not for serialization) static - field (belongs to class, not to object instance) volatile - field this this // used for constructor forwarding - class Class { Class(arglist1) { ... } Class(arglist2) { ... this(list1); // call Class(arglist1) ... } ... } super super // used for base-class subobject member access or construction - class Derived extends Base { Derived(arglist) { ... super(list); // call Base(arglist) to construct Base part ... super.method(...) // call Base's method not Derived's } ... }