Chapter 1 Introduction to Computers, Programs, and Java - PowerPoint PPT Presentation

1 / 44
About This Presentation
Title:

Chapter 1 Introduction to Computers, Programs, and Java

Description:

For example, to add two numbers, you might write an instruction in binary like this: ... you can compile the source program into a special type of object code, ... – PowerPoint PPT presentation

Number of Views:296
Avg rating:3.0/5.0
Slides: 45
Provided by: yda57
Category:

less

Transcript and Presenter's Notes

Title: Chapter 1 Introduction to Computers, Programs, and Java


1
Chapter 1Introduction to Computers, Programs,
and Java
2
Objectives
  • Review computer basics, programs, and operating
    systems.
  • Understand the relationship between Java and the
    World Wide Web.
  • Know Javas advantages.
  • Understand the terms API, IDE, and JDK.
  • Write a simple Java program.

3
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.

4
What is a Computer?
  • A computer consists of storage devices, memory,
    CPU, communication devices, input devices and
    output devices.

5
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.

6
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.

7
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

8
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

9
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

10
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)

11
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.

12
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),

13
Why Java?
  • Java enables users to develop and deploy
    applications on
  • the internet for web servers,
  • desktop computers, and
  • small hand-held devices

14
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

15
Examples of Javas Versatility
  • Standalone Application TicTacToe
  • Applet TicTacToe

16
Examples of Javas Versatility
  • Servlets SelfTest Web site
  • Mobile Computing Cell phones

17
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.

18
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/JavaCharacteris
    tics.pdf

19
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.

20
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.

21
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.
  • Java Is Architecture Neutral
  • Write once, run anywhere (WORA)
  • Java Is Portable
  • Because it is architecture neutral

22
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.

23
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.
  • Java Enterprise Edition (J2EE)
  • to develop server-side applications such as Java
    servlets and Java ServerPages.
  • Java Micro Edition (J2ME)
  • to develop applications for mobile devices such
    as cell phones.
  • This module will use J2SE only.

24
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

25
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("Wel
come to Java!")
26
Creating and Editing Using Notepad
  • To use Notepad, type
  • notepad Welcome.java
  • from the DOS prompt.

27
Creating, Compiling, and Running Programs
28
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 pathpathJAVA_HOME\bin
  • Add the current directory to classpath
  • set classpath.classpath
  • Compile
  • javac Welcome.java
  • Run
  • java Welcome

29
Anatomy of a Java Program
  • Comments
  • Package (will be introduced later)
  • Reserved words
  • Modifiers
  • Statements
  • Blocks
  • Classes
  • Methods
  • The main method

30
Comments
  • Styles of comment
  • Line comment
  • comments are preceded by two slashes (//) in a
    line
  • Paragraph or Block comment
  • comments are enclosed between / and / in one or
    multiple lines.
  • 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

31
Comments
  • /
  • Class description goes here.
  • _at_version 1.10 04 Oct 1996
  • _at_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!")
  • /

32
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/_keywords.html
  • reserved words are underlined

//This program prints Welcome to Java! public
class Welcome public static void
main(String args) System.out.println("Wel
come to Java!")
33
Reserved Words or Keywords
  • Java Keywords
  • abstract continue for new switch
  • assert default if package synchronized
  • boolean do goto private this
  • break double implements protected throw
  • byte else import public throws
  • case enum instanceof return transient
  • catch extends int short try
  • char final interface static void
  • class finally long strictfp volatile
  • const float native super while
  • Boolean literals (values)
  • true false
  • Null value
  • null

34
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
    /lang/reflect/Modifier.html
  • Common modifiers you will learn are
  • public, private, protected
  • static
  • final
  • abstract

35
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("Wel
come to Java!")
36
Blocks
  • A pair of braces in a program forms a block
    that groups components of a program.

37
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

38
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) documentation
  • Programmers can create their own methods

http//java.sun.com/reference/api/
39
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)

40
Message Dialog Box
  • Listing 1.2 WelcomeInMessageDialogBox.java

import javax.swing.JOptionPane public class
WelcomeInMessageDialogBox public static void
main(String args) JOptionPane.showMessageD
ialog( null , "Welcome to Java!"
, "Display Message" , JOptionPane.INFORMATI
ON_MESSAGE)
41
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 package is implicitly imported
  • java.lang.System.out.println can be simplified
    to System.out.println
  • Without the import statement,
  • JOptionPane.showMessageDialog must be written
    asjavax.swing.JOptionPane.showMessageDialog
  • To import all classes in the javax.swing package
  • import javax.swing.

42
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.

43
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!")
44
Some Rules for Beginners
  • Naming convention
  • Class name should be capitalized in each word.
    E.g. WelcomeInMessageDialogBox
  • File extensions
  • .java for source program
  • .class for compiled bytecode file
  • Source file name must have the same name of the
    public class declared
  • Java is case-sensitive
  • Compiler treats main and Main are different
Write a Comment
User Comments (0)
About PowerShow.com