How Java Works - Notes, Computer Engineering, Semester, Marshall Brain Notes | EduRev

: How Java Works - Notes, Computer Engineering, Semester, Marshall Brain Notes | EduRev

 Page 1


How Java Works 
by Marshall Brain
Have you ever wondered how computer programs work? Have you ever wanted to learn how to 
write your own computer programs? Whether you are 14 years old and hoping to learn how to 
write your first game, or you are 70 years old and have been curious about computer 
programming for 20 years, this article is for you. In this edition of HowStuffWorks, I'm going to 
teach you how computer programs work by teaching you how to program in the Java 
programming language.  
In order to teach you about computer programming, I am going to make several assumptions 
from the start:  
• I am going to assume that you know nothing about computer programming now. If you 
already know something then the first part of this article will seem elementary to you. 
Please feel free to skip forward until you get to something you don't know.  
• I am going to assume you do know something about the computer you are using. That is, 
I am going to assume you already know how to edit a file, copy and delete files, rename 
files, find information on your system, etc.  
• For simplicity, I am going to assume that you are using a machine running Windows 95, 
98, 2000, NT or XP. It should be relatively straightforward for people running other 
operating systems to map the concepts over to those.  
• I am going to assume that you have a desire to learn.  
All of the tools you need to start programming in Java are widely available on the Web for free. 
There is also a huge amount of educational material for Java available on the Web, so once you 
finish this article you can easily go learn more to advance your skills. You can learn Java 
programming here without spending any money on compilers, development environments, 
reading materials, etc. Once you learn Java it is easy to learn other languages, so this is a good 
place to start.  
Having said these things, we are ready to go. Let's get started!  
A Little Terminology 
Keep in mind that I am assuming that you know nothing about programming. Here are several 
vocabulary terms that will make things understandable:  
• Computer program - A computer program is a set of instructions that tell a computer 
exactly what to do. The instructions might tell the computer to add up a set of numbers, or 
compare two numbers and make a decision based on the result, or whatever. But a 
computer program is simply a set of instructions for the computer, like a recipe is a set of 
instructions for a cook or musical notes are a set of instructions for a musician. The 
computer follows your instructions exactly and in the process does something useful -- 
like balancing a checkbook or displaying a game on the screen or implementing a word 
processor.  
• Programming language - In order for a computer to recognize the instructions you give 
it, those instructions need to be written in a language the computer understands -- a 
programming language. There are many computer programming languages -- Fortran, 
Cobol, Basic, Pascal, C, C++, Java, Perl -- just like there are many spoken languages. 
They all express approximately the same concepts in different ways.  
• Compiler - A compiler translates a computer program written in a human-readable 
computer language (like Java) into a form that a computer can execute. You have 
probably seen EXE files on your computer. These EXE files are the output of compilers. 
They contain executables -- machine-readable programs translated from human-
readable programs.  
Page 2


