Docstoc

Chapter 1 Introduction to Java_6_

Document Sample
Chapter 1 Introduction to Java_6_ Powered By Docstoc
					Chapter 1 Introduction to Computers, Programs, and Java

Chapter 1 - Introduction to Computers, Programs, and Java

1

Objectives









Review computer basics, programs, and operating systems. Understand the relationship between Java and the World Wide Web. Know Java’s advantages. Understand the terms API, IDE, and JDK. Write a simple Java program.

Chapter 1 - Introduction to Computers, Programs, and Java

2

Objectives
 

 

Create, compile, and run Java programs Understand the Java Runtime Environment (JRE). Learn the basic syntax of a Java program. To display output on the console and on the dialog box.

Chapter 1 - Introduction to Computers, Programs, and Java

3

What is a Computer?


A computer consists of storage devices, memory, CPU, communication devices, input devices and output devices.
Bus

Storage Devices e.g., Disk, CD, and Tape

Memory

CPU

Communication Devices e.g., Modem, and NIC

Input Devices e.g., Keyboard, Mouse

Output Devices e.g., Monitor, Printer

Chapter 1 - Introduction to Computers, Programs, and Java

4

How Data is Stored?
 



 



Data are encoded as a series of bits (zeros and ones). Computers use zeros and ones because digital devices have two stable states, which are referred to as zero and one by convention. The encoding scheme varies. For example, character ‘J’ is represented by 01001010 in one byte. A small number such as three can be stored in a single byte. If computer needs to store a large number that cannot fit into a single byte, it uses a number of adjacent bytes. No two data can share or split a same byte. A byte is the minimum storage unit.

Memory address

Memory content

. . . 2000 2001 2002 2003 2004

. . . 01001010 01100001 01110110 01100001 00000011 Encoding for character ‘J’ Encoding for character ‘a’ Encoding for character ‘v’ Encoding for character ‘a’ Encoding for number 3

Chapter 1 - Introduction to Computers, Programs, and Java

5

Programs








Computer programs, known as software, are instructions to the computer. You tell a computer what to do through programs. Without programs or software, a computer is an empty machine. Computers do not understand human languages, so you need to use computer languages to communicate with them. Programs are written using programming languages.

Chapter 1 - Introduction to Computers, Programs, and Java

6

Programming Languages


Machine language
 



is a set of primitive instructions built into every computer. the instructions are in the form of binary code. Program with native machine language is a tedious process. programs are highly difficult to read and modify. For example, to add two numbers, you might write an instruction in binary like this:

1101101010011010
Chapter 1 - Introduction to Computers, Programs, and Java

7

Programming Languages


Assembly languages
 



is a low-level language which is machine-dependent 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 (or mnemonic) : ADDF3 R1, R2, R3
Assembly Source File … ADDF3 R1, R2, R3 … Machine Code File

Assembler

… 1101101010011010 …

Chapter 1 - Introduction to Computers, Programs, and Java

8

Programming Languages


High-level languages
 

are English-like and easy to learn and program. For example, the following is a high-level language statement that computes the area of a circle with radius 5 : area = 5 * 5 * 3.1415;

Chapter 1 - Introduction to Computers, Programs, and Java

9

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) 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 (whose developer designed B first) Java (We use it in this module)
Chapter 1 - Introduction to Computers, Programs, and Java

10

Compiling Source Code






