Java

Java is a statically typed object oriented programming language. It was designed as a language for cross platform application development.

Java is generally compiled to bytecode that is then interpreted by the Java Virtual Machine. This allows the same bytecode to run on multiple platforms. The HotSpot JVM may also compile bytecode to native machine code for additional performance.

Other languages such as JRuby and Clojure can also run on top of the JVM, which makes it an interesting platform for programming language development.

Why would I learn this language?

Java is a great language because it aims to provides a "write once, run anywhere" environment. While in practice, there are always additional complexities, Java code is generally cross platform.

Java provides a highly structured environment which tends to promote good practices when it comes to software development, especially when more than one person is working on the source code.

Java is not a purely object oriented language, because it has primitive types (which are not objects). This creates imperfections in both syntactic and semantic constructs within the language.

Due to the nature of all Java, code must always be defined within the scope of a class. This can requires a large amount of boiler plate code in certain scenarios.

Starting Points

Learning Java - Resources
A fantastic set of resources for different levels of programmers.
New to Java Programming Center
A set of resources for new programmers who want to understand the different capabilities of Java.
The Java Tutorials
Practical guide for programmers who want to use the Java programming language.

Example Code

The following is an example of the Fizz Buzz problem. You can run and edit this progam here.

public class Main
{
    public static void main (String[] args)
    {
        for (int i= 1; i <= 100; i++)
        {
            if (i % 3 == 0 && i % 5 == 0) {
                System.out.println("FizzBuzz");
            } else if (i % 3 == 0) {
                System.out.println("Fizz");
            } else if (i % 5 == 0) {
                System.out.println("Buzz");
            } else {
                System.out.println(i);
            }
        }
    }
}

The following is an example of the 100 doors problem. You can run and edit this progam here.

// To compile and run: javac Doors.java && java Doors

public class Doors {
	void doors (int n) {
		// Initialize an array of 100 boolean values (Java initializes to 0 by default)
		boolean[] doors = new boolean[n];
		
		// Process the doors
		for (int pass = 0; pass < n; pass++) {
			for(int door = pass; door < n; door += pass + 1){
				doors[door] = !doors[door];
			}
		}
		
		// Print out the results
		for (int i = 0; i < n; i++) {
		 	System.out.println("Door #" + (i + 1) + " is " + (doors[i] ? "open." : "closed."));
		}
	}
	
	public static void main (String[] args) {
		// Create a new Doors object
		Doors d = new Doors();
		
		// Call the doors method with n = 100
		d.doors(100);
	}
}

Further Reading

comments powered by Disqus