How Java Works 
by Marshall Brain
Have you ever wondered how computer programs work? Have you ever wanted to learn how to 
write your own computer programs? Whether you are 14 years old and hoping to learn how to 
write your first game, or you are 70 years old and have been curious about computer 
programming for 20 years, this article is for you. In this edition of HowStuffWorks, I'm going to 
teach you how computer programs work by teaching you how to program in the Java 
programming language.  
In order to teach you about computer programming, I am going to make several assumptions 
from the start:  
• I am going to assume that you know nothing about computer programming now. If you 
already know something then the first part of this article will seem elementary to you. 
Please feel free to skip forward until you get to something you don't know.  
• I am going to assume you do know something about the computer you are using. That is, 
I am going to assume you already know how to edit a file, copy and delete files, rename 
files, find information on your system, etc.  
• For simplicity, I am going to assume that you are using a machine running Windows 95, 
98, 2000, NT or XP. It should be relatively straightforward for people running other 
operating systems to map the concepts over to those.  
• I am going to assume that you have a desire to learn.  
All of the tools you need to start programming in Java are widely available on the Web for free. 
There is also a huge amount of educational material for Java available on the Web, so once you 
finish this article you can easily go learn more to advance your skills. You can learn Java 
programming here without spending any money on compilers, development environments, 
reading materials, etc. Once you learn Java it is easy to learn other languages, so this is a good 
place to start.  
Having said these things, we are ready to go. Let's get started!  
A Little Terminology 
Keep in mind that I am assuming that you know nothing about programming. Here are several 
vocabulary terms that will make things understandable:  
• Computer program - A computer program is a set of instructions that tell a computer 
exactly what to do. The instructions might tell the computer to add up a set of numbers, or 
compare two numbers and make a decision based on the result, or whatever. But a 
computer program is simply a set of instructions for the computer, like a recipe is a set of 
instructions for a cook or musical notes are a set of instructions for a musician. The 
computer follows your instructions exactly and in the process does something useful -- 
like balancing a checkbook or displaying a game on the screen or implementing a word 
processor.  
• Programming language - In order for a computer to recognize the instructions you give 
it, those instructions need to be written in a language the computer understands -- a 
programming language. There are many computer programming languages -- Fortran, 
Cobol, Basic, Pascal, C, C++, Java, Perl -- just like there are many spoken languages. 
They all express approximately the same concepts in different ways.  
• Compiler - A compiler translates a computer program written in a human-readable 
computer language (like Java) into a form that a computer can execute. You have 
probably seen EXE files on your computer. These EXE files are the output of compilers. 
They contain executables -- machine-readable programs translated from human-
readable programs.  
In order for you to start writing computer programs in a programming language called Java, you 
need a compiler for the Java language. The next section guides you through the process of 
downloading and installing a compiler. Once you have a compiler, we can get started. This 
process is going to take several hours, much of that time being download time for several large 
files. You are also going to need about 40 megabytes of free disk space (make sure you have the 
space available before you get started).  
Downloading the Java Compiler 
In order to get a Java development environment set up on your machine -- you "develop" (write) 
computer programs using a "development environment" -- you will have to complete the following 
steps:  
1. Download a large file containing the Java development environment (the compiler and 
other tools).  
2. Download a large file containing the Java documentation.  
3. If you do not already have WinZip (or an equivalent) on your machine, you will need to 
download a large file containing WinZip and install it.  
4. Install the Java development environment.  
5. Install the documentation.  
6. Adjust several environment variables.  
7. Test everything out.  
Before getting started, it would make things easier if you create a new directory in your temp 
directory to hold the files we are about to download. We will call this the download directory.  
Step 1 - Download the Java development environment 
Go to the page http://java.sun.com/j2se/1.4.1/download.html. Download the SDK software by 
selecting your operating system and clicking the link on the next page. You will be shown a 
licensing agreement. Click Accept. Download the file to your download directory. This is a huge 
file -- almost 35 megabytes -- and it will take several hours to download over a normal phone-line 
modem. The next two files are also large.  
Step 2 - Download the Java documentation 
Download the documentation by selecting your operating system and clicking the SDK 1.4.1 
documentation link.  
Step 3 - Download and install WinZip 
If you do not have a version of WinZip or an equivalent on your machine, go to the page 
http://www.winzip.com/ and download an evaluation copy of WinZip. Run the EXE you get to 
install it. We will use it in a moment to install the documentation.  
Step 4 - Install the development kit 
Run the j2sdk-1_4_1-*.exe file that you downloaded in step 1. It will unpack and install the 
development kit automatically.  
Step 5 - Install the documentation 
Read the installation instructions for the documentation. They will instruct you to move the 
documentation file to same directory as that containing the development kit you just installed. 
Unzip the documentation and it will drop into the proper place.  
Step 6 - Adjust your environment 
As instructed on this page, you need to change your path variable. This is most easily done by 
opening an MS-DOS prompt and typing PATH to see what the path is set to currently. Then open 
autoexec.bat in Notepad and make the changes to PATH specified in the instructions.  
Page 3


