Introduction To Java Programming Chapter 1 & 2: Week 1
Introduction To Java Programming Chapter 1 & 2: Week 1
Chapter 1 & 2
Week 1
Java History
• 1991 – Green Team started by Sun Microsystems
• *7 handheld controller for multiple entertainment systems
• There was a need for a programming language that would run on
various devices
• Java (first named Oak) was developed for this purpose
Introduction
• Java enabled web browser (HotJava) demonstrated at 1995 Sun World
conference
• Java is incorporated into Netscape shortly after
• Java is “cross platform”, meaning that it can run on various computer
operating systems
Java Applications and Applets
• Java programs can be of two types:
• Applications
• Stand-alone programs that run without the aid of a web browser
• Relaxed security model since the user runs the program locally
• Applets
• Small applications that require the use of a Java enabled web browser to run
• Enhanced security model since the user merely goes to a web page and the applet runs
itself
Programming Languages
Common Language Elements
• In this example, the variable hours is created as an integer and assigned the
value of 40
Programming Languages
Variables
• Variables are simply a name given to represent a place in memory
Programming Languages
Variables
The Compiler and the Java Virtual Machine
• A programmer writes Java programming statements for a program
• These statements are known as source code
• A text editor is used to edit and save a Java source code file
• Source code files have a .java file extension
• A compiler is a program that translates source code into an executable
form
The Compiler and the Java Virtual Machine
• A compiler is run using a source code file as input
• Syntax errors that may be in the program will be discovered during
compilation
• Syntax errors are mistakes that the programmer has made that violate
the rules of the programming language
• The compiler creates another file that holds the translated
instructions
The Compiler and the Java Virtual Machine
• Most compilers translate source code into executable files containing
machine code
• The Java compiler translates a Java source file into a file that contains
byte code instructions
• Byte code instructions are the machine language of the Java Virtual
Machine (JVM) and cannot be directly executed by the CPU
The Compiler and the Java Virtual Machine
• Byte code files end with the .class file extension
• The JVM is a program that emulates a micro-processor
• The JVM executes instructions as they are read
• JVM is often called an interpreter
• Java is often referred to as an interpreted language
Program Development Process
Portability
• Portable means that a program may be written on one type of computer
and then run on a wide variety of computers, with little or no modification
• Java byte code runs on the JVM and not any particular CPU; therefore,
compiled Java programs are highly portable
• JVMs exist on many platforms:
• Windows
• Unix
• Mac
• BSD
• Linux etc.
Portability
• With most programming languages, portability is achieved by
compiling a program for each CPU it will run on
• Java provides a JVM for each platform so that programmers do not
have to recompile for different platforms
Portability
Java Versions
• The software you use to write Java programs is called the Java
Development Kit, or JDK
• There are different editions of the JDK:
• Java SE – Java2 Standard Edition
• Java EE – Java2 Enterprise Edition
• Java ME – Java2 Micro Edition
Parts of a Java Program
• A Java source code file contains one or more Java classes.
• If more than one class is in a source code file, only one of them
may be public.
• The public class and the filename of the source code file must
match.
ex: A class named Simple must be in a file named Simple.java
• Each Java class can be separated into parts.
Parts of a Java Program
• See example: Simple.java
• To compile the example:
• javac Simple.java
• Notice the .java file extension is needed.
• This will result in a file named Simple.class being created.
• To run the example:
• java Simple
• Notice there is no file extension here.
• The java command assumes the extension is .class.
Analyzing The Example
This is a Java comment. It is
// This is a simple Java program. ignored by the compiler.
}
Analyzing The Example
This is the method header
// This is a simple Java program. for the main method. The
main method is where a Java
public class Simple application begins.
{
public static void main(String[] args)
{ This area is the body of the main method.
All of the actions to be completed during
} the main method will be between these curly braces.
}
Analyzing The Example
// This is a simple Java program.
\t tab Causes the cursor to skip over to the next tab stop
\b backspace Causes the cursor to back up, or move left, one position