An Introduction To Computer, Programming and Java

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 45

Lecture 1

An Introduction to Computer, Programming and Java

1
What is a Computer?
A computer is a machine that performs computations,
logical operations, or more generally, data manipulation
according to some prescribed sequence of instructions
called a computer program.

The physical components of a computer are termed


hardware and the programs software.

2
The Hardware
The major hardware components:

 The central processing unit (CPU)


 Primary or random access memory (RAM)
 Secondary or long term memory
 Input and output devices (I/O devices)

3
The Central Processing Unit
The CPU does:
 the computing
 the processing
 the majority of the work

Important components of the CPU:


 arithmetic and logic unit (ALU)
 control unit (CU)
 clock

4
The Central Processing Unit
 The ALU performs calculations, billions per second
 The CU controls or coordinates which calculations the
ALU performs
 The CPU clock determines how frequently the computer
hardware executes instructions.
 A system’s hardware components are synchronized with
the clock. Every time the clock ticks, another hardware
action occurs.

5
Primary or Random Access Memory
How Data is Stored
 binary format
 a sequence of 0’s and 1's called bits.

ASCII encoding:
 ‘a’ is represented by 01100001
 ‘b’ is encoded as 01100010

A sequence of eight bits is called a byte.

6
Where Data is Stored
 When the CPU executes a program, the program
instructions, along with relevant data, are stored in
primary memory.

 Primary memory is also known as random access memory


(RAM) because data may be retrieved or accessed in
random, rather than sequential, order.

7
Where Data Is Stored
 You can conceptualize RAM as a collection of storage cells or
boxes, each capable of holding just a single byte of
information.

 A unique number, or memory address, identifies each such


storage cell.

8
Secondary Memory
 Long term
 Permanent storage

Secondary memory devices:


 hard disks
 tapes
 CDs
 flash memory sticks.

9
Secondary Memory
 The programs that you use every day such as word
processors, spreadsheets, and games are permanently
stored on secondary storage devices.

 Compared to RAM, secondary memory is, in general,


cheaper (per bit), slower, larger, electromechanical rather
than electronic, and persistent: secondary memory devices
do not lose their values when you turn off the computer.

10
Input/Output Devices
Standard input devices: Output devices:

 keyboards  monitors
 mouses  printers
 joysticks  speakers
 stylus pens
 cameras
 microphones

11
The Software
 The programs that run on a computer are collectively known
as software. Word processors, internet browsers, editors,
database management systems, computer games, and
spreadsheets are all part of your computer's software library.

 When you turn on or boot your computer, a program called


the operating system automatically runs. This special
program provides an interface between you and your
computer.

12
Programming Languages
Machine Language  Assembly Language  High-Level
Language

Machine language is a set of primitive instructions built into


every computer. The instructions are in the form of binary
code, so you have to enter binary codes for various
instructions. Program with native machine language is a
tedious process. Moreover the programs are highly difficult
to read and modify. For example, to add two numbers, you
might write an instruction in binary like this:
1101101010011010

13
Programming Languages

Assembly languages were developed to make programming


easy. Since the computer cannot understand assembly
language, however, a program called assembler is used to
convert assembly language programs into machine code.
For example, to add two numbers, you might write an
instruction in assembly code like this:
ADDF3 R1, R2, R3
Assembly Source File
Machine Code File


ADDF3 R1, R2, R3
Assembler …
1101101010011010
… …

14
Programming Languages

The high-level languages are English-like and easy to


learn and program. For example, the following is a
high-level language statement that adds two
numbers:

total = 3 + 4;

15
Popular High-Level Languages
 COBOL (COmmon Business Oriented Language)
 FORTRAN (FORmula TRANslation)
 BASIC (Beginner All-purpose Symbolic Instructional Code)
 Pascal (named for Blaise Pascal)
 Ada (named for Ada Lovelace)
 C (whose developer designed B first)
 Visual Basic (Basic-like visual language developed by
Microsoft)
 Delphi (Pascal-like visual language developed by Borland)
 C++ (an object-oriented language, based on C)
 C# (a Java-like language developed by Microsoft)
 Java (We use it in the book)
16
Programming and Algorithms
 An algorithm is a finite, step-by-step procedure for
accomplishing some task or solving a problem.

 The study of algorithms is a basis of computer science.

 A programming language is your tool, a tool that you can


use to investigate and implement algorithms.

17
Compiling Source Code
A program written in a high-level language is called a source
program. A computer cannot understand a source program.
Program called a compiler is used to translate the source
program into a machine language program called an object
program. The object program is often then linked with other
supporting library code before the object can be executed on
the machine.

Source File Compiler Machine-language


Linker Executable File
File

Library Code

18
The Compiler
 The program must be translated into the machine language
of that computer.

 Accepts a program written in a high-level language and