How Java Works 
by Marshall Brain
Have you ever wondered how computer programs work? Have you ever wanted to learn how to 
write your own computer programs? Whether you are 14 years old and hoping to learn how to 
write your first game, or you are 70 years old and have been curious about computer 
programming for 20 years, this article is for you. In this edition of HowStuffWorks, I'm going to 
teach you how computer programs work by teaching you how to program in the Java 
programming language.  
In order to teach you about computer programming, I am going to make several assumptions 
from the start:  
• I am going to assume that you know nothing about computer programming now. If you 
already know something then the first part of this article will seem elementary to you. 
Please feel free to skip forward until you get to something you don't know.  
• I am going to assume you do know something about the computer you are using. That is, 
I am going to assume you already know how to edit a file, copy and delete files, rename 
files, find information on your system, etc.  
• For simplicity, I am going to assume that you are using a machine running Windows 95, 
98, 2000, NT or XP. It should be relatively straightforward for people running other 
operating systems to map the concepts over to those.  
• I am going to assume that you have a desire to learn.  
All of the tools you need to start programming in Java are widely available on the Web for free. 
There is also a huge amount of educational material for Java available on the Web, so once you 
finish this article you can easily go learn more to advance your skills. You can learn Java 
programming here without spending any money on compilers, development environments, 
reading materials, etc. Once you learn Java it is easy to learn other languages, so this is a good 
place to start.  
Having said these things, we are ready to go. Let's get started!  
A Little Terminology 
Keep in mind that I am assuming that you know nothing about programming. Here are several 
vocabulary terms that will make things understandable:  
• Computer program - A computer program is a set of instructions that tell a computer 
exactly what to do. The instructions might tell the computer to add up a set of numbers, or 
compare two numbers and make a decision based on the result, or whatever. But a 
computer program is simply a set of instructions for the computer, like a recipe is a set of 
instructions for a cook or musical notes are a set of instructions for a musician. The 
computer follows your instructions exactly and in the process does something useful -- 
like balancing a checkbook or displaying a game on the screen or implementing a word 
processor.  
• Programming language - In order for a computer to recognize the instructions you give 
it, those instructions need to be written in a language the computer understands -- a 
programming language. There are many computer programming languages -- Fortran, 
Cobol, Basic, Pascal, C, C++, Java, Perl -- just like there are many spoken languages. 
They all express approximately the same concepts in different ways.  
• Compiler - A compiler translates a computer program written in a human-readable 
computer language (like Java) into a form that a computer can execute. You have 
probably seen EXE files on your computer. These EXE files are the output of compilers. 
They contain executables -- machine-readable programs translated from human-
readable programs.  
In order for you to start writing computer programs in a programming language called Java, you 
need a compiler for the Java language. The next section guides you through the process of 
downloading and installing a compiler. Once you have a compiler, we can get started. This 
process is going to take several hours, much of that time being download time for several large 
files. You are also going to need about 40 megabytes of free disk space (make sure you have the 
space available before you get started).  
Downloading the Java Compiler 
In order to get a Java development environment set up on your machine -- you "develop" (write) 
computer programs using a "development environment" -- you will have to complete the following 
steps:  
1. Download a large file containing the Java development environment (the compiler and 
other tools).  
2. Download a large file containing the Java documentation.  
3. If you do not already have WinZip (or an equivalent) on your machine, you will need to 
download a large file containing WinZip and install it.  
4. Install the Java development environment.  
5. Install the documentation.  
6. Adjust several environment variables.  
7. Test everything out.  
Before getting started, it would make things easier if you create a new directory in your temp 
directory to hold the files we are about to download. We will call this the download directory.  
Step 1 - Download the Java development environment 
Go to the page http://java.sun.com/j2se/1.4.1/download.html. Download the SDK software by 
selecting your operating system and clicking the link on the next page. You will be shown a 
licensing agreement. Click Accept. Download the file to your download directory. This is a huge 
file -- almost 35 megabytes -- and it will take several hours to download over a normal phone-line 
modem. The next two files are also large.  
Step 2 - Download the Java documentation 
Download the documentation by selecting your operating system and clicking the SDK 1.4.1 
documentation link.  
Step 3 - Download and install WinZip 
If you do not have a version of WinZip or an equivalent on your machine, go to the page 
http://www.winzip.com/ and download an evaluation copy of WinZip. Run the EXE you get to 
install it. We will use it in a moment to install the documentation.  
Step 4 - Install the development kit 
Run the j2sdk-1_4_1-*.exe file that you downloaded in step 1. It will unpack and install the 
development kit automatically.  
Step 5 - Install the documentation 
Read the installation instructions for the documentation. They will instruct you to move the 
documentation file to same directory as that containing the development kit you just installed. 
Unzip the documentation and it will drop into the proper place.  
Step 6 - Adjust your environment 
As instructed on this page, you need to change your path variable. This is most easily done by 
opening an MS-DOS prompt and typing PATH to see what the path is set to currently. Then open 
autoexec.bat in Notepad and make the changes to PATH specified in the instructions.  
Step 7 - Test 
Now you should be able to open another MS-DOS window and type javac. If everything is set up 
properly, then you should see a two-line blob of text come out that tells you how to use javac. 
That means you are ready to go. If you see the message "Bad Command or File Name" it means 
you are not ready to go. Figure out what you did wrong by rereading the installation instructions. 
Make sure the PATH is set properly and working. Go back and reread the Programmer's Creed 
above and be persistent until the problem is resolved.  
You are now the proud owner of a machine that can compile Java programs. You are ready to 
start writing software!  
By the way, one of the things you just unpacked is a demo directory full of neat examples. All of 
the examples are ready to run, so you might want to find the directory and play with some of the 
samples. Many of them make sounds, so be sure to turn on your speakers. To run the examples, 
find pages with names like example1.html and load them into your usual Web browser.  
Your First Program 
Your first program will be short and sweet. It is going to create a drawing area and draw a 
diagonal line across it. To create this program you will need to:  
1. Open Notepad and type in (or cut and paste) the program  
2. Save the program  
3. Compile the program with the Java compiler to create a Java applet  
4. Fix any problems  
5. Run the Java applet  
Here is the program we will use for this demonstration:  
import java.awt.Graphics; 
 
