L01 - Introduction To Java
L01 - Introduction To Java
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
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.
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
• 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.