A program written in a high-level language is called a source program. A program called a compiler is used to translate the source program into a machine language program (or 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

Object File

Linker

Excutable File

Chapter 1 - Introduction to Computers, Programs, and Java

11

Compiling Java Source Code
 





A source program can be ported to any machine. With appropriate compilers, you can compile the source program into a special type of object code, known as bytecode. The bytecode can then run on any computer with a Java Virtual Machine (JVM) which interprets Java bytecode. Java language has the advantage of "Write once, run anywhere" (WORA), or "Write once, run everywhere" (WORE),
Java Bytecode Java Virtual Machine Any Computer

Chapter 1 - Introduction to Computers, Programs, and Java

12

Why Java?


Java enables users to develop and deploy applications on

 

the internet for web servers, desktop computers, and small hand-held devices

Chapter 1 - Introduction to Computers, Programs, and Java

13

Java, Web, and Beyond


Java can be used to develop web applications :
Java applets (must be run by a web browser)  Java servlets and JavaServer Pages (JSP) (must be run on a web server)




Java can also be used to develop applications for hand-held devices such as


Palm, and cell phones

Chapter 1 - Introduction to Computers, Programs, and Java

14

Examples of Java’s Versatility


Standalone Application: TicTacToe



Applet: TicTacToe

Chapter 1 - Introduction to Computers, Programs, and Java

15

Examples of Java’s Versatility


Servlets: SelfTest Web site



Mobile Computing: Cell phones

Chapter 1 - Introduction to Computers, Programs, and Java

16

Background on Java
 




First appeared in 1995 Developed by Sun Microsystems Corp. Cross platform = platform-independent In May 2007, Sun made available most of their Java technologies as free software under GNU General Public License.

Chapter 1 - Introduction to Computers, Programs, and Java

17

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 Is Multithreaded Java Is Dynamic


www.cs.armstrong.edu/liang/intro6e/JavaCharacteristics.pdf

Chapter 1 - Introduction to Computers, Programs, and Java

18

Characteristics of Java


Java Is Simple




Java is partially modeled on C++, but greatly simplified and improved. Some people refer to Java as "C++--" because it is like C++ but with more functionality and fewer negative aspects.



Java Is Object-Oriented




Object-oriented programming (OOP) is a popular programming approach that is replacing traditional procedural programming techniques. OOP provides great flexibility, modularity, clarity, and reusability through encapsulation, inheritance, and polymorphism.

Chapter 1 - Introduction to Computers, Programs, and Java

19

Characteristics of Java


Java Is Distributed




Distributed computing involves several computers working together on a network. Java is designed to make distributed computing easy since networking capability is inherently integrated into Java



Java Is Interpreted



The source programs are compiled into the Java Virtual Machine (JVM) code called bytecode. The bytecode is machine-independent and can run on any machine that has a Java interpreter, which is part of the JVM.

Chapter 1 - Introduction to Computers, Programs, and Java

20

Characteristics of Java


Java Is Robust





Java compilers can detect many problems that would first show up at execution time. Java has eliminated certain types of error-prone programming constructs. Java has a runtime exception-handling feature.



Java Is Secure


Java implements several security mechanisms to protect your system against harm caused by stray programs.
Write once, run anywhere (WORA) Because it is architecture neutral
Chapter 1 - Introduction to Computers, Programs, and Java



Java Is Architecture Neutral




Java Is Portable


21

Characteristics of Java


Java Is Multithreaded


Multithread programming is smoothly integrated in Java, whereas in other languages you have to call procedures specific to the operating system to enable multithreading.



Java Is Dynamic
 

Java was designed to adapt to an evolving environment. New features can be incorporated transparently as needed.

Chapter 1 - Introduction to Computers, Programs, and Java

22

Java Development Kit (JDK)


Versions


JDK 1.5 (2004)


also called JDK 5 or Java 5 or J2SE 5



JDK 1.6 (it is the latest version)



JDK Editions


Java Standard Edition (J2SE)


to develop client-side standalone applications or applets. to develop server-side applications such as Java servlets and Java ServerPages.



Java Enterprise Edition (J2EE)




Java Micro Edition (J2ME)


to develop applications for mobile devices such as cell phones.



This module will use J2SE only.
Chapter 1 - Introduction to Computers, Programs, and Java

23

Java IDE Tools


IDE (Integrated Development Environment)


help programmers to easily edit, compile, build, debug Java programs in one graphical user interface



Some Popular Java IDE tools
 


 

Eclipse Open Source by IBM [www.eclipse.org] TextPad Editor [www.textpad.com] NetBeans Open Source by Sun [www.netbeans.org] Sun ONE Studio by Sun MicroSystems Borland Jbuilder [www.borland.com]
Chapter 1 - Introduction to Computers, Programs, and Java

24

A Simple Java Program


Listing 1.1 Welcome.java
//This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }

Chapter 1 - Introduction to Computers, Programs, and Java

25

Creating and Editing Using Notepad
To use Notepad, type
notepad Welcome.java

from the DOS prompt.

Chapter 1 - Introduction to Computers, Programs, and Java

26

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

Byte code (generated by the compiler for JVM to read and interpret, not for you to understand)

Compile Source Code i.e., javac Welcome.java
If compilation errors stored on the disk

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

Bytecode

i.e., java

Run Bytecode

Welcome

Result
If runtime errors or incorrect result

Chapter 1 - Introduction to Computers, Programs, and Java

27

Compiling and Running Java from the Command Window


Set JAVA_HOME environment variable


set JAVA_HOME="c:\Program Files\java\jdk1.5.0"



Add a search path to JDK bin directory


set path=%path%;%JAVA_HOME%\bin



Add the current directory to classpath


set classpath=.;%classpath%



Compile


javac Welcome.java



Run


java Welcome
Chapter 1 - Introduction to Computers, Programs, and Java

28

Anatomy of a Java Program
        

Comments Package (will be introduced later) Reserved words Modifiers Statements Blocks Classes Methods The main method
Chapter 1 - Introduction to Computers, Programs, and Java

29

Comments


Styles of comment


Line comment


comments are preceded by two slashes (//) in a line comments are enclosed between /* and */ in one or multiple lines.



Paragraph or Block comment




Javadoc comment


begin with /** and end with */

 

Compiler will ignore comments. Purposes
 

to explain the purpose and design of the program to skip execution of certain program statements which may cause errors
Chapter 1 - Introduction to Computers, Programs, and Java

30

Comments
/** * Class description goes here. * @version 1.10 04 Oct 1996 * @author Firstname Lastname */ // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { String testMessage; // store greeting words /* temporarily not to display message System.out.println("Welcome to Java!"); */ } }
Chapter 1 - Introduction to Computers, Programs, and Java

31

Reserved Words or Keywords


are words that have a specific meaning to the compiler and cannot be used for other purposes in the program


The URL from Sun shows the listing of Reserved Words http://java.sun.com/docs/books/tutorial/java/nutsandbolts/_key words.html



reserved words are underlined :

//This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }
Chapter 1 - Introduction to Computers, Programs, and Java

32

Reserved Words or Keywords


Java Keywords
abstract assert boolean break byte case catch char class const continue default do double else enum extends final finally float false for if goto implements import instanceof int interface long native new package private protected public return short static strictfp super switch synchronized this throw throws transient try void volatile while

 

Boolean literals (values)
true

Null value
null

Chapter 1 - Introduction to Computers, Programs, and Java

33

Modifiers


are reserved words which specify the properties of the data, methods, and classes and how they can be used.


The URL from Sun shows the listing of Reserved Words



http://java.sun.com/j2se/1.4.2/docs/api/java/la ng/reflect/Modifier.html Common modifiers you will learn are :
public, private, protected  static  final  abstract

Chapter 1 - Introduction to Computers, Programs, and Java

34

Statements
 



 

they represents an action or a sequence of actions. Every statement in Java ends with a semicolon (;). A compound statement consists of a sequence of statements enclosed in a pair of braces { }. A statement can span multiple lines A statement is underlined :
//This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }
Chapter 1 - Introduction to Computers, Programs, and Java

35

Blocks


A pair of braces {} in a program forms a block that groups components of a program.

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

Class block Method block

Chapter 1 - Introduction to Computers, Programs, and Java

36

Classes
 





a class is a template or blueprint for objects. The mystery of the class will continue to be unveiled throughout this book. For now, though, understand that a program is defined by using one or more classes. Related classes can also be grouped into a single package for easy distribution

Chapter 1 - Introduction to Computers, Programs, and Java

37

Methods


What is System.out.println?




println is a pre-defined method in the Java class library. To learn the usage of some predefined Java classes or methods


read the JDK API (Application Programming Interface) http://java.sun.com/reference/api/ documentation



Programmers can create their own methods
Chapter 1 - Introduction to Computers, Programs, and Java

38

main Method





The main method provides the control of program flow. It is the starting point of program exectuion. The main method always looks like this:
public static void main(String[] args) { }

Chapter 1 - Introduction to Computers, Programs, and Java

39

Message Dialog Box


Listing 1.2 WelcomeInMessageDialogBox.java
import javax.swing.JOptionPane; public class WelcomeInMessageDialogBox { public static void main(String[] args) { JOptionPane.showMessageDialog( null , "Welcome to Java!" , "Display Message" , JOptionPane.INFORMATION_MESSAGE); } }

Chapter 1 - Introduction to Computers, Programs, and Java

40

Message Dialog Box
 

showMessageDialog is a method in the JOptionPane class. With the import statement, the compiler can


locate the JOptionPane class from imported packages java.lang.System.out.println can be simplified to System.out.println JOptionPane.showMessageDialog must be written as javax.swing.JOptionPane.showMessageDialog



java.lang package is implicitly imported




Without the import statement,




To import all classes in the javax.swing package :


import javax.swing.*

Chapter 1 - Introduction to Computers, Programs, and Java

41

Methods Can be Overloaded


JOptionPane.showMessageDialog(null, x, y, JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(null, x);




x parameter is a string for the text to be



displayed, and y parameter is a string for the title of the message dialog box.

Chapter 1 - Introduction to Computers, Programs, and Java

42

Blank Lines and White Spaces


White space






is a set of characters which includes newline, space and tab is ignored by compiler, but it makes program more readable This program looks messy, but it can run !
public class Welcome { public static void main(String[] args) { System.out.println( "Welcome to Java!"); }}
Chapter 1 - Introduction to Computers, Programs, and Java

43

Some Rules for Beginners


Naming convention


Class name should be capitalized in each word. E.g. WelcomeInMessageDialogBox .java for source program .class for compiled bytecode file



File extensions
 





Source file name must have the same name of the public class declared Java is case-sensitive


Compiler treats main and Main are different
Chapter 1 - Introduction to Computers, Programs, and Java

44


				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:6
posted:11/19/2009
language:English
pages:44