public class FirstApplet extends java.applet.Applet 
{ 
 
    public void paint(Graphics g) 
    { 
        g.drawLine(0, 0, 200, 200); 
    } 
} 
Step 1 - Type in the program 
Create a new directory to hold your program. Open up Notepad (or any other text editor that can 
create TXT files). Type or cut and paste the program into the Notepad window. This is important: 
When you type the program in, case matters. That means that you must type the uppercase and 
lowercase characters exactly as they appear in the program. Review the programmer's creed 
above. If you do not type it EXACTLY as shown, it is not going to work.  
Step 2 - Save the file 
Save the file to the filename FirstApplet.java in the directory that you created in step 1. Case 
matters in the filename. Make sure the 'F' and 'A' are uppercase and all other characters are 
lowercase, as shown.  
Step 3 - Compile the program 
Open an MS-DOS window. Change directory ("cd") to the directory containing FirstApplet.java. 
Type:  
    javac FirstApplet.java 
Page 4


How Java Works 
by Marshall Brain
Have you ever wondered how computer programs work? Have you ever wanted to learn how to 
write your own computer programs? Whether you are 14 years old and hoping to learn how to 
write your first game, or you are 70 years old and have been curious about computer 
programming for 20 years, this article is for you. In this edition of HowStuffWorks, I'm going to 
teach you how computer programs work by teaching you how to program in the Java 
programming language.  
In order to teach you about computer programming, I am going to make several assumptions 
from the start:  
• I am going to assume that you know nothing about computer programming now. If you 
already know something then the first part of this article will seem elementary to you. 
Please feel free to skip forward until you get to something you don't know.  
• I am going to assume you do know something about the computer you are using. That is, 
I am going to assume you already know how to edit a file, copy and delete files, rename 
files, find information on your system, etc.  
• For simplicity, I am going to assume that you are using a machine running Windows 95, 
98, 2000, NT or XP. It should be relatively straightforward for people running other 
operating systems to map the concepts over to those.  
• I am going to assume that you have a desire to learn.  
All of the tools you need to start programming in Java are widely available on the Web for free. 
There is also a huge amount of educational material for Java available on the Web, so once you 
finish this article you can easily go learn more to advance your skills. You can learn Java 
programming here without spending any money on compilers, development environments, 
reading materials, etc. Once you learn Java it is easy to learn other languages, so this is a good 
place to start.  
Having said these things, we are ready to go. Let's get started!  
A Little Terminology 
Keep in mind that I am assuming that you know nothing about programming. Here are several 
vocabulary terms that will make things understandable:  
• Computer program - A computer program is a set of instructions that tell a computer 
exactly what to do. The instructions might tell the computer to add up a set of numbers, or 
compare two numbers and make a decision based on the result, or whatever. But a 
computer program is simply a set of instructions for the computer, like a recipe is a set of 
instructions for a cook or musical notes are a set of instructions for a musician. The 
computer follows your instructions exactly and in the process does something useful -- 
like balancing a checkbook or displaying a game on the screen or implementing a word 
processor.  
• Programming language - In order for a computer to recognize the instructions you give 
it, those instructions need to be written in a language the computer understands -- a 
programming language. There are many computer programming languages -- Fortran, 
Cobol, Basic, Pascal, C, C++, Java, Perl -- just like there are many spoken languages. 
They all express approximately the same concepts in different ways.  
• Compiler - A compiler translates a computer program written in a human-readable 
computer language (like Java) into a form that a computer can execute. You have 
probably seen EXE files on your computer. These EXE files are the output of compilers. 
They contain executables -- machine-readable programs translated from human-
readable programs.  
In order for you to start writing computer programs in a programming language called Java, you 
need a compiler for the Java language. The next section guides you through the process of 
downloading and installing a compiler. Once you have a compiler, we can get started. This 
process is going to take several hours, much of that time being download time for several large 
files. You are also going to need about 40 megabytes of free disk space (make sure you have the 
space available before you get started).  
Downloading the Java Compiler 
In order to get a Java development environment set up on your machine -- you "develop" (write) 
computer programs using a "development environment" -- you will have to complete the following 
steps:  
1. Download a large file containing the Java development environment (the compiler and 
other tools).  
2. Download a large file containing the Java documentation.  
3. If you do not already have WinZip (or an equivalent) on your machine, you will need to 
download a large file containing WinZip and install it.  
4. Install the Java development environment.  
5. Install the documentation.  
6. Adjust several environment variables.  
7. Test everything out.  
Before getting started, it would make things easier if you create a new directory in your temp 
directory to hold the files we are about to download. We will call this the download directory.  
Step 1 - Download the Java development environment 
Go to the page http://java.sun.com/j2se/1.4.1/download.html. Download the SDK software by 
selecting your operating system and clicking the link on the next page. You will be shown a 
licensing agreement. Click Accept. Download the file to your download directory. This is a huge 
file -- almost 35 megabytes -- and it will take several hours to download over a normal phone-line 
modem. The next two files are also large.  
Step 2 - Download the Java documentation 
Download the documentation by selecting your operating system and clicking the SDK 1.4.1 
documentation link.  
Step 3 - Download and install WinZip 
If you do not have a version of WinZip or an equivalent on your machine, go to the page 
http://www.winzip.com/ and download an evaluation copy of WinZip. Run the EXE you get to 
install it. We will use it in a moment to install the documentation.  
Step 4 - Install the development kit 
Run the j2sdk-1_4_1-*.exe file that you downloaded in step 1. It will unpack and install the 
development kit automatically.  
Step 5 - Install the documentation 
Read the installation instructions for the documentation. They will instruct you to move the 
documentation file to same directory as that containing the development kit you just installed. 
Unzip the documentation and it will drop into the proper place.  
Step 6 - Adjust your environment 
As instructed on this page, you need to change your path variable. This is most easily done by 
opening an MS-DOS prompt and typing PATH to see what the path is set to currently. Then open 
autoexec.bat in Notepad and make the changes to PATH specified in the instructions.  
Step 7 - Test 
Now you should be able to open another MS-DOS window and type javac. If everything is set up 
properly, then you should see a two-line blob of text come out that tells you how to use javac. 
That means you are ready to go. If you see the message "Bad Command or File Name" it means 
you are not ready to go. Figure out what you did wrong by rereading the installation instructions. 
Make sure the PATH is set properly and working. Go back and reread the Programmer's Creed 
above and be persistent until the problem is resolved.  
You are now the proud owner of a machine that can compile Java programs. You are ready to 
start writing software!  
By the way, one of the things you just unpacked is a demo directory full of neat examples. All of 
the examples are ready to run, so you might want to find the directory and play with some of the 
samples. Many of them make sounds, so be sure to turn on your speakers. To run the examples, 
find pages with names like example1.html and load them into your usual Web browser.  
Your First Program 
Your first program will be short and sweet. It is going to create a drawing area and draw a 
diagonal line across it. To create this program you will need to:  
1. Open Notepad and type in (or cut and paste) the program  
2. Save the program  
3. Compile the program with the Java compiler to create a Java applet  
4. Fix any problems  
5. Run the Java applet  
Here is the program we will use for this demonstration:  
import java.awt.Graphics; 
 
