Lecture 1
Introduction to Java
What is a Computer Program?
• Computers can do many different things
because they are programmable.
• For a computer to be able to do anything
(multiply, play a song, run Microsoft word ), it
must be given the instructions to do so.
• A program is a set of instructions a computer
follows in order to perform a task.
• A programming language is a special
language used to write a computer program.
Such as C, C++, Java, python, etc.
Computer Program
• Algorithm is a set of well defined steps for
performing a task or solving a problem.
• A computer CPU can only process instructions
that are written in machine language, which is a
stream of binary numbers.
• Each different type of CPU has it own machine
language.
– It is related with the architecture & operating system.
• A compiler is a program that translates a
programming language into a machine code.
Recipe Analogy
Comparing a computer program to a food recipe
Food Recipe Computer Program
• a chef writes a set of • a programmer writes a set of
instructions called a recipe instructions called a program
• the recipe requires • the program requires
specific ingredients specific inputs
• the cook follows the • the computer follows the
instruction step-by-step instructions step-by-step
• the food will vary • the output will vary
depending on the amount depending on the values of
of ingredients. the inputs.
What is a program made of?
• There are certain elements that are
common to all programming language.
• Simple java program
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
What is a program made of?
• Key words / Reserved words
– These are words that have a special meaning in the
programming language E.g. public, class
• Operators
– Are symbols or words that perform operations on one or
more operands e.g. = , + & *
• Punctuation
– are characters that specify specific purposes such as
marking the end of statement.
• Variable
– is a named storage location in the computer.
What is a program made of?
• Program defined names – often called identifiers
– Used to identify storage locations in memory or part of
a program defined by the programmer.
• Syntax
– these are rules that must be followed when writing a
program.
– Syntax dictates how operators, key words & punctions
used
• Statement
– Is a complete instruction that causes the computer to
perform some action.
Compiling Programs
• Computers do not understand the languages
(C++, Java, etc.) that programs are written in.
• Programs must first be compiled (converted)
into machine code that the computer can run.
• A compiler is a program that translates a
programming language into machine code.
• During the translation the compiler uncovers
any syntax errors that may be in the program.
Compiling Programs
• Most programing language compilers translate
source code directly into files that contain machine
language.
• Since they are directly executed by the computer
CPU such files are called executable files.
• The Java Compiler, However, translates a java
Source code into a file that contain byte code
instructions.
• JVM (Java Virtual Machine) is a program that reads
java byte code instructions and execute them as
they read.
• JVM is often called a interpreter and Java is
interpreted language.
Multiple Compilers
• Because different operating systems (Windows, Macs,
Unix) require different machine code, you must compile
most programming languages separately for each platform.
program
compiler compiler
compiler
Unix
Win
MAC
Java Interpreter
• Java is a little different.
• Java compiler produces bytecode not
machine code.
• Bytecode can be run on any computer
Win
with the Java interpreter installed.
Java Program Java Bytecode
MAC
compiler Interpreter
Unix
Advantages and Disadvantages of Java
Advantages:
• Java is highly portable. it is platform independent. Once it's
compiled, you can run the bytecode on any machine with a Java
interpreter. You do not have to recompile for each platform.
• Java is safe. Certain common programming bugs and dangerous
operations are prevented by the language and compiler.
• Java standardizes many useful operations like managing
network connections and providing graphical user interfaces.
Disadvantages:
• Running bytecode through the interpreter is not as fast as
running machine code, which is specific to that platform.
• Because it is platform independent, it is difficult to use platform
specific features (e.g., Windows taskbar, quick launch) in Java.
• Java interpreter must be installed on the computer in order to run
Java programs.
Compiling and Running java program
• The software that is used to create java program
is referred to as the JDK (java Development kit)
or SDK (Software Development kit)
• Once you have installed the JDK.
• Open text editor and Write the java code (source
code)
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Compiling and Running java program
• Save the file as HelloWorld.java at c:\java directory. This
file is known as source file.
• To run the program that you just wrote, open the
command prompt and type the following to get the
directory:
cd c:\java
• To compile the program that you wrote, you need to run
the Java Development Tool Kit Compiler as follows:
At the command prompt type:
c:\java> javac HelloWorld.java
• You have now created your first compiled Java program
named HelloWorld.class
• To run your first program, type the following at the
command prompt:
c:\java>java HelloWorld
Compiling and Running java program
• IDE (integrated Development Environment)
• There are several java IDE’s such as Apache
NetBeans.
• These environments consist a text editor,
compiler ,debugger, and other utilities.
• The program is compiled and executed with a
single click of a button.
Programming Paradigms
• There are three type of programming
paradigms in use today:
– Procedural, Functional and Object-Oriented
• Procedural Programming
– list of instructions to tell the computer what to
do step by step.
• Functional Programming
– is an approach to problem solving that treats
every computation as a mathematical function.
– Is about passing data from function to function
to function to get a result.
Object-Oriented Programing
• In object-oriented programming (OOP),
programs are organized into objects.
• Object-oriented programming (OOP) is
about encapsulating data and behavior
into objects.
• The properties (data and behavior) of
objects are determined by their class.
Object
• An object is a software entity that has
State and Behavior.
• Software Objects are often used to
model real-world objects.
• Example: dogs have states (name,
color, hungry, breed) and behaviors
(bark, run, and wag tail).
Object Examples
• Example 1: Dogs
– States: name, color, breed, and “is hungry?”
– Behaviors: bark, run, and wag tail
• Example 2: Cars
– States: color, model, speed, direction
– Behaviors: accelerate, turn, change gears
Class
• A class is a blueprint that defines the states and
the behaviors common to all objects of a certain
kind.
• In the real world, you often have many objects of
the same kind.
• Even though all dogs have four legs, and bark,
each dog’s behavior is independent of other dogs.
• For example: Dog #1 is a black, Dog #2 is a white
Object-oriented Programming
• Object-oriented design has many advantages.
• For example: OOP address the problem of code / data
separations through encapsulations and data hiding.
• Encapsulation
– Refers to combining of data & code into a single object
• Data hiding
– Refers to an objects ability to hide its data from a code
that are outside the class.
• When an object data is hidden form outside code we can
protect the data from accidental corruption.
• In addition, the programming code outside the class does
not need to know about the format or internal structure of
the object’s data.
Parts of Java Program
• Java is an object-oriented programming
language
• E.g. Simple.java
// This is simple java program
public class Simple {
public static void main(String[] args) {
System.out.println(“Programming is fun");
}
}
Parts of Java Program
• Comment
– The // or /* … */ sign marks the beginning a comment
– The Compiler ignores everything in the comment
• Class header - public class Simple
– Marks the beginning of class definition
– Java program must have at least one class definition
– public is a java key word. It is known as class specifier
and controls where the class may be accessed
– All java key words must be written in lowercase letters.
– Java is a case sensitive language
• Simple
– is the class name and made by the programmer.
Parts of Java Program
• Method header – public static void main( String [] args)
– This marks the beginning of a method
– Every java application must have a method named main.
– The main method is the starting point of an application.
• The print and println and Java API
– The print & println methods are used to display text output.
– They are part of java API, which is a collection of
prewritten classes and methods for performing specific
operations.
– System is a class in java API , out is instance of
PrintStream, and is a public member of a system class.
– print & println are out object methods.
End of Lecture 1