Difference between revisions of "Hello World"

From CompSciWiki
Jump to: navigation, search
(added a bunch of words. they should be mostly in the right order.)
Line 3: Line 3:
 
|ProblemName=Hello World
 
|ProblemName=Hello World
  
|Problem=The problem
+
|Problem=The [http://en.wikipedia.org/wiki/Hello_world Hello World] problem has been around almost as long as modern programming has. Traditionally, when trying a new programming language or technology, developers and students like to make their new medium say hello. Because of this, the Hello World problem will be your first Java application.
  
|Solution=The solution...
+
When run, this application will display a message box saying "Hello World!".
 +
 
 +
|Solution=This solution is in three steps:
 +
* Import the JOptionPane library
 +
* Create the HelloWorld class and main method
 +
* Use JOptionPane's showMessageDialog to display a message.
 +
 
 +
To start, in a blank .java file import the JOptionPane library. Do this by typing the following:
 +
<pre>import javax.swing.JOptionPane;</pre>
 +
This tells Java that when we later use the showMessageDialog method, it knows where to look for it.
 +
 
 +
Next, create a Hello World java class. In your .java file, under the import statement, create a public class called "HelloWorld" and a standard main method within the class.
 +
<pre>public class HelloWorld
 +
{
 +
public static void main(String[] args)
 +
{
 +
}
 +
}</pre>
 +
 
 +
Now you are ready to create the body of your program. From the JOptionPane library you imported earlier, refrence the showMessageDialog method. This method takes two parameters, a Parent Object and a String object. We don't need to worry about a Parent object for this problem so we can just pass the method null instead. As for the String, this is where you will write your message.
 +
<pre>JOptionPane.showMessageDialog(null, "Hello World!");</pre>
 +
 
 +
Lastly, compile and run your application. Congratulations! You've written your first Java application. It doesn't do much for now but don't worry, you'll learn much more later. Also note that it's a good idea to keep your HelloWorld.java file around since it's a convenient template for future Java applications you might write.
  
 
|SolutionCode=<pre>import javax.swing.JOptionPane;
 
|SolutionCode=<pre>import javax.swing.JOptionPane;

Revision as of 19:30, 6 April 2010

Back to the Program-A-Day homepage

Problem

The Hello World problem has been around almost as long as modern programming has. Traditionally, when trying a new programming language or technology, developers and students like to make their new medium say hello. Because of this, the Hello World problem will be your first Java application.

When run, this application will display a message box saying "Hello World!".

 

Getting Started

Wiki lockers01.jpg

Solution

This solution is in three steps:

  • Import the JOptionPane library
  • Create the HelloWorld class and main method
  • Use JOptionPane's showMessageDialog to display a message.

To start, in a blank .java file import the JOptionPane library. Do this by typing the following:

import javax.swing.JOptionPane;

This tells Java that when we later use the showMessageDialog method, it knows where to look for it.

Next, create a Hello World java class. In your .java file, under the import statement, create a public class called "HelloWorld" and a standard main method within the class.

public class HelloWorld
{
	public static void main(String[] args)
	{
	}
}

Now you are ready to create the body of your program. From the JOptionPane library you imported earlier, refrence the showMessageDialog method. This method takes two parameters, a Parent Object and a String object. We don't need to worry about a Parent object for this problem so we can just pass the method null instead. As for the String, this is where you will write your message.

JOptionPane.showMessageDialog(null, "Hello World!");

Lastly, compile and run your application. Congratulations! You've written your first Java application. It doesn't do much for now but don't worry, you'll learn much more later. Also note that it's a good idea to keep your HelloWorld.java file around since it's a convenient template for future Java applications you might write.

Code

Solution Code

Back to the Program-A-Day homepage