public class FirstApplet extends java.applet.Applet 
{ 
 
    public void paint(Graphics g) 
    { 
        g.drawLine(0, 0, 200, 200); 
    } 
} 
Step 1 - Type in the program 
Create a new directory to hold your program. Open up Notepad (or any other text editor that can 
create TXT files). Type or cut and paste the program into the Notepad window. This is important: 
When you type the program in, case matters. That means that you must type the uppercase and 
lowercase characters exactly as they appear in the program. Review the programmer's creed 
above. If you do not type it EXACTLY as shown, it is not going to work.  
Step 2 - Save the file 
Save the file to the filename FirstApplet.java in the directory that you created in step 1. Case 
matters in the filename. Make sure the 'F' and 'A' are uppercase and all other characters are 
lowercase, as shown.  
Step 3 - Compile the program 
Open an MS-DOS window. Change directory ("cd") to the directory containing FirstApplet.java. 
Type:  
    javac FirstApplet.java 
Case matters! Either it will work, in which case nothing will be printed to the window, or there will 
be errors. If there are no errors, a file named FirstApplet.class will be created in the directory right 
next to FirstApplet.java.  
(Make sure that the file is saved to the name FirstApplet.java and not FirstApplet.java.txt. This 
is most easily done by typing dir in the MS-DOS window and looking at the file name. If it has a 
.txt extension, remove it by renaming the file. Or run the Windows Explorer and select Options in 
the View menu. Make sure that the "Hide MD-DOS File Extensions for file types that are 
registered" box is NOT checked, and then look at the filename with the explorer. Change it if 
necessary.)  
Step 4 - Fix any problems 
If there are errors, fix them. Compare your program to the program above and get them to match 
exactly. Keep recompiling until you see no errors. If javac seems to not be working, look back at 
the previous section and fix your installation.  
Step 5 - Run the Applet 
In your MS-DOS window, type:  
    appletviewer applet.htm 
