web stats

Java for Beginners

I whole heartedly recommend AGAINST IDE’s for a beginner programmer. They make the whole experience a mess, teach bad practices (sloppy programming) and worst of all perform “magic”. What do I mean by magic? Well “To compile this program click this and that then press f5, MAGIC!”.

IDE’s are professional tools for people who know what they are doing. It’s like giving a MIG welder to a toddler.

But, that’s just me.

Here is my guide:
1) Install the Java Development Kit (JDK) so that you can run the java compiler from a terminal (cmd in windows). This is trivial on linux, just find it in your package manager. I think it’s already there on OSX and for Windows, well… urgh, just google it. You will know it’s installed when you can run javac (the java compiler).

Code:
$ javac

It should give you some usage guide because you haven’t gave it any arguments.

2) Write your program in a plain text editor. Gedit, TextEdit, Vim, Notepad++. Anything with syntax highlighting really, it will really help (most plain text editors have syntax highlighting unless you’re using something like notepad or something. WHY THE FUCK ARE YOU USING NOTEPAD?!

Here is a sample program to get you started (call it HelloWorld.java):

Code:
public class HelloWorld
{
  public static void main(String[] args)
  {
    System.out.println("Hello World!");
  }
}

3) Compile it with the java compiler

Code:
$ javac HelloWorld.java

4) That should give you a HelloWorld.class file. You can then run the java virtual machine and pass it your class name (HelloWorld). It will search for that class in the current directory (HelloWorld.class), build and object from it and then run it:

Code:
$ java HelloWorld
Hello World!

YAY!

Summary:
1) Install JDK
2) Write a program (a .java source file)
3) Compile it (javac <source file>)
4) Run it (java <class name>)

This is the ‘real’ way. Using an IDE is for when you’re writing something like an Operating System or a mass market game in a team of 30.

Virtual Machine? WTF?!
A C source file gets compiled into a pure binary (machine code) that runs on your CPU, if you moved this to your phone/other OS, it probably wouldn’t work. Java source code gets compiled into byte code and runs on a virtual machine. You can think of byte code as machine code (binary) for a special CPU, the JVM. Everyone has the same virtual machine installed which means a java program on your computer will also run on your phone, your friends mac, your nans windows and whatever other device has the JVM installed on it.

Code explanation

Code:
public class HelloWorld
{
  public static void main(String[] args)
  {
    System.out.println("Hello World!");
  }
}

“class” tells the compiler that the thing between the next two braces is the HelloWorld class.

“public” means that people outside this class can access the things in it. Sometimes your compiler will complain if it’s not there because the JVM can’t access it. On my machine it compiled fine without though… weird, just put it there anyway.

When you invoke the JVM, it looks for a method (a fancy name for a function in Object Oriented Programming), called main. Every method must have a return type and if you like, some arguments and modifiers. For example:

Code:
int fuckingAwesomeMethod()
{
  System.out.println("FUCK YEAH!");
  return 5;
}

String giveMeAWordOfSize(int size)
{
  if (size == 5)
  {
    return "penis";
  }
  else if (size == "3")
  {
    return "lol";
  }
  else
  {
    return "";
  }
}

void derp()
{
  System.out.println("derp");
}

The method fuckingAwesomeMethod() has no arguments, prints “FUCK YEAH!” and then gives back 5.
The method giveMeAWordOfSize() takes an integer argument and returns a string.
The method derp() prints derp and has no return type or arguments. A “return;” command is implicitly places at the end of the function by the compiler.

Anyway, the JVM looks for a method called main and passes it an array of strings. You can call this array of strings whatever you like but “args” is the standard (arguments). This is an array containing each of the arguments you gave your java program:

Code:
$ java MyProgram cat dog sheep

In the above; args[0] is cat, args[1] is dog and args[2] is sheep.

The main method also have some modifiers;
public – the function can be accessed from outside of this class (I.e. by the JVM)
static – everything in the method exists in memory for the lifetime of this program

The System.out.println() method is a method which is inside the ‘out’ class (object at run time), which is in the System library. It takes a string as an argument and prints it, followed by a new line. Fun Fact: There are actually two methods called println; one that takes an argument and one that doesn’t. The one that doesn’t just prints a new line. Also, the method print() prints without a new line character.

You have to end each line with a semicolon because when the compiler looks at your code, all of the white space is removed. You can even type the whole program on one line! Don’t. Why? Source code is read by humans. Once you compile your program the computer doesn’t see your source at all. Comment and use lots of white space. It will make someone else’s life better . Use good variable names too.

Some other tips
You only need braces for more than one line. So see those if/else statements above with the braces? Well I could of just wrote:

Code:
if (size == 5)
  return "penis";
else if (size == "3")
  return "lol";
else
  return "";

Also, note how there is a space between the else and the if. Java has no elseif statement so what I wrote is essentially:

Code:
if (size == 5)
  return "penis";
else
{
  if (size == "3")
    return "lol";
  else
    return "";
}

But remember how if it’s only one line then we can ignore the braces? Well the WHOLE if statement is one line . We write it like this for clarity:

Code:
if (size == 5)
  return "penis";
else if (size == "3")
  return "lol";
else
  return "";

Pretty neat, eh?

Your arguments are Strings. If you want to pass in integers you just parse the string as an integer first:

Code:
public static void main(String[] args)
{
  int age = args[0];                    // WRONG!
  int age = Integer.parseInt(args[0]);  // CORRECT
}

Please ask questions in this thread. We can get some pretty sweet knowledge base going on

Discuss http://www.totse.info/bbs/showthread.php?t=11091

Leave a Reply