My First Java Program

Tue, 10 Aug 2010

I decided to learn some Java in my free time and I picked up a book titled "Sun Certified Programmer for Java 5". Obviously I'm far from being certified but it will be a great study guide. I find Java quite a bit more difficult than PHP OOP, I suppose because of the forced type-casting, and things are just run in a stricter way, and the fact that I'm new to it, but I don't mind!

I'm having a tough time figuring out an application to build to help me learn. I haven't found many good resources so I can learn by examining others code yet. Anyways, here is my first program which does nothing impressive!

public class Test {

    String name;

    
    public static void main(String[] args)
    {
		Test t = new Test();
		t.doPrint("Kitten, first application!");
    }

    void doPrint(String val)
    {
		System.out.println("Hey so this is a ... " + val);
    }

}

It took me a half hour to figure out how to call a function from a class, it appears you have to create (this Class) inside of the constructor (main, I think) and use the variable reference from that point on.

And finally a cheezy console output of a value, I think from the manual I'd have to apply some "Overloading" for the method in order to accept string and integers, however I could just force the value to be output as a string and that would accept digits also.

// For the input
import java.util.Scanner;

public class Test {

	int val;
	
	public static void main(String[] args)
	{

		// Construct this class
		Test self = new Test();

		// Setup the Scanner
		Scanner in = new Scanner(System.in);

		// Set val to the Scanners nextInt
		self.val = in.nextInt();
		
		// Close the Util
		in.close();
		
		self.Output();
	}

	void Output()
	{
		System.out.println("You inserted: " + this.val);
	}

}