IT 114: Introduction to Java
Class 3
Topics
Review
New Material
Homework
The first homework assignment is due this Sunday at 11:59 PM.
You will find the assignment here.
If you have a problem or a question, make a post on the Class Discussion Area.
Review
Connecting to a Unix Machine
Your Home Directory
- When you first connect to the Unix machine you will be in your
home directory
- A directory
holds files and other directories
- It is called a folder on Windows and the Mac
- Every Unix account on our system has a home directory
- This directory belongs to you and you alone
The Hierarchical Filesystem
- Unix uses a
hierarchical filesystem
- Which means that all files are kept in directories (folders)
- And directories live inside other directories
- With one special directory at the top called root
- Which is written as /
Moving To A Different Directory
- To move to a different directory you use the
cd
command
cd
stands for "change directory"
- The command has the following format
cd DIRECTORY_NAME
- The hierarchical filesystem can be a little confusing
- If you are ever confused by where you are when connected to Unix, there is a simple fix
- Use the
cd
command without an argument to go back to your home directory
Always Know Where You Are
- The hierarchical filesystem is like pyramid
- The root directory is at the top
- All other directories are inside this directory
- Or in sub-directories
- Or in sub-sub-Directories
- And so on
- The pyramid gets wider the deeper you go
- Many commands will not work properly if you are in the wrong directory
- Always know where you are in the hierarchical filesystem
- One way to learn your location is to use the
pwd
command
$ pwd
/home/olivia93
Creating A New Directory
- You create a directory using the
mkdir
command
mkdir
stands for "make directory"
- It has the following format
mkdir DIRECTORY_NAME
Looking at the Contents of a File
- If you want to see the contents of a Unix file use the
cat
command
cat
stands for "concatenate"
- A term that will be defined later
- The format for this command is
cat FILENAME
Compiled versus Interpreted Languages
- When you write a program in you create a text file
- This file consists of statements in the programming language
- This file is called the
source code
- But the computer does not understand the language of the source code
- It only understand programs in its own binary
machine language
- A program is needed to turn your source code into the binary code the machine understands
- There are two ways a program can do this
- It can create a new file, translating the source code into machine language
- It can run the program inside itself, translating the code line by line
- The programs that take the first approach are called
compilers
- Programs that take the second approach are called
interpreters
- The programming languages that take the first approach are compiled languages
- The programming languages that take the second approach are scripting languages
Compiled Languages
- Language like C and C++ are compiled languages
- You run a C compiler on a C source code file
- And it creates an executable file
- The executable file can then be run on the machine
- Unfortunately, different machines have different machine languages
- So an executable file for one machine will not run on a machine of a different type
Scripting Languages
- Languages like Perl, Python and PHP are scripting languages
- In a scripting language the source code is not compiled
- So no executable file is created
- Instead the source code is run inside a program called an interpreter
- Interpreters are written in a compiled language like C
- But they must be compiled for specific machines
- Programs written in a scripting language run more slowly than than compile programs
Java Works Differently
- Java takes a middle course between compiled and interpreted languages
- You take a Java source file and run it through the Java compiler,
javac
- The source file must have the .java extension
- The compiler creates an
object file
with extension .class
- This object file does not contain machine language instructions
- Instead it contains what are called Java
bytecodes
- So you cannot run this .class file directly on the machine
- Instead you must run it inside the Java interpreter,
java
- The Java interpreter is also called the Java Runtime Environment (JRE)
- Or the Java Virtual Machine (JVM)
- Java interpreters have been written for all major platforms
- That means you can distribute a single set of .class files that will run on most machines
- Java gives developers most of the efficiency of a compiled language
- But the object files can be run almost anywhere
New Material
A Simple Java Program
- Let's see what a real java program looks like
- Here is the Hello program from the textbook
public class Hello {
public static void main(String[ ] args) {
System.out.println("Hello world!") ;
}
}
- Notice the name of the source file, Hello.java
- It is the same as the class name, Hello, with a .java extension
- To compile this program, I run
$ javac Hello.java
- There are now two files whose name, before the extension, is Hello
$ ls Hello*
Hello.class Hello.java
- To run the program, I use
$ java Hello
Hello World!
- Notice that the argument I gave to
java
was not the name of a file
- It was the name of the class
Things That Make Java Hard
- Java is an object oriented language
- This means that every Java program has to have a certain structure
- In a scripting language like Perl I could write the following
to add the numbers from 1 to 1000
$sum = 0;
for ($i = 0; $i <= 1000; $i++) {
$sum = $sum + $i;
}
print $sum;
- In Java, a program to the same thing would look like this
public class Sum_1000 {
public static void main(String[ ] args) {
int sum = 0;
for (int i = 0; i < 1000; i++){
sum += i;
}
System.out.println(sum);
}
- I've highlighted in red all the extra things .that Java requires
- Why does Java do this?
- Different computer languages are designed for different purposes
- A scripting language like Perl and Python was designed to get something done fast
- Scripting languages aren't very fussy
- Which is great for doing something quickly
- Languages like Java were designed for writing big programs
- If you were writing software for a bank you wouldn't use Perl or Python
- You would use Java
- Why?
- Because all the fussiness built into Java makes it harder to make certain kinds of errors
- Java is a language for big projects
- Where many different programmers work together
- And they have to follow certain rules to avoid chaos
Structure of Java Programs
Copying A File in Unix
The nano
Text Editor
- All your assignments for this class involve creating scripts on the Unix machine users3.cs.umb.edu
- Scripts are just text files that contain Python statements
- Test files only contain characters you type at the keyboard
- They do not contain fonts or styles like bold
- To create these files you need to use a Unix text editor
- There are many Unix text editors that you can use but I would suggest using
nano
nano
is a simple text editor created as part of the GNU project
- In
nano
you run a command by holding down the Control key while pressing a letter key
- The Control key is on the lower left side of your keyboard and is often labeled "Ctrl"
- The control key is a modifier key
- If you hold down the Control key and press another key it makes something happen
- You can move to the beginning of a line of text by pressing Control A
- And move to the end of the line with Control E
- "Control A" means hold down the Control key while pressing
A
- Although I used a capital A here
I do not mean you should hold down the shift key
- I use capital letters when writing control key sequences because the capital letters are easier to read
- Some of the basic
nano
commands appear at the bottom of the page
- The ^ in this list of commands stands for the Control key
so ^O means Control O
- The
nano
feature set is limited
- You can only work with one file at a time
- You can search for text but there is no search and replace feature
nano
does have a limited cut and paste feature
- If you press Control K the entire line will disappear
- Then if you go to another line and press Control U
the line will be pasted back at that point
- When you want to save a file you press Control O
- And the name of the file will appear at the bottom of the screen
- You need to hit Enter to accept that name and complete the save process
- Control X will quit
nano
- I have created a web page with instructions for using
nano
here
- There is a link to it on the class web page
Using nano
in This Class
- There are two types of assignments in this class
- Class Exercises
- Homework Assignments
- In each case you will make a copy of the template file
xxx.java to your current directory
- Then you will use
nano
to change the file
- To run
nano
use the following format
nano FILE_NAME
- You can use
nano
to create a new source code file
- Or to change a file that already exists
- Either way, we use the same format
nano FILE_NAME
Creating the Ex3.java for Today's Class Exercise
- In today's Class Exercise you are asked to create Ex3.java
- When you run this program it prints "Hello world!
- Following the instructions in today's Class Exercise you will create an
ex3 directory inside you ex directory
- You will then
cd
into this directory and make a copy of
xxx.java called Ex3.java
cd ex3
cp /home/ghoffman/course_files/files_it114/xxx.java Ex3.java
- Ex3.java will contain the following text
public class CLASS_NAME {
public static void main(String[] args) {
# your code goes here
}
}
- Open the source code file in nano
nano Ex3.java
- You need to make two changes
- Change CLASS_NAME in the first line to Ex3
- It must be capitalized this way or it won't work
- The first line should now look like this
public class Hello {
- Next you need to remove the line that reads
# your code goes here
- And replace it with
System.out.println("Hello world!");
- Don't leave out the ; at the end of the line or it won't work
- Save your changes and quit
Compiling the Source Code
- Ex3.java is the source code
- You need to compile this to create the object file that you can run
- You do this by running
javac
the Java compiler
javac Ex3.java
- If the compilation works you should see two files in your directory
$ ls
Ex3.class Ex3.java
- Ex3.class is the object file
- It contains bytecodes that the Java interpreter understands
- If file does not compile you will get an error message
- Basically this means that you have made an error when you edited the file
- Often this means one of two things
- You made an error in capitalization
- Or you left out the ; at the end of the
println statement
Running the Program
- users3 does not understand bytecodes
- To run your program you need to use the Java interpreter
- Which is also known as the Java Runtime Environment (JRE)
- The name of this program is
java
- You need to tell it which object file to run
- But you don't use the full name of the file
- Instead you give it the filename without the .class extention
$ java Ex3
Hello world!
Class Quiz
Attendance
Class Exercise