IT Community - Software Programming, Web Development and Technical Support

Java:Tutorial - "Hello World"

This is a discussion on Java:Tutorial - "Hello World" within the Java Programming forums, part of the Software Development category; Object: To build your very first Java App The Idea: Like every first tutorial, this tutorial will show you how ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > Java Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 02-23-2007, 09:54 AM
pranky pranky is offline
D-Web Programmer
 
Join Date: Feb 2007
Posts: 51
pranky is on a distinguished road
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

Code:

package helloworld;
public class MyFirstApp {
}

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.

Code:

package helloworld;

import javax.swing.*;

public class MySecondApp extends JFrame {

public MySecondApp(){

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

5. Finally we are going to create a window using the the JFrame methods. I will discuses these more in depth in later tutorials.
Code:

package helloworld;

import javax.swing.*;

public class MySecondApp extends JFrame {

public MySecondApp(){
setSize(150,60);
setLocation(20,20);
JLabel myLabel = new JLabel("Hello World!");
add(myLabel);
setVisible(true);
}
public static void main(String[] args) {
new MySecondApp();
}

}

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

And your done
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
I keep getting "Data Missing" when I click the "back" button in my browser. How can I oxygen HTML, CSS and Javascript Coding Techniques 1 07-28-2007 01:12 AM
Diffrence between a "assignment operator" and a "copy constructor" Sabari C and C++ Programming 1 07-24-2007 05:00 AM
Write any small program that will compile in "C" but not in "C++" Sabari C and C++ Programming 1 07-24-2007 04:48 AM
Why do I get "HTTP 500" error(or "(DLL)initialization routine failed")in my browser? kingmaker ASP and ASP.NET Programming 1 07-20-2007 04:38 AM
Difference between a "assignment operator" and a "copy constructor"? vigneshgets C and C++ Programming 2 07-12-2007 05:30 AM


All times are GMT -7. The time now is 11:20 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0