<?PHP include('HEADER.php'); ?>

<h1>Java Cheatsheet/Overview</h1>

<p>Actually, this isn't so much a Java programming language cheatsheet as it is a series of running notes. I'm currently (December 2008) learning Java, so this page might not be that useful to anyone but me for a few weeks yet.</p>

<ul>
	<li><a href="#workflow">Workflow</a></li>
	<li><a href="#comments">Comments</a></li>
	<li><a href="#main">Main</a></li>
	<li><a href="#variables">Variables</a></li>
	<li><a href="#builtintypes">Built-in data types</a></li>
	<li><a href="#loops">Loops and conditionals</a></li>
	<li><a href="#classes">Classes and Objects</a></li>
	<li><a href="#exceptionhandling">Exception handling</a></li>
	<li><a href="#fileio">File IO</a></li>
	<li><a href="#jar">JAR for archiving and distribution</a></li>
	<li><a href="#links">Links</a></li>
</ul>

<h1 id="workflow">Workflow</h1>

<p>Code goes into a <code class="prettyprint">MyClass.java</code> text file. That source file is compiled like: <code class="prettyprint">javac MyClass.java</code>, which produces the bytecode file <code class="prettyprint">MyClass.class</code>. That bytecode (if it contains a runnable main class) can be executed in the Java virtual machine: <code class="prettyprint">java MyClass</code>.</p>

<h1 id="comments">Comments</h1>

<p>Java has end-of-line and block comments:</p>

<pre class="prettyprint">/* This is
a block
comment
*/
int foo = 3    // Another comment</pre>

<p>There are also <a href="http://java.sun.com/javadoc/index.html">javadoc</a> comments, which describle classes, constructors, methods, and field, and appear immediately before the declaration. Note that these start with two splats, not just one.</p>

