# Introduction to Java

 Site: Saylor Academy Course: CS101: Introduction to Computer Science I Book: Introduction to Java
 Printed by: Guest user Date: Thursday, March 23, 2023, 12:46 AM

## Description

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.

## 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.

Chapter Topics:

• Hello World program
• Java Bytecodes
• Java compiler and Java virtual machine
• Using Notepad to create a source program
• Running a Java program

#### Question 1:

Can the processor of a computer system directly execute source programs written in Java?

## 2. Simple Java Program

Answer:No

# 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.

class Hello
{
public static void main ( String[] args )
{
System.out.println("Hello World!");
}
}

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.

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.

Question 2:What are the two ways that a source program can be run on a computer system?

## 3. Bytecodes

Answer:Translation (into machine instructions, which are then directly executed by the processor)Interpretation (by an interpreter program)

# Bytecodes

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 Hello.java  into bytecodes. The file of bytecodes is called Hello.class.

In this picture, the source program Hello.java is examined by a program called javac  running on your computer. The javac program is a compiler (a translator) that translates the source program into a bytecode file called Hello.class.

Important Idea: The bytecode file will contain exactly the same bytecodes no matter what computer the javac compiler runs on.
The architecture of the processor that executes Java bytecodes is well-documented and is available to anyone. The Java compiler on a Macintosh will produce the same bytecodes as the Java compiler on an Intel system.

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

Answer: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.

(Another) Important Idea: When the Java interpreter is running on a computer system, that system acts just like a hardware Java bytecode processor. It is a Java Virtual Machine.

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.

Question 4:Say that Apple has just come out with a new computer and wants this computer to run Java programs. What must Apple do?

## 5. Portability

Answer:Apple must write a Java interpreter for their new system.

# Portability

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.

Question 5:Can bytecodes be sent from computer to computer over the Internet?

## 6. Applets

Answer:Yes.

# Applets

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.

Question 6:Is the Java used to write applets the same Java as that used to write applications?

## 7. Creating a Java Source Program

Answer:Yes.

# 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.

Goals:

1. Start a command prompt window (DOS window) in a folder.
2. Create the source file Hello.java
3. Save the source file in the same folder.
4. Compile the source file to create the file of bytecodes Hello.class in the folder.
5. Run the bytecode file.
The following few pages show all this done on a particular Windows 7 computer. The Java Development Kit JDK has already been installed on this computer. It is almost certain that your computer will be different, but hopefully not too different.

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.

class Hello
{
public static void main ( String[] args )
{
System.out.println("Hello World!");
}
}

Question 7:Will you see exactly the same things as above on your computer system?

## 8. Click Run

Answer: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.

# Click Run

After clicking the "Start" button you should see something similar to the above. (Windows 7 on the left, Windows 8 on the right.)

Question 8:Click "Run". (Windows 8 option: click "command prompt")

## 9. CMD

Answer:A new menu appears.

# CMD

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.)

Question 9:Enter cmdWindows 8 option: If you clicked "command prompt" (previous page) you can skip this step.

## 10. Command Interpreter Window

Answer: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

Windows 7

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

 C:\Users\TEMP>

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:

 C:\WINNT\System32>

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):

C:\Users\TEMP

If you need to get to this folder type the following commands:

C:

CD \Users\TEMP

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).

Windows 8

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

 C:\Users\YourName>

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):

C:\Users\YourName\JavaSource>

To create this directory do this:

C:\Users\YourName\>MKDIR JavaSource

This creates the subdirectory (folder) JavaSource

However, the default directory has not changed. To change the default directory to JavaSource do this:

C:\Users\YourName\>CD JavaSource

C:\Users\YourName\JavaSource>DIR

The first command CD switches to the JavaSource subdirectory. The second command makes DIR lists the contents of that directory (which should be empty.)

Question 10:Can all of this be done using "File Explorer" from the desktop.

Answer:Yes.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:

C:Users\TEMP> notepad

C:Users\TEMP>

Windows 8:

C:\Users\YourName\JavaSource>notepad

C:\Users\YourName\JavaSource>

Notepad starts in its own window.

Question 11:Is all this getting to be just too much?

Answer: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.

Question 12:Can you type characters into the Notepad window, much like with a word processor?

Answer:Yes.

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.

Question 13:Does NotePad save your source file into the default directory it was started in?

## 14. Saving the Source File

Answer: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.

Question 14: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

Answer: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:

class Hello

so the file name must be Hello.java. When you get to the Users/TEMP directory (or whatever default directory you are using):

1. Type the file name into the box.
2. 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.
1. Select "Save as all files" by clicking on the little down arrow it the box below the file name box.
2. Select "Encoding: ANSI" in the bottom box.
3. 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.

Question 15:So, finally your Java program source file is saved. What must you do to run it?

## 16. Running the Program

Answer: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.)

C:\Users\YourName\JavaSource>dir
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 javac Hello.java.

C:\Users\YourName\JavaSource>javac Hello.java

Notice that this command is javac, java-with-a-c which invokes the Java compiler. If you see the following

C:\Users\YourName\JavaSource>javac Hello.java
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:

Windows 7:

C:\Users\YourName\JavaSource> C:\Program Files\Java\jdk1.8.0_05\bin\javac Hello.java

Windows 8:

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 java Hello.

C:\Users\YourName\JavaSource>java Hello
Hello World!
C:\Users\YourName\JavaSource>

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.

Question 16:After all of this, what did the Java program actually do?

## 17. Result of Running the Example Java Program

Answer: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 [ and { and ( and ". 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.

Question 17:Had enough, for now?

## 18. End of the Chapter!

Answer:Yes.

# 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.