
By Yakov Fain | Article Rating: |
|
November 16, 2003 12:00 AM EST | Reads: |
89,505 |

Getting Started
The Java Development Kit (JDK) could be downloaded from the Sun Microsystems' Internet site at http://java.sun.com/j2se/1.4/ .
The installation process is pretty simple - just run the downloaded executable file and it'll install it on your disk (the default directory for Java under Microsoft Windows is c:\j2sdk1.4).
To start writing a Java program you could use any plain text editor. In Windows, it could be an editor called Notepad. In UNIX, it could be the vi editor. The files with Java programs must be saved in a plain text format and must have names ending in .java. For example, if you want to write a program called HelloWorld, enter its code in Notepad and save it in a class named HelloWorld.java.
Keep in mind that Java is a case sensitive language, which means that if you named the program HelloWorld with a capital H and a capital W, do not try to start the program helloworld.
Here is the infamous program that prints the words Hello World on the screen:
public class HelloWorld {
public static void main(String[] args){
System.out.println("Hello World");
}
}
Now you need to compile this program. We'll be using the javac compiler, which is a part of JDK.
Let's say you've saved your program in the directory called c:\practice. Open a command window, change the current directory to c:\practice and compile the program:
c:\>cd \practice
c:\practice>javac HelloWorld.java
If your environment is set properly and your program does not have syntax errors, it will create a new file called HelloWorld.class in the same directory.
If an error message is displayed saying something like "javac is not found", or "bad command/file name" make sure that the directory c:\j2sdk1.4\bin is included to the search path of your environment.
- If you are using Windows 98, open the file c:\autoexec.bat
and add the directory where your JDK is installed to the environment
variable PATH, for example
c:\j2sdk1.4\bin;
- In Windows 2000 or XP set the PATH using the menu Settings |
Control Panel | System | Environment Variables.
- In Unix - add it to the shell's PATH environment variable.
You won't see any confirmation of a successful compilation, just type dir in Windows or ls in Unix, and a new file named HelloWorld.class has to be there. This proves that your program has been successfully compiled.
If the program has some syntax errors, the compiler will print error messages. In this case you'd need to fix the errors, and recompile the program again. You may need to do it more than once until the file HelloWorld.class is created.
Now let's run the program - enter the following command:
c:\practice> java HelloWorld
Please note that we do not start javac, but java , which is called the Java run-time environment or the Java Virtual Machine (JVM).
This time the error message may say that the HelloWorld.class is not found. Even though you have the .class file in the same directory as your .java file, JVM is not going to look for it in the current directory unless the current directory is listed in the so-called CLASSPATH variable. Don't confuse this with the variable PATH, that's been discussed earlier.
The variable CLASSPATH variable is used by the JVM to find compiled classes. Let's do a procedure similar to what you've done with the PATH.
For example, in Windows 98, open the file autoexec.bat and add the following line to it:
set CLASSPATH=.;
The dot above represents the current directory. If you already had the CLASSPATH variables set in your machine, just add the dot and semicolon to the end of its value.
Give your Java class and its file the same name. There could be exceptions to this rule, but not in this simple program.
While writing Java programs, you create classes which represent objects from real life. You'll learn more about classes in the lesson called "Introduction to Object-Oriented Programming in Java".
Our HelloWorld program is also a class and it contains a method main(). Methods in Java classes represent actions that the class could perform. The method main() calls the method println() to display the text "Hello World" on the screen.
Here is the method signature of the method main():
public static void main(String[] args)
The method signature includes the access level - public, instructions on usage - static, return value type - void, name of the method - main, and the argument list - String[] args.
The keyword public means that the method main() could be accessed by any other Java class. The keyword static means that you don't have to create an instance of this class to use this method. The keyword void says that the method main() doesn't return any value to the calling program.
The keyword Stirng[] args tells us that this method will receive an array of Strings as the argument (some values could be passed to this method from a command line).
The main() method is the starting point of your program. You can have a program that consists of more that one class, but at least one of them usually has the method main(), otherwise the program will not start. A Java class can have more than one method. For example, a class Employee can have the methods updateAddress(), raiseSalary(),changeName(), etc.
The body of the method main()contains the following line :
System.out.println("Hello World");
The println() is a method that is used to print data on the system console (command window). Java's method names are always followed by parentheses.
System and out are not methods, but names that represent other Java classes.
System.out means that the variable out is defined inside the class System.
The out.println() tells us that there is an object represented by a variable called out and it has a method called println().
We will be using this so-called dot notation to access class methods or variables. Say you have a class Employee that has a method changeAddress(). Here is an example:
Employee.changeAddress("25 Broadway")
Let's review the steps you would perform to create and run the HelloWorld program:
Step 1. Set the values for the PATH and CLASSPATH system variables.
Step 2. Create a new directory called practice.
Step 3. Using a text editor, enter the code of the class
HelloWorld and save it in the file
c:\practice\HelloWorld.java.
Step 4. Compile and run the program:
c:\practice> javac HelloWorld.java
c:\practice> java HelloWorld
Assignment. Write a program to print your address using more than one statement println().
Published November 16, 2003 Reads 89,505
Copyright © 2003 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
- Intro to Object-Oriented Programming with Java
- Methods, Constructors, Overloading and Access Levels
- Java Exceptions
- Java Streams Basics
- Reading Data from the Internet
- Java Serialization - Lesson 7
- Java Serialization
- Teaching Kids Programming: Even Younger Kids Can Learn Java
- Taking a Sun Java Studio Creator for a Drive
- Java Basics: Introduction to Java Threads, Part 1
- Java Gotchas: Instance Variables Hiding
- Java Basics: Introduction to Java Threads, Part 2
- SYS-CON Webcast: Eclipse IDE for Students, Useful Eclipse Tips & Tricks
- Java Basics: Lesson 11, Java Packages and Imports (Live Video Education)
More Stories By Yakov Fain
Yakov Fain is a Java Champion and a co-founder of the IT consultancy Farata Systems and the product company SuranceBay. He wrote a thousand blogs (http://yakovfain.com) and several books about software development. Yakov authored and co-authored such books as "Angular 2 Development with TypeScript", "Java 24-Hour Trainer", and "Enterprise Web Development". His Twitter tag is @yfain
![]() |
Peter Sweet 04/23/04 11:53:44 AM EDT | |||
This is good stuff. The explanation of what PATH and CLASSPATH do was useful. |
![]() |
Andrey Postoyanets 11/19/03 11:42:21 AM EST | |||
As a Java programmer in a college IT department, I''ve worked with quite a few college interns (and also have been approached by fellow co-workers who want to try Java.) They would usually take a college-level Java course or try self-study, but are quite scared to start on their own. Thus, the usual question is "What should I do first?" Many of them expect some magic IDE and are quite surprised with an answer that a pure Notepad would be enough :) |
![]() |
Yakov FaAin 11/18/03 04:25:54 PM EST | |||
Hi J.D, I agree with you, object-oriented nature is important. But if I start with explaining OOP to people with different backgrounds, I''ll lose a half of my students right away. Guess what? I usually do this on the lesson #2 :) Regards, |
![]() |
J.D. Baker 11/18/03 11:53:06 AM EST | |||
What''s here is ok, but it ignores the object-oriented nature of the Java programming language. IMHO, the proper approach to Hello World in Java is a version published by Shengyang Shong (I forget where). This version of Hello World had a class with a main called SayHello that instantiated a Mouth that had a say() method that printed "hello". This example can be expanded, introducing constructors for the Mouth and overloaded say() methods. J.D. |
![]() Apr. 19, 2018 11:00 AM EDT Reads: 5,273 |
By Liz McMillan ![]() Apr. 19, 2018 11:00 AM EDT Reads: 6,951 |
By Elizabeth White ![]() Apr. 19, 2018 10:45 AM EDT Reads: 4,371 |
By Yeshim Deniz Apr. 19, 2018 10:00 AM EDT Reads: 4,714 |
By Pat Romanski ![]() Apr. 19, 2018 09:45 AM EDT Reads: 5,124 |
By Elizabeth White Apr. 19, 2018 09:45 AM EDT Reads: 4,104 |
By Liz McMillan ![]() Apr. 19, 2018 09:15 AM EDT Reads: 3,478 |
By Yeshim Deniz Apr. 19, 2018 08:45 AM EDT Reads: 2,180 |
By Yeshim Deniz ![]() Apr. 19, 2018 08:30 AM EDT Reads: 1,791 |
By Yeshim Deniz Apr. 19, 2018 08:30 AM EDT Reads: 2,945 |
By Liz McMillan Apr. 19, 2018 08:15 AM EDT Reads: 2,394 |
By Liz McMillan ![]() Apr. 19, 2018 08:15 AM EDT Reads: 7,787 |
By Yeshim Deniz Apr. 19, 2018 08:00 AM EDT Reads: 2,948 |
By Yeshim Deniz Apr. 19, 2018 07:45 AM EDT Reads: 2,402 |
By Liz McMillan ![]() Apr. 19, 2018 07:00 AM EDT Reads: 1,408 |