Introduction to Java
|Course:||CS101: Introduction to Computer Science I|
|Book:||Introduction to Java|
|Printed by:||Guest user|
|Date:||Thursday, March 23, 2023, 12:46 AM|
Work through these slides. As you read, think about and answer the questions at the bottom of each page. These will be your first experience with Java, so make sure you follow each step closely.
Table of contents
- 1. Introduction to Java
- 2. Simple Java Program
- 3. Bytecodes
- 4. Java Virtual Machine
- 5. Portability
- 6. Applets
- 7. Creating a Java Source Program
- 8. Click Run
- 9. CMD
- 10. Command Interpreter Window
- 11. Starting Notepad
- 12. Notepad
- 13. Using Notepad
- 14. Saving the Source File
- 15. Naming the File
- 16. Running the Program
- 17. Result of Running the Example Java Program
- 18. End of the Chapter!
1. Introduction to Java
These notes are written for Java 2 Standard Edition version 5.0 or higher. Many features were added to this edition and previous versions will not work as well with these notes. If you are installing Java for the first time on your computer, install the Java Development Kit (JDK) from Oracle.
- Hello World program
- Java Bytecodes
- Java compiler and Java virtual machine
- Using Notepad to create a source program
- Running a Java program
Can the processor of a computer system directly execute source programs written in Java?
Source: Bradley Kjell, https://chortle.ccsu.edu/java5/Notes/chap05/ch05_1.html
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 License.
2. Simple Java Program
Simple Java Program
Here is an example Java program. It is about as small as a Java program can be. When it runs, it writes Hello World! on the computer monitor. The details will be explained later.
public static void main ( String args )
This program can be created using a text editor such as the Notepad editor that comes with Windows. The program will be in a text file on the hard disk, named Hello.java.
See the following pages for details on how to do this.
A source program is a text file that contains a program (such as above) written in a programming language. Since it contains ordinary text (stored as bytes) it can not be directly executed (run) by the computer system. As a text file, you can print it, display it on the monitor, or alter it with a text editor.
What are the two ways that a source program can be run on a computer system?
- Translation (into machine instructions, which are then directly executed by the processor)
- Interpretation (by an interpreter program)
Java combines these ideas in a way that will take some explaining. To run a Java program the source file is first translated into a file of bytecodes .
A Java bytecode is a machine instruction for a Java processor. A file of bytecodes is a machine language program for a Java processor.
Conceptually, a Java processor is a silicon processor chip that directly executes a machine language called Java bytecodes. This is like an Intel processor that directly executes Intel machine language.
The picture shows the Java compiler translating the sample Java program
into bytecodes. The file of bytecodes is called
In this picture, the source program
Hello.java is examined by a program called
running on your computer. The javac program is a compiler (a translator) that translates the source program into a bytecode file called
Important Idea: The bytecode file will contain exactly the same bytecodes no matter what computer the javac compiler runs on.
Question 3:Could a processor chip be built that executes Java bytecodes directly, just as an Intel processor chip executes its machine language directly.
4. Java Virtual Machine
Yes. Hardware Java processor chips have been created out of silicon and can execute bytecode files directly.
Java Virtual Machine
Usually, however, people do not have hardware Java processor chips. They have ordinary PCs and Macintoshes.
Now for the clever part: the Java processor can be implemented as software! It is implemented as a program that reads the bytecodes and performs the operations they specify. This is another type of interpreter. Some interpreters run source code written in a high level language like Basic; others (like the Java interpreter) run bytecodes.
This second type of interpreter is sometimes called an emulator because it emulates hardware, but in fact is software. A Java bytecode interpreter can be created for any computer system. Once you have a Java compiler and a Java interpreter you can run any Java program no matter what type of computer you have.
The "Java interpreter" in the picture is an executable program that is running on an ordinary computer system, such as a desktop Intel system. Each type of computer system has its own Java interpreter that can run on that system. The "Actual Processor" is the actual hardware processor chip of that computer system.
Any computer system can execute a Java bytecode program by using a Java interpreter. The Java interpreter has to be specifically written for that particular computer system, but once that is done, the computer system can become a Java virtual machine.
That is, it looks like a computer with a hardware Java processor chip and can run Java bytecodes.
When a Java program is translated into bytecodes, the bytecodes are the same no matter what computer system is used.
A Java source program (such as
Hello.java) can be written and compiled on one computer (say a Windows computer) to produce bytecode (say
Hello.class). Now that bytecode can run on any computer that has a Java interpreter.
Say that Apple has just come out with a new computer and wants this computer to run Java programs. What must Apple do?
Apple must write a Java interpreter for their new system.
Apple writes a Java interpreter for their new system. Now that one program (the interpreter) can run on their system, and that interpreter can run any Java bytecode program. Nothing new needs to be done with those bytecode programs.
Java programs are portable, which means that the same bytecode program can run on any computer system that has a Java interpreter. Also, a source program can be compiled into bytecodes on any computer that has a Java compiler.
The source program does not have to be changed to meet the particular needs of a particular computer systems. No matter what computer you have, you can write the same Java programs.
This is unlike most other programming languages, where a different version of a program must be made for each variety of computer, and an executable program that runs on one type of computer will not run on another.
Can bytecodes be sent from computer to computer over the Internet?
An applet is a Java bytecode program that runs on a Web browser. Most up-to-date Web browsers include a Java interpreter. A Web page may contain an applet, which means that part of what the page displays is controlled by Java bytecodes.
The computer that hosts the Web page sends Java bytecodes to a client computer (like yours) that has asked for the page. The Web browser on the client runs the Java applet using its built-in interpreter.
Applets are used for user interaction, graphics, and animation. Applets will be discussed in later chapters of these notes. For now, let us concentrate on Java programs that get input from the keyboard and write output to the command prompt window of
the monitor. These are called Java application programs.
Is the Java used to write applets the same Java as that used to write applications?
7. Creating a Java Source Program
Creating a Java Source Program
If you have already figured out how to create, compile and run Java programs on your computer, you can skip the rest of this chapter.
We will use the Notepad editor that comes with Microsoft Windows operating systems. Notepad is simple to use and good for getting started. After you see how things work using Notepad, move on to a better text editor (such as Crimson or Notepad++) or to
a Java integrated development environment (IDE) (such as BlueJ or Dr. Java). For now, our goal is to create a text file called Hello.java containing the text at right. We want to save the file in a folder (subdirectory) and to start a command
prompt window in the same folder.
- Start a command prompt window (DOS window) in a folder.
- Create the source file Hello.java
- Save the source file in the same folder.
- Compile the source file to create the file of bytecodes Hello.class in the folder.
- Run the bytecode file.
Windows 7: To start, LEFT click the "start" icon at the lower left of your screen.
Windows 8: To start, RIGHT click the "window" icon at the lower left of your screen.
public static void main ( String args )
Will you see exactly the same things as above on your computer system?
8. Click Run
No. Probably not.
An alternative way of doing all this is to use Microsoft's "File Explorer" to navigate to a desired subdirectory (or to create it) and start a command window there. If this is appealing, skip ahead to page 11.
After clicking the "Start" button you should see something similar to the above. (Windows 7 on the left, Windows 8 on the right.)
Click "Run". (Windows 8 option: click "command prompt")
A new menu appears.
In the Open: box, enter cmd.
(It also works to enter command, but the command interpreter window you get doing that is somewhat less convenient.)
Windows 8 option: If you clicked "command prompt" (previous page) you can skip this step.
10. Command Interpreter Window
Your own system will likely be different, especially if you are using a school computer. There are many ways to do these things.
Command Interpreter Window
After you start the command prompt window you can enter commands as if you were running an old-time DOS computer. You should see something like the picture. The line of text
is a prompt. You are expected to enter commands to the system after it. The prompt shows that the command interpreter is automatically expecting commands to apply to files in the folder C:\Users\TEMP. This is called the default folder.
Depending on how your computer has been set up, you may see a prompt something like this:
This prompt means that the command interpreter is automatically expecting to use files in the folder C:\WINNT\System32. DO NOT use the C:\WINNT\System32 folder.
It doesn't matter where you start out because you can change the default directory using the
CD command. (This is the Change Directory command.) For now, let us create a Java program in the folder (directory):
If you need to get to this folder type the following commands:
Type these commands after whatever command prompt you see. The prompt changes to show the current default directory. The first command C: switches to the C: disk (which is the hard disk of the system if you have only one). The second command makes C:\Users\TEMP the default directory (default folder).
After you start the command prompt window you can enter commands as if you were running an old-time DOS computer. You should see something like the picture. The line of text similar to this
is a prompt. Of course, your login name will replace "YourName". You are expected to enter commands to the system after it. The prompt shows that the command interpreter is automatically expecting commands to apply to files in the folder C:\Users\YourName. This is called the default folder.
For now, let us create a Java program in the folder (directory):
To create this directory do this:
This creates the subdirectory (folder) JavaSource
However, the default directory has not changed. To change the default directory to JavaSource do this:
The first command CD switches to the JavaSource subdirectory. The second command makes DIR lists the contents of that directory (which should be empty.)
Can all of this be done using "File Explorer" from the desktop.
11. Starting Notepad
An alternative to all of the above (for both Win 7 and Win 8) is to navigate to a convenient folder (using File Explorer),
then SHIFT-RIGHT click in the file listing of that directory and pick "Open Command Window Here" from the menu.
You should see a command window similar to the previous page.
Start NotePad from the command prompt:
Notepad starts in its own window.
Is all this getting to be just too much?
- Yes — Find a Web site that explains basic computer use and DOS commands,
or find a suitable book in your library. You will only need to look at the first one or two
chapters. Better yet, find a friend that knows all this and is happy to show off.
- No — Good.
Notepad should start running, as in the picture.
Can you type characters into the Notepad window, much like with a word processor?
13. Using Notepad
Once Notepad is running, just type the program into the window, as in the picture.
To enter characters, just type them. You can move around the text using the mouse or arrow keys. To delete mistakes, use the "backspace " key or the "delete " key. To start a new line, just hit "Enter ".
In typing in the program, make sure upper and lower case letters and all punctuation are exactly correct. You do not have to get the spaces exactly correct.
Now you need to save the file to the hard disk. Do this with one of the selections in the menu bar at the top of the Notepad window.
Does NotePad save your source file into the default directory it was started in?
14. Saving the Source File
Sadly, NO. Be super careful that all your work is done in the same subdirectory.
Saving the Source File
Move the mouse pointer to "File" and click on it. You will get a sub-menu. Now click on "Save As". As the picture shows, you may not start out in the default directory of the command prompt window. Use the various controls at the top of the "Save As" dialog to navigate to the subdirectory you want (the default directory).
Navigating to the default subdirectory will be similar to the same task with Microsoft Word or other application program. You may need to go up the file hierarchy and then down to the default subdirectory. See next page for a picture of NotePad's file navigator.
If you save the file in a different directory than the one being used for the DOS window, will there be a problem?
15. Naming the File
Yes. You want the source file Hello.java to be saved in the disk directory that is the default
directory for the command prompt window.
Naming the File
The name of the source file should be the same as the name that follows the word
class in the program (match upper and lower case), and should be given the extension .java (lower case). For example, our program
contains the line:
so the file name must be Hello.java. When you get to the Users/TEMP directory (or whatever default directory you are using):
- Type the file name into the box.
- The file name should be Hello.java
- For some versions of Notepad you should put quote marks around the file name like this: "Hello.java" even though the quote marks will not be part of the file name.
- Sometimes Notepad automatically uses the extension ".txt" and you may need to struggle to name the file what you want.
- Select "Save as all files" by clicking on the little down arrow it the box below the file name box.
- Select "Encoding: ANSI" in the bottom box.
- Click on "Save"
Your system is likely to be slightly different. Experiment with different choices until you are able to create a text file that contains the sample Java program.
Fussy, bothersome, irksome, irritating details. Yes, I know... Some people actually like this stuff. But if you can tie your shoes or drive a car you can do this. It might take some practice.
So, finally your Java program source file is saved. What must you do to run it?
16. Running the Program
Compile it into Java bytecodes, then run the bytecode file with the Java interpreter.
Running the Program
To do all that, find the DOS command prompt window you started up a while back. It should still be positioned in the C:\Temp directory (or other directory of your choice). To check that you put the source file Hello.java in this directory, enter the command dir. You should see the source file, as below (for Windows 8). For Windows 7, the subdirectory will be C:\Users\TEMP (unless you picked a different one.)
Volume in drive C has no label.
Volume Serial Number is 30E8-A977
Directory of C:\Users\YourName\JavaSource
08/14/2014 09:28 PM <DIR> .
08/14/2014 09:28 PM <DIR> ..
08/14/2014 09:28 PM 228 Hello.java
1 File(s) 228 bytes
2 Dir(s) 274,381,590,528 bytes free
To compile the source file (thereby producing a file of bytecodes) enter the command
Notice that this command is javac, java-with-a-c which invokes the Java compiler. If you see the following
The name specified is not recognized as an internal or external command, operable program or batch file.
then the PATH environment variable has not been set correctly. Look at Appendix C for information about this.
As a temporary alternative to a defective PATH, tell the command prompt exactly where to find javac:
C:\Users\YourName\JavaSource> C:\Program Files\Java\jdk1.8.0_05\bin\javac Hello.java
C:\Users\YourName\JavaSource> C:\\"Program Files\"\Java\jdk1.8.0_05\bin\javac Hello.java
Adjust the above command to match whatever directory your version of Java has been installed in. You almost certainly will need to change jdk1.8.0_05 to whatever you have installed, perhaps jdk1.7.0_06 or jdk1.8.0_06
Finally, to run the program, enter the command
Notice that this command is java, java-without-a-c which invokes the Java interpreter. Again, if PATH is incorrect, you can run java by using the full path name to its location.
After all of this, what did the Java program actually do?
17. Result of Running the Example Java Program
The example Java program wrote the words "Hello World!" in the command interpreter window.
Result of Running the Example Java Program
The picture shows my system when everything has worked correctly (which happens every time, of course).
Your program may not have run correctly. It is possible that you did not type in exactly the right characters. Spaces don't particularly matter. But check especially characters like
". You have to use the correct ones. Check that upper case and lower case characters match the sample program.
Fuss around for a while to get the program to work. If you just can't get it to work, give up and move on. Probably there is some trivial detail that you have overlooked and will see clearly later on. This happens all the time in programming.
If everything worked out perfectly, then go back and do something wrong. This will help you understand what happened later on when you unintentionally do something wrong.
Had enough, for now?
18. End of the Chapter!
End of the Chapter!
Hopefully this chapter explained just exactly what you needed to know to run Java programs, and everything worked out perfectly, and you can now create Java source programs, and compile them into bytecodes, and execute them using the Java interpreter.
Well.... one can hope. If something was less than perfect, find a friend that knows more about these things and is happy to help. Or, start again and go through everything once again. Or, move on and come back to this chapter later.
You may wish to review the following. Click on a subject that interests you to go to where it was discussed.
The next chapter discusses how the example program worked.