You should see a diagonal line running from the upper left corner to the lower right corner:  
 
Pull the applet viewer a little bigger to see the whole line. You should also be able to load the 
HTML page into any modern browser like Netscape Navigator or Microsoft Internet Explorer and 
see approximately the same thing.  
You have successfully created your first program!!!  
Understanding What Just Happened 
So what just happened? First, you wrote a piece of code for an extremely simple Java applet. An 
applet is a Java program that can run within a Web browser, as opposed to a Java application, 
which is a stand-alone program that runs on your local machine (Java applications are slightly 
more complicated and somewhat less popular, so we will start with applets). We compiled the 
applet using javac. We then created an extremely simple Web page to "hold" the applet. We ran 
the applet using appletviewer, but you can just as easily run it in a browser.  
Page 5


How Java Works 
by Marshall Brain
Have you ever wondered how computer programs work? Have you ever wanted to learn how to 
write your own computer programs? Whether you are 14 years old and hoping to learn how to 
write your first game, or you are 70 years old and have been curious about computer 
programming for 20 years, this article is for you. In this edition of HowStuffWorks, I'm going to 
teach you how computer programs work by teaching you how to program in the Java 
programming language.  
In order to teach you about computer programming, I am going to make several assumptions 
from the start:  
• I am going to assume that you know nothing about computer programming now. If you 
already know something then the first part of this article will seem elementary to you. 
Please feel free to skip forward until you get to something you don't know.  
• I am going to assume you do know something about the computer you are using. That is, 
I am going to assume you already know how to edit a file, copy and delete files, rename 
files, find information on your system, etc.  
• For simplicity, I am going to assume that you are using a machine running Windows 95, 
98, 2000, NT or XP. It should be relatively straightforward for people running other 
operating systems to map the concepts over to those.  
• I am going to assume that you have a desire to learn.  
All of the tools you need to start programming in Java are widely available on the Web for free. 
There is also a huge amount of educational material for Java available on the Web, so once you 
finish this article you can easily go learn more to advance your skills. You can learn Java 
programming here without spending any money on compilers, development environments, 
reading materials, etc. Once you learn Java it is easy to learn other languages, so this is a good 
place to start.  
Having said these things, we are ready to go. Let's get started!  
A Little Terminology 
Keep in mind that I am assuming that you know nothing about programming. Here are several 
vocabulary terms that will make things understandable:  
• Computer program - A computer program is a set of instructions that tell a computer 
exactly what to do. The instructions might tell the computer to add up a set of numbers, or 
compare two numbers and make a decision based on the result, or whatever. But a 
computer program is simply a set of instructions for the computer, like a recipe is a set of 
instructions for a cook or musical notes are a set of instructions for a musician. The 
computer follows your instructions exactly and in the process does something useful -- 
like balancing a checkbook or displaying a game on the screen or implementing a word 
processor.  
• Programming language - In order for a computer to recognize the instructions you give 
it, those instructions need to be written in a language the computer understands -- a 
programming language. There are many computer programming languages -- Fortran, 
Cobol, Basic, Pascal, C, C++, Java, Perl -- just like there are many spoken languages. 
They all express approximately the same concepts in different ways.  
• Compiler - A compiler translates a computer program written in a human-readable 
computer language (like Java) into a form that a computer can execute. You have 
probably seen EXE files on your computer. These EXE files are the output of compilers. 
They contain executables -- machine-readable programs translated from human-
readable programs.  
In order for you to start writing computer programs in a programming language called Java, you 
need a compiler for the Java language. The next section guides you through the process of 
downloading and installing a compiler. Once you have a compiler, we can get started. This 
process is going to take several hours, much of that time being download time for several large 
files. You are also going to need about 40 megabytes of free disk space (make sure you have the 
space available before you get started).  
Downloading the Java Compiler 
In order to get a Java development environment set up on your machine -- you "develop" (write) 
computer programs using a "development environment" -- you will have to complete the following 
steps:  
1. Download a large file containing the Java development environment (the compiler and 
other tools).  
2. Download a large file containing the Java documentation.  
3. If you do not already have WinZip (or an equivalent) on your machine, you will need to 
download a large file containing WinZip and install it.  
4. Install the Java development environment.  
5. Install the documentation.  
6. Adjust several environment variables.  
7. Test everything out.  
Before getting started, it would make things easier if you create a new directory in your temp 
directory to hold the files we are about to download. We will call this the download directory.  
Step 1 - Download the Java development environment 
Go to the page http://java.sun.com/j2se/1.4.1/download.html. Download the SDK software by 
selecting your operating system and clicking the link on the next page. You will be shown a 
licensing agreement. Click Accept. Download the file to your download directory. This is a huge 
file -- almost 35 megabytes -- and it will take several hours to download over a normal phone-line 
modem. The next two files are also large.  
Step 2 - Download the Java documentation 
Download the documentation by selecting your operating system and clicking the SDK 1.4.1 
documentation link.  
Step 3 - Download and install WinZip 
If you do not have a version of WinZip or an equivalent on your machine, go to the page 
http://www.winzip.com/ and download an evaluation copy of WinZip. Run the EXE you get to 
install it. We will use it in a moment to install the documentation.  
Step 4 - Install the development kit 
Run the j2sdk-1_4_1-*.exe file that you downloaded in step 1. It will unpack and install the 
development kit automatically.  
Step 5 - Install the documentation 
Read the installation instructions for the documentation. They will instruct you to move the 
documentation file to same directory as that containing the development kit you just installed. 
Unzip the documentation and it will drop into the proper place.  
Step 6 - Adjust your environment 
As instructed on this page, you need to change your path variable. This is most easily done by 
opening an MS-DOS prompt and typing PATH to see what the path is set to currently. Then open 
autoexec.bat in Notepad and make the changes to PATH specified in the instructions.  
Step 7 - Test 
Now you should be able to open another MS-DOS window and type javac. If everything is set up 
properly, then you should see a two-line blob of text come out that tells you how to use javac. 
That means you are ready to go. If you see the message "Bad Command or File Name" it means 
you are not ready to go. Figure out what you did wrong by rereading the installation instructions. 
Make sure the PATH is set properly and working. Go back and reread the Programmer's Creed 
above and be persistent until the problem is resolved.  
You are now the proud owner of a machine that can compile Java programs. You are ready to 
start writing software!  
By the way, one of the things you just unpacked is a demo directory full of neat examples. All of 
the examples are ready to run, so you might want to find the directory and play with some of the 
samples. Many of them make sounds, so be sure to turn on your speakers. To run the examples, 
find pages with names like example1.html and load them into your usual Web browser.  
Your First Program 
Your first program will be short and sweet. It is going to create a drawing area and draw a 
diagonal line across it. To create this program you will need to:  
1. Open Notepad and type in (or cut and paste) the program  
2. Save the program  
3. Compile the program with the Java compiler to create a Java applet  
4. Fix any problems  
5. Run the Java applet  
Here is the program we will use for this demonstration:  
import java.awt.Graphics; 
 
