Java Programming, Second edition

Download Report

Transcript Java Programming, Second edition

Java Programming, Second
Edition
Chapter One
Creating Your First Java Program
In this chapter, you will:
 Learn about programming
 Understand object-oriented programming





concepts
Learn about the Java programming language
Start a Java program
Add comments to a Java program
Run a Java program
Modify a Java program
Learning about programming
 Program - A set of instructions that you




write to tell a computer what to do
Machine language - The language a
machine works in
High-level programming languages Languages that allow programmers to use
terms such as “read,” “write,” and “add”
Syntax-The rules of the language
Logic – correct statements and order to
produce outcome desired
Converting high level-language
code
 Compiler - All instructions converted at




once from high-level language to machine
language; new program is then run
C/C++, COBOL, FORTRAN, etc. compile
Interpreter - High-level language code is
read and converted at run time line by line
Basic, Perl, PHP, Python are interpreted
Java is compiled, then interpreted
Procedural Programming
 Procedural programming – step-by-step




approach to problem-solving
Variables – hold values
Operations – read, write, math
Procedures are groups of operations
Calls – when a program jumps to a
procedure
OOP Concepts
 OOP is a different approach to problem




solving; conceptualizing objects instead of
steps
Create objects, then applications that use
these objects
Similar to concrete objects in the real
world
Objects have states and methods
States are also called attributes
Methods are functions – blocks of code
OOP Concepts cont.
 Class – a group of objects with common
properties
 Instance – single existing object of a class
 Inheritance – objects inherit attributes from
the parent class
 Encapsulation – “data hiding”; programmer
only needs to know the interface for a
class, not its internal workings
Java Programming Language
 Developed by Sun Microsystems as an
object-oriented language that is both used
for general-purpose business programs
and for interactive World Wide Web-based
Internet programs
Architecturally Neutral
 The Java programming language can be used to
write a program that will run on any platform or
operating system using the JVM (Java Virtual
Machine).
 Java runs on a hypothetical computer known as
the Java virtual machine (JVM)
 When your source code program is compiled
into byte code, an interpreter within the JVM
interprets the byte code and interfaces with the
operating system to produce the program results
Java Program Types
 Java applets – programs embedded in a
Web page
 Java applications – stand-alone programs
 Applications can be
 Console-based: DOS-style input/output from
keyboard to screen, or
 Windowed – GUI interface
Starting a Java Program
 Literal string- A series of characters that will
appear exactly as entered
 Arguments- Information that a method requires to
perform its task
System.out.println(“First Java Program”);
Java class name requirements
 Must begin with a letter of the alphabet,
[or an underscore, or a dollar sign]
 Can only contain letters, digits,
underscores, or dollar signs
 Cannot be a Java reserved keyword
 Cannot be one of the following values:
true, false, or null
Printing a String
 Access Modifier- Defines the
circumstances under which a class can be
accessed
 static- Ensures that the method is
accessible even though no objects of the
class exist
 void- The method does not return any
value when it is called
Program Comments
 Program Comments- Nonexecuting
statements that you add to a program for
the purpose of documentation
 Line comments- //
 Block comments - /* */
 javadoc comments - /** */
Running a Java Program
1. You must compile the program you wrote
(called the source code) into bytecode.
2. You must use the Java interpreter to translate
the bytecode into executable statements.
Running a Java Program
 The commands for compiling and executing are:
 Compiling a program (from the DOS prompt)
 javac Hello2.java
 Executing a program
 java Hello2
Modifying a Program
 To produce the new output:
 Modify the text file that contains the existing
program
 Change the class name
 Change the literal string that currently prints
and then add an additional text string
Errors
 Run-time or Logic Errors- Occur when the
syntax of the program is correct and the
program is compiled, but produces
incorrect results
 Syntax Errors- Programming errors that
occur when you introduce typing errors
into your program
Documentation
 http://java.sun.com has full Java
documentation of every class, method,
and attribute in the Java libraries, plus
tutorials and more