<pre class="prettyprint">/**
* The class myClass provides . . .
*/
public class myClass { . . .</pre>

<h1 id="main">Main</h1>

<p>Each Java program has one or (usually) many classes, but only one Main method:</p>

<pre class="prettyprint">public class HelloWorld {
	public static void main (String[] args) {
		System.out.println("Hello, world!");
	}
}</pre>

<h1 id="variables">Variables</h1>

<pre class="prettyprint">int myInt = 2;
String[] myArray = {"foo", "bar", "bat"};
String myString = "The magic word is: " + myArray[myInt];
int[][] twoDimensional = {
	{ 2, 7, -19},
	{ 23, 9, 44},
	{ 18, -1, 3}
}</pre>

<h2>Built-in data types</h2>

<ul>
	<li><code class="prettyprint">boolean</code> with value "true" or "false"</li>
	<li><code class="prettyprint">char</code> like "A" or "\n" or "5"</li>
	<li><code class="prettyprint">int</code> like "3" or "-143"</li>
	<li><code class="prettyprint">double</code> like "33.2344" or "-0.753212"</li>
	<li><code class="prettyprint">String</code> like "Hello, world!" or "Foo Bar"</li>
</ul>

<h1 id="loops">Loops and Conditionals</h1>

<p>Java has the usual loops, including while loops:</p>

<pre class="prettyprint">int beers = 99
while (beers >= 0) {
	System.out.println(beers + " bottles of beer on the wall!");
	System.out.println(beers + " bottles of beer!");
	beers --;
	System.out.println("Take one down, pass it around, " + beers 
        + " bottles of beer on the wall!");
}</pre>

<p>And do-while loops:</p>

<pre class="prettyprint">do {
	stuff();
	i++;
} while (i &lt; 100);</pre>

<p>for loops:</p>

<pre class="prettyprint">for (int i = 0; i &lt;= n; i++) {
	doStuff();
}</pre>

<p>To get for-each functionality, use this for loop syntax (note lack of curly braces):</p>

<pre class="prettyprint">String[] birds = {"owl", "jay", "sparrow", "parrot"};
for (String b: birds)
	System.our.println("The " + b + " is a bird.");</pre>

<p>Java has switch statements:</p>

<pre class="prettyprint">switch (dayOfWeek) {
	case 0: String dayName = "Sunday"; break;
	case 1: String dayName = "Monday"; break;
	case 2: String dayName = "Tuesday"; break;
	case 3: String dayName = "Wednesday"; break;
	case 4: String dayName = "Thursday"; break;
	case 5: String dayName = "Friday"; break;
	case 6: String dayName = "Saturday"; break;
}</pre>

<p>And, or course, if-else:</p>

<pre class="prettyprint">if (x == y) {
	doStuff();
} else if ( x &gt; y) {
	otherStuff();
} else {
	ohNoes();
}</pre>

<h1 id="classes">Classes and objects</h1>

<p>Classes are the blueprints for objects.</p>

<pre class="prettyprint">[access modifier] [return data type] [class name] [extends superclass] [implements interface] {
	[declarations]
	[methods]
}</pre>

<p>Example:</p>

<pre class="prettyprint">public class Bicycle {
	public int speed;
	public int gear;
	public Bicycle(int startSpeed, int startGear) { 
        // The constructor has the same name as the class.
		speed = startSpeed;
		gear = startGear;
	}
	public void setGear(int newGear) {
		gear = newGear;
	}
	public void faster(int i) {
		speed += i;
	}
	public void slower(int i) {
		speed -= i;
	}
}</pre>

<h2>Access modifiers</h2>

<p>Access modifiers enforce scope and enable encapsulation. They can be applied to variables or methods.</p>

<ul>
	<li><code class="prettyprint">public</code> accessible from all classes</li>
	<li><code class="prettyprint">private</code> accessible within its own class</li>
	<li><code class="prettyprint"></code></li>
</ul>

<h2>Class inheritance</h2>

<p>A class can have children which inherit thier parent class' methods and members.</p>

<pre class="prettyprint">public class BikeBuiltForTwo <b>extends</b> Bicycle {
	public boolean secondRider;
	public void addRider() {
		secondRider = true;
	}
}</pre>

<h2>Objects</h2>

<p>Object are instantiated by calling the constructor of the class:</p>

<pre class="prettyprint">Widget myWidget = new Widget("argument");</pre>

<h1 id="exceptionhandling">Exception handling</h1>

<pre class="prettyprint">try {
	// Do stuff.
} catch (Exception e) {
	// Do this on error.
} finally {
	// Do this, whether there's an error or not.
}</pre>

<h1 id="fileio">File IO</h1>

<p>Read a file:</p>

<pre class="prettyprint">String line = null;
try {
	BufferedReader input = new BufferedReader(new FileReader(inputFile));
	while ((line = input.readLine()) != null) {
		System.out.println(line);
}
	input.close();
} catch (Exception e) {
	System.err.println("Can't read input file: " + e.getMessage());
	System.exit(1);
}</pre>

<p>Write a file:</p>

<pre class="prettyprint">DataOutputStream output = new DataOutputStream(new FileOutputStream(outputFile.txt));
output.writeUTF(textOutput);
output.close();</pre>

<h1 id="jar">JAR files for archiving and distribution</h1>

<p>Java programs can be packaged as runnable, compressed JAR files for distribution.</p>

<ul>
	<li>Create a JAR file: <code class="prettyprint">jar cf jarfile inputfile1 inputfile2 inputdir</code></li>
	<li>View contents of JAR file: <code class="prettyprint">jar tf jarfile</code></li>
	<li>Extract all files from JAR: <code class="prettyprint">jar xf jarfile</code></li>
	<li>Update JAR (overwrites existing files with same names): <code class="prettyprint">jar uf jarfile1 jarfile2</code></li>
	<li>Run a JAR as an application: <code class="prettyprint">java -jar jarfile.jar</code></li>
</ul>

<p>Note that for a JAR file to be runnable, it have a manifest file to indicate where the main() method is located. A work-around to creating a manifest file is to specify the entry point with the -e flag when creating the JAR file: <code class="prettyprint">jar cfe jarfile.jar MyApp MyApp.class</code></p>

<h1 id="links">Links</h1>

<ul>
	<li><a href="http://java.sun.com/j2se/1.5.0/docs/api/">Sun Java API docs</a></li>
	<li><a href="http://java.sun.com/docs/books/tutorial/">Sun Java Tutorials</a></li>
	<li><a href=""></a></li>
</ul>

<?php include('../FOOTER.php'); ?>
