Sie sind auf Seite 1von 23

Java Programming

Fifth Edition
Chapter 1
Creating Your First J ava Classes
Java Programming, Fifth Edition 2
Learning About Programming
Program
Set of written instructions that tells computer what to do
Machine language
Most basic circuitry-level language
Low-level programming language
High-level programming language
Allows you to use vocabulary of reasonable terms
Syntax
Rules of language
Program statements
Similar to English sentences
Carry out tasks of program

Java Programming, Fifth Edition 3
Learning About Programming
(continued)
Compiler or interpreter
Translates language statements into machine code
Syntax error
Misuse of language
Misspelled programming language word
Debugging
Freeing program of all errors
Logic errors
Also called semantic errors
Incorrect order or procedure
Java Programming, Fifth Edition 4
Introducing Object-Oriented
Programming Concepts
Procedural programming
Sets of operations executed in sequence
Variables
Named computer memory locations that hold values
Procedures
Individual operations grouped into logical units
Object-oriented programs
Create classes
Create objects from classes
Create applications
Java Programming, Fifth Edition 5
Understanding Objects and Classes
Objects
Made up of attributes and methods
Attributes
Characteristics that define object
Differentiate objects of same class
Value of attributes is objects state
Class
Describes objects with common properties
Definition
Instance
Understanding Objects and Classes
(continued)
Java Programming, Fifth Edition 6
Java Programming, Fifth Edition 7
Learning About Java
Java
Developed by Sun Microsystems
Object-oriented language
General-purpose
Advantages
Security features
Architecturally neutral
Java (continued)
Can be run on wide variety of computers
Does not execute instructions on computer directly
Runs on hypothetical computer known as Java virtual machine (JVM)
Source code
Programming statements written in high-level programming langua
Java Programming, Fifth Edition 8
Learning About Java (continued)
Bytecode
Statements saved in file
Java compiler converts source code into binary
program
Java interpreter
Checks bytecode and communicates with operating
system
Executes bytecode instructions line by line within
Java virtual machine

Java Programming, Fifth Edition 9
Learning About Java (continued)
Java Programming, Fifth Edition 10
Analyzing a Java Application That
Uses Console Output (continued)
Understanding the Statement
That Prints the Output (continued)
Java Programming, Fifth Edition 11
Java Programming, Fifth Edition 12
Understanding the First Class
Everything used within Java program must be part of a class
Define Java class using any name or identifier
Requirements for identifiers
Must begin with:
Letter of English alphabet
Or non-English letter (such as or )
Cannot begin with digit
Can only contain:
Letters
Digits
Underscores
Dollar signs
Cannot be Java reserved keyword
Cannot be true, false, or null

Java Programming, Fifth Edition 13
Understanding the First Class
(continued)
Understanding the First Class
Java Programming, Fifth Edition 14
Understanding the First Class
(continued)
Java Programming, Fifth Edition 15
Java Programming, Fifth Edition 16
Understanding the main() Method
static
Reserved keyword
Means method accessible and usable
Even though no objects of class exist
void
Use in main() method header
Does not indicate main() method empty
Indicates main() method does not return value
when called
Doesnt mean main() doesnt produce output
Shell Code
Java Programming, Fifth Edition 17
Java Programming, Fifth Edition 18
Adding Comments to a Java Class
Program comments
Nonexecuting statements added to program for documentation
Use to leave notes for yourself or others
Include author, date, classs name or function
Comment out a statement
Compiler does not translate
Types of Java comments
Line comments
Start with two forward slashes (//)
Continue to end of current line
Do not require ending symbol
Block comments
Start with forward slash and asterisk (/*)
End with asterisk and forward slash (*/)

Java Programming, Fifth Edition 19
Adding Comments to a Java Class
(continued)
Types of Java comments (continued)
Javadoc comments
Special case of block comments
Begin with slash and two asterisks (/**)
End with asterisk and forward slash (*/)
Use to generate documentation


Java Programming, Fifth Edition 20
Creating a Java Application
Using GUI Output
JOptionPane
Produce dialog boxes
Dialog box
GUI object resembling window
Messages placed for display
Package
Group of classes
import statement
Use to access built-in Java class
Java Programming, Fifth Edition 21
Creating a Java Application
Using GUI Output (continued)
Java Programming, Fifth Edition 22
Correcting Errors and
Finding Help (continued)
Parsing
Process compiler uses to divide source code into
meaningful portions
Logic error
Syntax correct but produces incorrect results when
executed
Usually more difficult to find and resolve
Java API
Also called the Java class library
Prewritten Java classes
Dont Do It
Files name must match name of class
Dont confuse names parentheses, braces,
brackets, curly braces, square brackets, and angle
brackets
Dont forget to end a block comment
Dont forget that Java is case sensitive
End every statement with semicolon
Do not end class or method headers with semicolon
Recompile when making changes
Java Programming, Fifth Edition 23

Das könnte Ihnen auch gefallen