I’ve never been too fond of books when trying to learn a language for the first time. The reason is that they give you too many details in the beginning that you may not necessarily be interested in until later on. The approach that I’ve found to be very useful in learning or teaching is to let the user/student get an overview first and then go into how things actually work. So, in this article we are going to write a simple application/program and run it. As we move on, I will explain what is going on.
Hello World
For those who don’t know, Hello world is a very popular program in the computing world. Almost every language’s tutorial has one for beginners. All it does is print Hello world on the screen in one form or another. This is what we will do too. We will write a program that will print “Hello world” in your console window.
Create a folder in your c:\ drive and name it java_projects. This is where we will place all Java files.
Open notepad and type this in it :
import java.io.*; public class HelloWorld { public static void main(String[] args) { System.out.println("Hello world"); } }
After copying and pasting the above code in your notepad window, save the file as HelloWorld.java in c:\java_projects\ . When you save it, make sure that file type is selected to “All files” or notepad will save your file as “HelloWorld.java.txt” and not “HelloWorld.java” .
Oh, and you will have to save the file as HelloWorld.java (with the capital letters) or the code won’t compile. I will get to the reason in later articles.
We have written the code. Now, we need to compile and run it. We will do that by opening the command prompt (also referred to as the console) and do it from there. Click on start then on Run and type cmd in the dialog box that opens up.
A black window will appear with a blinking cursor. It is the command prompt. Type this in it :
cd c:\java_projects\
Then type :
javac HelloWorld.java
The screen will get stuck for a few seconds and then you will see a blinking cursor again. javac is used to compile java code into byte-code. If you go to c:\java_projects\ in windows explorer, you will see that there javac created a file called HelloWorld.class . This is the byte-code file that the command “java” needs to run the application. You can run your code by typing :
java HelloWorld
A line of text saying Hello world should appear in your console. Congratulations ! you just ran your first application.
Leave a Reply