produces a translation into the target machine language.
Java
 General-purpose language developed by Sun Microsystems in
the early 1990s, which became part of Oracle Corporation in
2010. Three main goals for their new language:

 Platform Independence - Java programs should be capable


of running on any computer.

 Security - Java programs should not be susceptible/open to


hackers' code and dangerous viruses.

 Reliability - Java programs should not "crash.”

20
The Java Virtual Machine
 In order to make Java a cross-platform programming
language, Java's creative team designed an abstract
computer implemented in software called the Java Virtual
Machine (JVM). You install software on your computer
that simulates a JVM computer. The machine language of
the JVM is called bytecode. Java programs are first
compiled into bytecode, and then executed.

21
The Java Virtual Machine
 The Java interpreter, which is part of the JVM, executes
each bytecode instruction, one by one.

Once a Java program is translated into bytecode, the


bytecode can run on any computer that has installed the
JVM. A Java program needs to be compiled into
bytecode just once.
Java, Web, and Beyond
 Java can be used to develop any applications.
 Java Applets
 Java Web Applications
 Java can also be used to develop applications for
hand-held devices such as Palm, cell phones, PC
Tablets, etc.

23
PDA and Cell Phone

24
Java’s History
 James Gosling and Sun Microsystems
 Oak
 HotJava
 The first Java-enabled Web browser
 Bought by Oracle Corporation in January 2010
 Java History Website:

http://en.wikipedia.org/wiki/Java_(programming_language)

25
Characteristics of Java
 Java Is Simple
 Java Is Object-Oriented
 Java Is Distributed
 Java Is Interpreted
 Java Is Robust
 Java Is Secure
 Java Is Architecture-Neutral
 Java Is Portable
 Java's Performance
 Java Is Multithreaded
 Java Is Dynamic
http://www.cs.armstrong.edu/liang/intro8e/JavaCharacteristics.pdf
26
Characteristics of Java
 Java Is Simple Java is partially modeled on C++, but greatly
simplified and improved. Some people refer to
 Java Is Object-Oriented Java as "C++--" because it is like C++ but
 Java Is Distributed with more functionality and fewer negative
aspects.
 Java Is Interpreted
 Java Is Robust
 Java Is Secure
 Java Is Architecture-Neutral
 Java Is Portable
 Java's Performance
 Java Is Multithreaded
 Java Is Dynamic
27
Characteristics of Java
 Java Is Simple Java is inherently object-oriented.
 Java Is Object-Oriented Although many object-oriented languages
began strictly as procedural languages,
 Java Is Distributed Java was designed from the start to be
 Java Is Interpreted object-oriented. Object-oriented
programming (OOP) is a popular
 Java Is Robust programming approach that is replacing
 Java Is Secure traditional procedural programming
techniques.
 Java Is Architecture-Neutral
 Java Is Portable One of the central issues in software
development is how to reuse code. Object-
 Java's Performance oriented programming provides great
 Java Is Multithreaded flexibility, modularity, clarity, and
reusability through encapsulation,
 Java Is Dynamic
inheritance, and polymorphism.
28
Characteristics of Java
 Java Is Simple Distributed computing involves several
computers working together on a network.
 Java Is Object-Oriented Java is designed to make distributed
 Java Is Distributed computing easy. Since networking
capability is inherently integrated into
 Java Is Interpreted Java, writing network programs is like
 Java Is Robust sending and receiving data to and from a
file.
 Java Is Secure
 Java Is Architecture-Neutral
 Java Is Portable
 Java's Performance
 Java Is Multithreaded
 Java Is Dynamic
29
Characteristics of Java
 Java Is Simple You need an interpreter to run Java
 Java Is Object-Oriented programs. The programs are compiled into
the Java Virtual Machine code called
 Java Is Distributed bytecode. The bytecode is machine-
 Java Is Interpreted independent and can run on any machine
that has a Java interpreter, which is part of
 Java Is Robust the Java Virtual Machine (JVM).
 Java Is Secure
 Java Is Architecture-Neutral
 Java Is Portable
 Java's Performance
 Java Is Multithreaded
 Java Is Dynamic
30
Characteristics of Java
 Java Is Simple Java compilers can detect many problems
that would first show up at execution time
 Java Is Object-Oriented in other languages.
 Java Is Distributed
Java has eliminated certain types of error-
 Java Is Interpreted prone programming constructs found in
 Java Is Robust other languages.
 Java Is Secure
Java has a runtime exception-handling
 Java Is Architecture-Neutral feature to provide programming support
 Java Is Portable for robustness.
 Java's Performance
 Java Is Multithreaded
 Java Is Dynamic
31
Characteristics of Java
 Java Is Simple
 Java Is Object-Oriented
 Java Is Distributed
 Java Is Interpreted
 Java Is Robust Java implements several security
