introduction to computer systems and the java programming language

26
Introduction to Computer Systems and the Java Programming Language

Upload: clyde-allison

Post on 01-Jan-2016

226 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Introduction to Computer Systems and the Java Programming Language

Introduction to Computer Systems and the

Java Programming Language

Page 2: Introduction to Computer Systems and the Java Programming Language

Computer systems

A computer is a system made up of components Two categories of components:

– hardware: electronic and mechanical parts – software: programs and data

Main hardware components of a computer system– Processor (CPU)– Main memory – Secondary memory – Input devices (keyboard, mouse)– Output devices (monitor, printer)

Page 3: Introduction to Computer Systems and the Java Programming Language

Processor

Central processing unit (CPU), processor chip “Brain” of the computer Contains the logic circuitry controlling the interpretation

and execution of machine instructions– programmed instructions– arithmetic and logical operations on data– controls input/output functions

Instructions – electronic operations– executed one at a time– millions per second– collection of instructions executable program

Page 4: Introduction to Computer Systems and the Java Programming Language

Computer memory

Holds data and programs Main memory

– temporary memory– stores programs and data when the processor is

actively using them– random access memory (RAM)

Secondary memory – permanent– stores (saves) programs and data on a long-term

basis– hard disk, floppy disks

Page 5: Introduction to Computer Systems and the Java Programming Language

Binary

In both main and secondary memory, information is stored as patterns of bits

Binary “Two states”– 1 and 0– true and false– on and off

Binary devices – can be in just one of two possible states at a time

A single binary value is called a bit– binary digit

Page 6: Introduction to Computer Systems and the Java Programming Language

Example

A light switch is a binary device– holds one bit of information– can be in one of two states: on or off– can not be in any other possible state

A light dimmer is not a binary device– can be on, off, or some state in-between– difficult to characterize in-between states

• how to turn a light exactly 50% or “half” on? • how to tell “how much” a light is on?

Page 7: Introduction to Computer Systems and the Java Programming Language

Binary representation

Bits can be used to represent patterns Specifically, any system or set of symbols can be

translated into bit patterns– patterns of ones and zeros– 10100001101

Example: characters from any language alphabet Require enough bits so that all symbols have a

unique bit pattern to represent them– How many bits are needed to represent the

English alphabet? Require set of symbols is finite

Page 8: Introduction to Computer Systems and the Java Programming Language

Bits and Bytes

Bit – single binary or 0/1 value

One bit of information is so little that usually computer memory is organized into groups of eight bits

Byte: group of eight bits– kilobyte (KB): 210 = 1024 bytes – megabyte (MB): 220 = 1,048,576 bytes

• 1MB = 210 KB– gigabyte (GB): 230 = 1,073,741,824 bytes

• 1GB = 210 MB

Page 9: Introduction to Computer Systems and the Java Programming Language

High-level programming languages

Most programs are created using a high level programming language – closer to human language than machine language

(low-level)– Java, C, C++, Pascal, FORTRAN– easier to read, write, and maintain

Source programs are translated to executable (machine language) programs by a compiler

Different programming languages require different compilers

Language can have many compilers– computer type, software package

Page 10: Introduction to Computer Systems and the Java Programming Language

Source program Executable program

Create source program using a text editor– written (typed) instructions in a high-level language

Save source program on disk Compile source program

– compiler translates source program to executable program

– source program remains unchanged– a new executable program is created – executable program is saved on disk

Run executable program– copied into main memory and run by processor

Page 12: Introduction to Computer Systems and the Java Programming Language

HelloWorld.java

