Your First Java program - Part 1 - Hello World

Wednesday, December 12, 2007 | Labels: | |

This series is not yet finished. Stay tuned!

Welcome to Object Oriented Programming (OOP). When I used to read tutorials, I hated long introductions where the author rambled on about his biography, precautions, etc. Most of those junk are actually useless. So for this tutorial, I’m going to get right to the point.

We are going to make a simple program today. It will give you a feel of Java, and object oriented programming. Be sure to read every word, for every word is important.

To start, you need an Integrated Development Environment (IDE), which is a program where you can write code in. Why don’t just use notepad you say? Because with an IDE, it compiles and catches errors for you; it is much easier.

For starters, there are many free IDEs such as Eclipse, JCreator, and NetBeans. The code I will talk about here will work for all these IDEs, you simply have to copy them in and press the “compile” button, wherever it is in your IDE.

After installing the IDE, let’s play with some code :)

public class HelloWorld
{
public static void main (String[] args)
{
System.out.println("Hello World");
}
}
What this did: public class HelloWorld creates a new object called "HelloWorld" (the class is an object). Inside the object, we created a method called "main", this is by default what the program runs no matter what other methods you put into the class. Inside main, we use the system console to print out a line on the screen, "Hello World."
When we defined the main method, we can see that it is public: any other object can access this (other choices are private and protected) it is also static: an easy way to explain this would be that you can't use "this" command inside the method... we are going to talk about this later. It is also a void: it returns nothing (other options can be anything, we are going to talk about returning stuff later). The Strings[] args inside the brackets are the method's arguments, or the parameter by which it must be ran.

Sorry, a lot of thing I'm leaving to explain later, because they are truly beyond you right now.

Find your compile button on your screen and compile the code. You will see a single line on the screen, nothing too exciting -_-'

To make things funner, we can make a console. Proceed to Part 2.

0 comments:

 
Free URL Redirection @ .co.nr