View Single Post
  #1 (permalink)  
Old 12-09-2006, 12:06 AM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 19
Posts: 2,815
Last Blog:
Passwords
Rep Power: 20
John has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud of
Send a message via AIM to John
Default Java:Tutorial - "Hello World"

Object:
To build your very first Java App

The Idea:
Like every first tutorial, this tutorial will show you how to display "Hello World," but in two ways. The first most simple way, will print "Hello World" to the console, the second (although I wont fully explain everything until later tutorials) will display "Hello World" using the OOP aspect of Java.

Prerequisites:
No Previous knowledge of Java is needed but you should have read this tutorial:
http://forum.codecall.net/tutorials-...g-started.html

The Tutorial:
1. Create a new class called MyFirstApp

java Code:
  1. package helloworld;
  2. public class MyFirstApp {
  3. }

2. Above is the basic structure of a class. The first line is the package declaration. It serves as a "folder" that holds a bunch of class's. The second line is the class declaration header. The next step is to create a constructor.

Code:
package helloworld;
public class MyFirstApp {
	public MyFirstApp(){
	}
}
3. The third line is the constructor declaration. It is started with an access modifier public/private and then followed by the name of the class with an closed set of parentheses. At this point we don't have much so lets get to the good stuff!

Code:
package helloworld;
public class MyFirstApp {
	public MyFirstApp(){
	System.out.println("Hello World!");
	}
}
4. At this point we have a fully functioning class. However we need to create a means of starting it. There are two ways, you could implement an applet or make it an application. At this point, lets not get hung up on the differences or the what the syntax means just know it works. In our example we will be using an application

Code:
package helloworld;
public class MyFirstApp {
	public MyFirstApp(){
	System.out.println("Hello World!");
	}
	public static void main(String[] args){
	new MyFirstApp();
	}
}
5. public static void main(String[] args){ is the method java looks for to initialize the application. The "new" creates a new instance of the class in the computers memory and viola your code works.

VIDEO TUTORIAL: http://www.extreme-hq.com/other/Vide...loWorldOne.wmv

However, printing words to the console can get pretty boring, to show you some of Javas capabilities, I will give you a brief introduction to working with swing. Although I wont explain in depth the reason behind each line of code as that is beyond the scope of this tutorial.

1. Create a class as shown above, but this time name it MySecondApp

2. Create a constructor and instantiate it in the main method. At this point we should have something like this:
Code:
package helloworld;

public class MySecondApp {
	
	public MySecondApp(){
	}
	public static void main(String[] args) {
		new MySecondApp();
	}
}
3. Next we are going to import the swing package that is avalable to us
Code:
package helloworld;

import javax.swing.*;

public class MySecondApp {
	
	public MySecondApp(){
	}
	
	public static void main(String[] args) {
		new MySecondApp();
	}

}
4. Next we are going to extend JFrame (which is a class in the swing package) so we can inherit its capabilities.

Java Code:
  1. package helloworld;
  2.  
  3. import javax.swing.*;
  4.  
  5. public class MySecondApp extends JFrame {
  6.    
  7.     public MySecondApp(){
  8.  
  9.     }
  10.     public static void main(String[] args) {
  11.         new MySecondApp();
  12.     }
  13. }

5. Finally we are going to create a window using the the JFrame methods. I will discuses these more in depth in later tutorials.
Java Code:
  1. package helloworld;
  2.  
  3. import javax.swing.*;
  4.  
  5. public class MySecondApp extends JFrame {
  6.    
  7.     public MySecondApp(){
  8.         setSize(150,60);
  9.         setLocation(20,20);
  10.         JLabel myLabel = new JLabel("Hello World!");
  11.         add(myLabel);
  12.         setVisible(true);
  13.     }
  14.     public static void main(String[] args) {
  15.         new MySecondApp();
  16.     }
  17.  
  18. }

VIDEO TUTORIAL: http://www.extreme-hq.com/other/Vide...loWorldTwo.wmv

And your done

Last edited by John; 08-29-2007 at 02:51 PM.
Reply With Quote

Sponsored Links