import java.awt.*;public class HelloWorld extends java.applet.Applet { TextField t; public void init() {

t = new TextField(50); t.setText(“Hello World!"); add(t);

}}

Page 13: Introduction to Computer Systems and the Java Programming Language

Java programs

Java programs are created as text files using a text editor (like emacs!)

Save to disk with.java file extension HelloWorld.java

The file contains characters (stored as bytes) – file can be printed, displayed on monitor, or

edited– file cannot be directly executed (run) by the

computer system Java must first translate the program into

bytecodes before it can be run

Page 14: Introduction to Computer Systems and the Java Programming Language

Bytecodes

Java bytecode– machine instruction for the Java processor

Java compiler javac translates the source program into bytecodes

Bytecode file has same name as the source program with a with.class file extension

HelloWorld.class

HelloWorld.java javac HelloWorld.class

source programJava

compilerJava bytecodes

Page 15: Introduction to Computer Systems and the Java Programming Language

Java Virtual Machine (JVM)

Bytecode (class) file will contain exactly the same bytecodes no matter what computer system is used

Bytecode file is executed by a Java bytecode interpreter – processor specific executable program

Each type of computer system has its own Java interpreter that can run on that system.

Any computer system can execute Java bytecode programs if it has a Java interpreter

Computers with Java interpreters are called Java Virtual Machines– a “computer” with a Java processor that can run

Java bytecodes

Page 16: Introduction to Computer Systems and the Java Programming Language

Java applets

An applet is a Java bytecode program that runs on a Web browser

Most newer Web browsers have Java interpreters

Web pages on the Internet contain instructions that send Java bytecodes to your computer

Web browser runs the Java applet with its built-in interpreter

Page 17: Introduction to Computer Systems and the Java Programming Language

Data Types

Computer memory stores arbitrary bit patterns Meaning of a bit pattern depends its use The particular pattern used for a particular string of bits

is a data type. – uses bits to represent values– values are any kind of data a computer can process– all values are represented using some data type

Example: What does the following pattern of 16 bits represent?

0000000001100111 No way to know without more information If data type is short (a Java type) it represents 103

Page 18: Introduction to Computer Systems and the Java Programming Language

Java data types

Primitive

– types of data that are so fundamental ways to represent them are built into Java

Reference

– reference to (an address of) the value or set of values represented by the variable

Page 19: Introduction to Computer Systems and the Java Programming Language

Primitive data types

Primitive data values use fixed number of bytes

There are exactly eight primitive data types:

byte, short, int, long, float, double, char, boolean

A programmer can not create new primitive data types

Any data type you invent will be a type of object

Page 20: Introduction to Computer Systems and the Java Programming Language

Java primitive data types

Primitive Type Description Range

byte 8-bit integer -128 to 127

short 16-bit integer -32768 to 32767

int 32-bit integer -2147483648 to 2147483647

long 64-bit integer -263 to 263-1

float 32-bit floating point 10-46 to 1038

double 64-bit floating point 10-324 to 10308

char Unicode character

boolean Boolean variable false and true

Page 21: Introduction to Computer Systems and the Java Programming Language

Declaration and initialization

Declaration: type <variable-name>;

type <variable-name> = <value>; Variable names

– any combination of letters, numbers, and the underscore character

– may not start with number– may not be reserved word (int, return)– may not be same as method name– case-sensitive (num and Num are different)

Page 22: Introduction to Computer Systems and the Java Programming Language

Examples

int x, y, z; int sum = 0; float f; double pi = 3.14; char first = ‘T’, middle = ‘L’, last = ‘B’;

char first = ‘T’; char middle = ‘L’; char last = ‘B’;

Page 23: Introduction to Computer Systems and the Java Programming Language

Basic operators

Operator Java Description

Assignment = assigns rhs to lhs

Arithmetic +,-,*,/,%addition, subtraction, multiplication, division, remainder

Unary -,++,--negative, auto increment, auto decrement

Equality ==, != equals to, not equals to

Relational <,<=,>,>=less than, less than or equals to, greater than, greater than or equals to

Logical &&,||,! AND, OR, NOT

Page 24: Introduction to Computer Systems and the Java Programming Language

Conditional statements

if-else

if( expression ){ … statements …}else{ … statements …}

Page 25: Introduction to Computer Systems and the Java Programming Language

Conditional statements

while

while( expression ){ … statements …}

for

for( initialization; test; update ){ … statements …}

Page 26: Introduction to Computer Systems and the Java Programming Language

Java methods

int sum_between(int x, int y)

{

int k, sum = 0;

for(k = x; k <= y; k++)

{

sum = sum+k;

}

return sum;

}