public class FirstApplet extends java.applet.Applet 
{ 
 
    public void paint(Graphics g) 
    { 
        g.drawLine(0, 0, 200, 200); 
    } 
} 
Step 1 - Type in the program 
Create a new directory to hold your program. Open up Notepad (or any other text editor that can 
create TXT files). Type or cut and paste the program into the Notepad window. This is important: 
When you type the program in, case matters. That means that you must type the uppercase and 
lowercase characters exactly as they appear in the program. Review the programmer's creed 
above. If you do not type it EXACTLY as shown, it is not going to work.  
Step 2 - Save the file 
Save the file to the filename FirstApplet.java in the directory that you created in step 1. Case 
matters in the filename. Make sure the 'F' and 'A' are uppercase and all other characters are 
lowercase, as shown.  
Step 3 - Compile the program 
Open an MS-DOS window. Change directory ("cd") to the directory containing FirstApplet.java. 
Type:  
    javac FirstApplet.java 
Case matters! Either it will work, in which case nothing will be printed to the window, or there will 
be errors. If there are no errors, a file named FirstApplet.class will be created in the directory right 
next to FirstApplet.java.  
(Make sure that the file is saved to the name FirstApplet.java and not FirstApplet.java.txt. This 
is most easily done by typing dir in the MS-DOS window and looking at the file name. If it has a 
.txt extension, remove it by renaming the file. Or run the Windows Explorer and select Options in 
the View menu. Make sure that the "Hide MD-DOS File Extensions for file types that are 
registered" box is NOT checked, and then look at the filename with the explorer. Change it if 
necessary.)  
Step 4 - Fix any problems 
If there are errors, fix them. Compare your program to the program above and get them to match 
exactly. Keep recompiling until you see no errors. If javac seems to not be working, look back at 
the previous section and fix your installation.  
Step 5 - Run the Applet 
In your MS-DOS window, type:  
    appletviewer applet.htm 
