Get into Java Part 1: Hello World

27. July 2009

Forward
Two years ago I developed a number of Java applications. Since then I have been developing exclusively in .Net. I just got a project requiring Java development, and it occurred to me that my Java skills are lacking since my Java prime. This is a series of tutorials designed as a refresher course.

Overview
The intention is to remove the base concepts of object oriented programming and patterns bringing how to develop in the language forward. It is designed for previous developers of Java and advanced developers in other languages trying to jump into Java.

Tools
At the time of this writing Java 6 revision 14 JRE is out. For those that don’t know the Java Runtime Environment (JRE) runs on your machine and executes Java applications. The Java Development Kit (JDK) is the development tools for writing Java applications. Eclipse is the primary IDE for Java and will contain the JDK.

 

Hello World
I’m going to assume you are using eclipse for now. If you would like to compile projects by hand the javac application in the jdk is how you would go about it. Lets get started.

  • Launch eclipse
  • Click File->New Java Project
  • Project Name: HelloWorld
  • Click Finish

 

This created your application’s directory structure. Now we want to add the actual HelloWorld class.

  • Click on the arrow next to HelloWorld in the Package Explorer to open the directory tree.
  • Right click on src
  • Click New->Class
  • Under Package, enter com.cyberkruz
  • Under Name, enter HelloWorld
  • Under “Which method stubs would you like to create?” check “public static void main(String[] args)”
  • Click Finish

 

Change your code to the following

package com.cyberkruz;

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

Up at the top there is a green button with an arrow, click it and your Console (down below) should say “Hello World.” Success!

Explanation

  • package – Defines that the application is located in the com.cyberkruz package. This is used for sorting your code. It is basically the same as a namespace in .Net (I know there are differences, don’t flame me). When the code is organized, it is placed in the corresponding folder structure according to the package.
  • class – The basis for oop. Not going into oop this is how you define a class in Java. The public keyword defines the access level. Here are the access levels:
    • public – class, package, subclass, world
    • protected – class, package, subclass
    • no modifier – class, package
    • private – class
  • static – The static keyword denotes that the method isn’t called from an instance of the class.

 

So we defined a package com.cyberkruz. We added a class to the package that is visible to the world. We added a method taking in a string array of arguments that can be passed from command line. We then accessed the System.out class and the println method which prints a string to the command line.

I realize this is incomplete. The intention is to refresh memory and help people jump into Java code without a lot of hassle. More tutorials in this series will be posted soon.



Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Java

Comments

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading