![]() |
![]() Spring Semester 2005
|
The environment used in this class is Windows XP. Your desktop might look like this:
In this class we will be writing computer programs.
A computer program is just another file.
We use text editors to create files.
In this class we will be using Notepad
to create and edit our programs.
To start Notepad
you might have to go through something like this:
Once you start Notepad
a blank editor window will show up on your screen:
In this class we will be using the Java programming language to write programs.
You will find Java as peculiar as any other foreign language.
Java uses some English words but also curly braces, square brackets, semicolons, parens and such.
They all have a meaning of their own. It is the purpose of this class to demystify this language for you.
Use Notepad
to create the simplest possible Java program. Type exactly as indicated below:
When you're done you need to save the file:
You never know where Notepad
wants to save the file you just created.
In the picture below we're in some folder called converter
.
But we really don't want to be there.
Instead we want to save the file we just created on the Desktop
.
Once the Desktop
was selected, we're ready to give the file a name and save it.
Java programs need to be saved as files with extension .java
That means the default extension Notepad
is offering (.txt
) is
not what we need.
So we change it to... whatever else is available.
In this case only All Files
is available.
All files on the Desktop
are listed.
We need to give our file a name and Hello.java
is the only name we can use in this case.
The extension .java
is mandatory and the name (Hello
) is the same as the identifier that follows the keyword class
in the code.
Once we save the file an icon should indicate that on the Desktop
.
Can you see it at the top? (Go by the name listed under the icon, since the icon is arbitrary).
Programs need to be compiled before the computer can make any sense of them.
The Java programming language (arcane as it may look to you) is still readable.
The language that the computer really understands (in this case bytecode) is not readable by normal human beings.
If we compile the program we wrote into Java bytecode the computer will be able to understand it.
We need to use the javac
utility to compile the program.
To run the javac
utility we need to get a command prompt window.
To obtain one you might need to go through a menu like this:
The command prompt window looks like the one shown below:
Notice there is a working directory (folder) you're in.
Since your program is on the desktop you should move there (with cd
).
And once you move the window will be updated.
You can list the files in the directory, so you can also check on your
newly created Hello.java
file.
The pattern Hell*
indicates all the files whose names start with Hell
.
There's only one and some information about the file is listed (size, date of creation).
To compile you invoke the javac
utility on the file name (Hello.java
in
this case).
This simply means you need to type: javac Hello.java
at the command prompt.
If you make no mistakes in typing the file exactly as shown the compilation succeeds and a new prompt is given to you.
But most of the times we make mistakes. In that case you have to go back and correct them.
Finding and correcting errors is what a programmer typically does.
But eventually no errors remain and the compilation succeeds.
When that happens additional files will get created.
In this case one additional file (Hello.class
) which contains the bytecode.
Compilation of a program is only the first step.
Through it the computer is provided with a complete set of instructions (for a specific task) in its own language.
For the computer to perform the task the program instructs it to, we need to execute the program.
Execution of the program is accomplished with the help of the java
utility.
Our program is the simplest possible program: one that does nothing.
But even the requirement of doing nothing has to be expressed in Java.
And it has the form you just saw.
Let's change the program and make it do something.
We need to change the source code (the file we developed earlier).
Creating or editing files is done with a text editor.
So start Notepad
.
And then make your intentions of opening an existing file known.
Notepad
shows you the contents of the desktop because that's where you last saved a file.
But it assumes that you want to look at a text file (one that has a .txt
extension).
So change this to All Files
and then you will see even the file that
you want to open.
Choose Hello.java
and push the Open
button.
And the contents of what we created just a few minutes ago will be loaded in the
Notepad
window.
Change the source code (the contents of the file) by typing a new line, as shown.
Then Save
the file.
No questions will be asked this time around, and
Desktop
Since the file has been updated you can compile it.
And if there are no compilation errors you can execute (or run) the program.
Tue Jan 11 09:34:52 EST 2005