mechanisms to protect your system against
 Java Is Secure harm caused by stray programs.
 Java Is Architecture-Neutral
 Java Is Portable
 Java's Performance
 Java Is Multithreaded
 Java Is Dynamic
32
Characteristics of Java
 Java Is Simple
 Java Is Object-Oriented
 Java Is Distributed
 Java Is Interpreted
 Java Is Robust
 Java Is Secure
Write once, run anywhere
 Java Is Architecture-Neutral
 Java Is Portable With a Java Virtual Machine (JVM),
you can write one program that will
 Java's Performance run on any platform.
 Java Is Multithreaded
 Java Is Dynamic
33
Characteristics of Java
 Java Is Simple
 Java Is Object-Oriented
 Java Is Distributed
 Java Is Interpreted
 Java Is Robust
 Java Is Secure
 Java Is Architecture-Neutral
Because Java is architecture neutral,
 Java Is Portable Java programs are portable. They can
 Java's Performance be run on any platform without being
recompiled.
 Java Is Multithreaded
 Java Is Dynamic
34
Characteristics of Java
 Java Is Simple
 Java Is Object-Oriented
 Java Is Distributed
 Java Is Interpreted
 Java Is Robust
 Java Is Secure
Because Java is interpreted, the
 Java Is Architecture-Neutral bytecode is not directly executed by
 Java Is Portable the system, but is run through the
 Java's Performance interpreter. However, its speed is
more than adequate for most
 Java Is Multithreaded interactive applications, where the
 Java Is Dynamic CPU is often idle, waiting for input
or for data from other sources.
35
Characteristics of Java
 Java Is Simple
 Java Is Object-Oriented
 Java Is Distributed
 Java Is Interpreted
 Java Is Robust
 Java Is Secure
 Java Is Architecture-Neutral
 Java Is Portable
 Java's Performance Multithread programming is smoothly
integrated in Java, whereas in other
 Java Is Multithreaded languages you have to call procedures
 Java Is Dynamic specific to the operating system to enable
multithreading.
36
Characteristics of Java
 Java Is Simple
 Java Is Object-Oriented
 Java Is Distributed
 Java Is Interpreted
 Java Is Robust
 Java Is Secure
 Java Is Architecture-Neutral
 Java Is Portable Java was designed to adapt to an evolving
 Java's Performance environment. New code can be loaded on the
fly without recompilation. There is no need for
 Java Is Multithreaded developers to create, and for users to install,
major new software versions. New features can
 Java Is Dynamic
be incorporated transparently as needed.
37
JDK Versions
 JDK 1.02 (1995)
 JDK 1.1 (1996)
 JDK 1.2 (1998)
 JDK 1.3 (2000)
 JDK 1.4 (2002)
 JDK 1.5 (2004) a. k. a. JDK 5 or Java 5
 JDK 1.6 (2006) a. k. a. JDK 6 or Java 6
 JDK 1.7 (2010) a. k. a. JDK 7 or Java 7

http://www.oracle.com/technetwork/java

38
JDK Editions
 Java Standard Edition (J2SE)
 J2SE can be used to develop client-side
standalone applications or applets.
 Java Enterprise Edition (J2EE)
 J2EE can be used to develop server-side
applications such as Java servlets and Java
ServerPages.
 Java Micro Edition (J2ME).
 J2ME can be used to develop applications for
mobile devices such as cell phones.

39
A Simple Java Program
Listing 1.1

//This program prints Welcome to Java!


public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

40
Creating and Editing Using NotePad
To use NotePad, type
notepad Welcome.java
from the command prompt.

41
Creating and Editing Using WordPad
To use WordPad, type
write Welcome.java
from the command prompt.

42
Creating, Compiling, and Running Programs
Create/Modify Source Code

Source code (developed by the programmer)


Saved on the disk
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!"); Source Code
}
}

Compile Source Code


Byte code (generated by the compiler for JVM i.e., javac Welcome.java
to read and interpret, not for you to understand)

Method Welcome() If compilation errors
0 aload_0 stored on the disk

Bytecode
Method void main(java.lang.String[])
0 getstatic #2 …
3 ldc #3 <String "Welcome to
Java!">
5 invokevirtual #4 …
8 return Run Byteode
i.e., java Welcome

Result

43 If runtime errors or incorrect result


Compiling and Running Java from the
Command Window
 Set path to JDK bin directory
 set path=c:\Program Files\java\jdk1.7.0\bin
 Set classpath to include the current directory
 set classpath=.
 Compile
 javac Welcome.java
 Run
 java Welcome

44
Acknowledgement
 The lecture notes for this course include slides from:
 Daniel Liang – Introduction to Java Programming, 2011
 Antonio Martinez – Java Programming: From the Ground
Up, 2010

45

You might also like