Introduction to Java Programming
Y. Daniel Liang
Introduction
What is a computer?
– Electronic devices that stores and process data – Includes both hardware and software
Hardware – physical aspects that you can see
– – – – Central Processing Unit (CPU) Memory (main memory) Storage Devices (hard disk, floppy disk, CDs, tapes) Input and Output Devices (monitors, keyboards, mice, printers) – Communication Devices (modems and network interface cards)
Hardware
Central Processing Unit (CPU)
– Brain of a computer – Retrieves instructions from memory and executes them – 2 components:
control unit
– controls and coordinates the actions of the other components
arithmetic/logic unit
– perform numeric operations (addition, subtraction, multiplication, division) – Perform logic operations (comparison)
Built on semiconductor chip with millions of transistors
Hardware
Memory
(main memory)
– Data are encoded as a series of bits. – Bit – binary digit: zero and one – Stores data and program instructions for CPU to execute – A memory unit is an ordered sequence of bytes, each holding 8 bits – Encoding scheme: ASCII
Hardware
Memory (main memory)
– A program and its data must be brought to memory before they can be executed – The content of memory is lost when new infor is placed in it. – Each byte has a unique address, used to locate the byte. – Can be accessed in any order, thus is called RAM (Random-access memory) – 1 Megabyte (MB) is about 1 million bytes. – Also built on silicon semiconductor ships containing thousands of transistors, but less complicated. – Memory is volatile, the content is lost when the power is turned off.
Hardware
Storage Devices (hard disk, floppy disk, CDs, tapes)
– Programs and data are stored permanently on storage devices and moved to memory when computer actually uses them. – 4 main types of storage devices:
Disk drives (hard disks, floppy disks) CD drives (CD-R, CD-RW, DVD). DVD stands for Digital versatile disc. Tape drives USB flash drives
Hardware
Input
and Output Devices (monitors, keyboards, mice, printers)
– Allows user to communicate with the computer – Common input devices: keyboards, mice – Common output devices: monitors, printers.
Hardware
Communication
Devices (modems and network interface cards)
– To create network of computers
Software
Software/computer program
– invisible instructions that control the hardware and make it perform specific tasks. – Tell computer what to do in computer/machine language
Software
Computer languages
1. Machine language 2. Assembly language 3. High level language
Software
Machine
language
– a set of primitive instructions built into every computer – Different for different types of computers. – The instructions are in the form of binary code, e.g. to add 2 numbers (very tedious):
1101101010011010
Software
Assembly
language
– Low-level programming language which uses mnemonic to represent machine-language instructions – E.g.: ADDF3 R1, R2, R3 – Assembly code need to be converted into machine code by using an assembler – Assembly program
platform dependent Combination of mnemonic and machine instruction
is
Software
High-level
language
– English-like and easy to learn and program. – E.g.:
Area
= 5 * 5 * 3.1415;
– COBOL, FORTRAN, BASIC, Pascal, Ada, C, Visual Basic, Delphi, C++, C#, Java – Source program is compiled into machine code by a compiler and linked to supporting library code by a linker to form an executable file. Fig. 1.4
Software
High-level
language (continue)
– Can port/move a source program to any machine with appropriate compilers but the source program must be recompiled – Java program, compiled it once into intermediate machine code known as bytecode. Bring the bytecode to any computer with a JVM (Java Virtual Machine). JVM interprets the bytecode into the machine codes and execute them.
An overview of the java development process.
Source: http://java.sun.com/docs/books/tutorial/getStarted/intro/definition.html
Through the Java VM, the same application is capable of running on multiple platforms.
Source: http://java.sun.com/docs/books/tutorial/getStarted/intro/definition.html
Software
Compiling
versus interpreting
– Compiling translates the high-level code into a target language code as a single unit – Interpreting translates the individual steps in the high level code one at a time. Each step is execute immediately after it is translated.
Software
Operating system (OS)
– – – – Software that controls and manages the systems Fig. 1.6, page 36. E.g.: Windows (98, NT, XP, ME), MacOS, Linux etc. Major tasks
Controlling and monitoring system activities (security, input, output, file directories, make sure programs running together do not interfere with each other) Allocating and assigning system resources to program Scheduling operations
– Multiprogramming – multiple programs to run simultaneously by sharing CPU – Multithreading – allows concurrency within a program; it subunits can run at the same time (e.g. editing and saving at the same time) – Multiprocessing/parallel processing – use 2 or more processors together to perform a task.
What Is Java?
History Characteristics
of Java
History
James Gosling Oak – 1991, for embedded consumer electronic appliances Renamed Java, 1995, for developing Internet applications HotJava
– The first Java-enabled Web browser
Java applets – java programs that run from a Web browser
– http://javaboutique.internet.com/movingtree/
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
Java IDE Tools
Inprise
JBuilder Visual J++ Café
Microsoft Symantec Forte
by Sun MicroSystems
IBM
Visual Age for Java
6.0 (free, open-source IDE, runs on
NetBeans JCreator
Windows, Linux, Solaris, and the MacOS)
LE 4.0 (free) by Xinox Software
Java Language Specification, API, JDK and IDE
Java Language Specification
– Technical definition of the language which includes the syntax and semantics of the Java Programming language (java.sun.com/docs/books/jls)
API
– Contains predefined classes and interfaces for developing Java programs. – 3 editions of Java API
J2SE - version Java SE 6.0 (jdk1.6.0_02) J2EE J2ME
Java Language Specification, API, JDK and IDE
JDK
– A set of programs for developing and testing Java program, each of which is invoked from a command line.
IDE
(integrated development environment)
– Software that provides integrated development environment (editing, compiling, building, debugging and online help) for rapidly developing Java program
Getting Started with Java Programming
A
Simple Java Application Programs
Compiling
Executing
Applications
A Simple Application
Example 1.1
//This application program prints Welcome //to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } Source Run
Creating, compiling and executing a Java Programs
Fig.
1.11, page: 44.
Compiling Programs
On command line – javac file.java
Java Source File
Compiler
Bytecode
Executing Applications
On command line – java classname
Bytecode
Java Interpreter on Windows
Java Interpreter on Linux
...
Java Interpreter on Sun Solaris
Example
javac Welcome.java java Welcome output:...
Anatomy of a Java program
Comments Reserved Modifier Statement
words
Block
Class Method The
main method
Anatomy of a Java program
Comments
– – – – Documents the program for understanding purpose Ignored by compiler // /* */
Reserved words or keywords
– Words that have specific meaning to the compiler and cannot be used for other purposes in the program – E.g: public, static, class, void
Anatomy of a Java program
Modifier
– Certain reserved words are modifiers that specify the properties of the data, methods and class and how they can be used. – E.g: public, private, static, final, abstract, protected.
Statement
– Represents an action or a sequence of actions – Ends with semicolon (;)
Block
– Groups the components of the program – Begins with opening brace { and ends with a closing brace } – Class block, method block
Anatomy of a Java program
Class
– Program is defined by using one or more class – Will be covered in more details later.
Method
– A collection of statements that perform a sequence of operations. – Can be used without fully understanding how it works. – Invoke by calling the method name with the requirement argument
The main method
– A special method where the program execution begins. – JVM invokes the main method to execute an application.
Summary