You should see a diagonal line running from the upper left corner to the lower right corner:  
 
Pull the applet viewer a little bigger to see the whole line. You should also be able to load the 
HTML page into any modern browser like Netscape Navigator or Microsoft Internet Explorer and 
see approximately the same thing.  
You have successfully created your first program!!!  
Understanding What Just Happened 
So what just happened? First, you wrote a piece of code for an extremely simple Java applet. An 
applet is a Java program that can run within a Web browser, as opposed to a Java application, 
which is a stand-alone program that runs on your local machine (Java applications are slightly 
more complicated and somewhat less popular, so we will start with applets). We compiled the 
applet using javac. We then created an extremely simple Web page to "hold" the applet. We ran 
the applet using appletviewer, but you can just as easily run it in a browser.  
The program itself is about 10 lines long:  
import java.awt.Graphics; 
 
public class FirstApplet extends java.applet.Applet 
{ 
 
    public void paint(Graphics g) 
    { 
        g.drawLine(0, 0, 200, 200); 
    } 
} 
This is about the simplest Java applet you can create. To fully understand it you will have to learn 
a fair amount, particularly in the area of object oriented programming techniques. Since I am 
assuming that you have zero programming experience, what I would like you to do is focus your 
attention on just one line in this program for the moment:  
        g.drawLine(0, 0, 200, 200); 
This is the line in this program that does the work. It draws the diagonal line. The rest of the 
program is scaffolding that supports that one line, and we can ignore the scaffolding for the 
moment. What happened here was that we told the computer to draw one line from the upper left 
hand corner (0,0) to the bottom right hand corner (200, 200). The computer drew it just like we 
told it to. That is the essence of computer programming!  
(Note also that in the HTML page, we set the size of the applet's window in step 5 above to have 
a width of 200 and a height of 200.)  
In this program, we called a method (a.k.a. function) called drawLine and we passed it four 
parameters (0, 0, 200, 200). The line ends in a semicolon. The semicolon acts like the period at 
the end of the sentence. The line begins with g., signifying that we want to call the method named 
drawLine on the specific object named g (which you can see one line up is of the class Graphics
-- we will get into classes and methods of classes in much more detail later in this article).  
A method is simply a command -- it tells the computer to do something. In this case, drawLine 
tells the computer to draw a line between the points specified: (0, 0) and (200, 200). You can 
think of the window as having its 0,0 coordinate in the upper left corner, with positive X and Y 
axes extending to the right and down. Each dot on the screen (each pixel) is one increment on 
the scale.  
Read More
Offer running on EduRev: Apply code STAYHOME200 to get INR 200 off on our premium plan EduRev Infinity!