Lesson 2
Lesson 2
The source file for a high-level language needs to be translated into a form that
a machine can understand through a process called compilation.
Compilation is the process of translating high-level language into machine language
for platform-specific languages or some other intermediate code representation for
other languages. A program called a compiler does this.
Machine Language is a sequence of instructions directly executed by a processing
unit (CPU in most cases).
When you compile Java source code it results in Java Byte Code, NOT Machine
(Specific) Language
From Source Code to a Running Program
Running a compiler successfully on source code results in an executable file.
An Executable File is a file that can be directly executed or run by a processing
unit (again, most often the CPU) or run by another program that executes the file.
When a program executes (runs) the instructions of that program are carried-out.
Executable files containing machine code can run without aid by a processing
unit.
Executable files created by the Java compiler are in Java Byte Code, which are
not specific to any platform, and thus cannot be run directly by a processing unit.
Java executable files must be run by a special program installed on a machine
called the Java Virtual Machine (JVM).
The Java Virtual Machine (JVM) is a program that reads instructions from Java Byte Code and
executes them for the platform that the JVM is installed on.
The JVM is often called an interpreter, because it “interprets” java byte code to be run on
whatever machine it is installed on. For this reason, Java is often called an interpreted
language, as opposed to a “true” compiled language
From Source Code to a Running Program
Steps to create and run Java Program:
1. Write the source code in the Java programming
language.
2. Run the source code through the Java Compiler
(Interpreter)
3. Run the resulting Java Byte Code through the JVM
4. The programs executes!
Errors
A compilation will not be successful if there are syntax
errors in the program.
Syntax Errors are grammatical errors in the Java
programming language.
Meaning, if you enter code that the java compiler does not
understand, it will not complete compiling.
There are two other kinds of errors that cannot be caught
by the Java compiler:
Run-Time Errors – Errors that cause the execution of a
program to end abnormally (crash).
Logical Errors – Errors in the programmer’s logic that
cause incorrect results.
Portability
So, why was Java created again?
So the programs written in it can be run on different devices easily.
This means that Java programs are very easily portable.
Portability in programming means that a program can be written on one computer and
run on many different ones.
In traditional compiled languages, you must recompile your program
from the source code for every different machine you want to run it
on.
Tedious
Can run into problems between systems
As long as the machine you want to execute the program has a
compatible version of the JVM, your Java program can be run on it
without recompiling it!
If the JVM is correctly installed, there is (almost…) no problems.
Java Applications and Applets
There are two kinds of programs that can be created with
Java:
Application – stand alone programs that runs on a computer.
Applets – Small applications designed to be transmitted over the
internet and executed in a web browser.
Most web pages are written in Hypertext Markup Language (HTML)
HTML is not very sophisticated, Java allows for much of the power of a
normal application.
What’s the main problem with allowing users to create full-fledged
applications that can be sent over the internet and used in a web browser?
Answer: Security!
Applications have access to memory and secondary storage.
Solution: Applets run in a secure part of memory and cannot access
anything outside of that unless the user specifically states so.
Procedural Programming
There are two primary methods of programming in use
today
The first is Procedural Programming – a programming
methodology in which a program was made up of one of
more procedures.
A procedure is a set of programming statements that,
together, perform a specific task.
Typically, in this model, data is passed between procedures.
Here data and the code that acts on it are entirely separate.
Is there a problem with this?
What if the form of the data changes? If the data and the code
acting on it are completely separate, can it still function?
Object-Oriented Programming
The response to this is Object-Oriented Programming (OOP) – a
programming methodology in which a programs parts are abstracted
into the interaction between Objects.
An object is an abstraction of a real-world (or sometimes not) entity that
consists of data (attributes) and the procedures that act upon the objects data
(methods).
Attributes can be seen as the information kept about the object whether it be
the current state of the object or unchanging characteristics about it, and the
methods can be seen as the behavior of the object that change the state.
In Java the generalization of all the possible individual objects of a kind
is called a class. An individual instance of the class is called an object.
You can think of a class as being a blueprint or recipe and an object as
being a single result from the blueprint or recipe.
Object Example
Example Class: Cup
Attributes:
Total Volume
Current Volume
Color
Dimensions
Methods:
Fill
Pour
Encapsulation and Data Hiding
Two important terms that describe how OOP addresses the data/code
separation that Procedural Programming presents:
Encapsulation refers to the combining of data and code into a single object.
Advantages:
When the data in the object changes, the methods that interact with the data can be easily
located and changed.
Data Hiding refers to the object’s ability to hide its data from outside the object.
Things outside of the object (objects, or other code) can access and manipulate the data inside
of an object through the methods defined by the object.
This has many advantages:
When using an object, you do not need to know the details about how it was implemented,
you just need to know the methods needed to do a task.
Outside sources can only manipulate the data of an object in the ways the object designer
intends.
In Java, almost everything is an object, but you can still program
procedurally.
Your First Java Program
“Hello World”
Every programming language has a “Hello World”
program.
It’s purpose is to do the most basic functionality of a
program: Simply display text.
No computations
No information kept
Just spit out text
Often languages are compared by the “Hello World”
programs in terms of verbosity.
Compiling and Running From Command
Line
You can compile a java source file from either a Windows or
Unix command prompt with the following command:
javac YourProgramName.java
This will create a file called “YourProgramName.class”.
This contains the Java Byte Code for your program.
You can run a java program from either a Windows or Unix
command prompt with the following command:
java YourProgramName
Notes:
Your source file should end in “.java”
When you run your Java program just use the name of your source
file WITHOUT “.java”
// Hello World Program