Learning Java

The Java Language


Text Encoding

    unicode, can be specified as \uXXXX, where XXXX is 1-4 hex digits

Comments

    /* multiline 
    comment */

    // single-line comment

    /** javadoc comment */

Types

	All types known at compile time, info available at runtime.

	Primative Types

		Boolean    true/false
		Char        16-bit unicode
		Byte        8-bit signed 2's comp int
		Short        16
		Int        32
		Long        64
		Float        32-bit IEEE 754 value
		Double    64

	Var Declaration and Initialization

		int foo;
		int foo = 42 * 3;

		Instance var's are set to default values, local var's are not.
		Scope is limited to declaring code block.

	Reference Types

		These point to objects and cannot be examined directly.
		The type is that of objects class, parent class, or implemented interface.
		null can be assigned to any reference type.

		NOTE: all types are passed by value (copy), but when you pass a reference type
		by value, in effect you've passed an object by reference.

		NOTE: Strings are objects, but the compiler offers some extras here:
		String s = "I love to have fun."; //quoted material automatically objectified
		String s += "  Do you?" + ":)";   //'+' and '+=' are only ovrldd operators

	Arrays

		Special reference type which holds elements of its base type.

		Declaration and Creation

			int [] arrayOfInts;    //same as    int arrayOfInts [];
			int [] arrayOfInts = new int [34];
			String [] someStrings = new String [42];

			Creation of an array of objects creates references not instances.  Use curly
			braces for creating and initializing an array.

			int [] arrayOfInts = {1,2,3,5,7};
			Object [] arrayOfObjects = { stopButton, "A word", null };

		Using

			int i = arrayOfInts[0]; // puts the first value into i
			int l = arrayOfInts.length; //puts 34 into l

Statements and Expressions

    Statements - describe all activies of a Java program

      var declaration and initialization
      code blocks are statements
      methods - code blocks that take parameters

      if ( condition ) //condition is a boolean expression
        statement;
      [else
        statement; ]

      while ( condition )
        statement;

      do
        statement;
      while ( condition );

      for ( init; condition; incr )
        statement;

      switch ( int expression ) {
        case int expression :
          statment;
          break;
        [...
        default :
          statment; ]
      }

      break [label]; // terminate innermost or labeled loop/switch
      continue [label]; // same as break but just the current iteration

      also; try, catch, finally, throw, and synchronized will be dicussed later

    Expressions - produce a value of numeric, reference, or void type when evaluated

      Operators - almost the same as C
      Assignment - both statement and expression
      Dot operator - can be used to access instance or static var or invoke a method
      Method Invocation - an expression, can be used in further evaluations
      Example Compound Expression - len = myObj.name.substring(5,10).length();

Exceptions

	Catch or Specify Requirement

		A method must either catch or specify that it can throw all checked exceptions
		that can be thrown within its scope. Those which are not caught are passed up
		the call stack.

		void exMethod(String s) throws ex1Exception, ex2Exception {
			try
				statement; //may throw and exception types 1, 2, or 3
			catch (ex3Exception e)
				statement; //attempt to handle exception type 3
			finally
				statement; //execute in any case
		}

		Unchecked exceptions are those subclassed from java.lang.RuntimeException or
		java.lang.Error.  These can be ignored but it might not be the best idea.
		Notice how the exception subtypes enhance flow control.

	Throwing 

		throw new Exception("something bad happened");