chapter 2 getting started with java. topics components of a java program –classes –methods...
Post on 20-Dec-2015
215 views
TRANSCRIPT
Chapter 2
Getting Started with Java
Topics
• Components of a Java Program– classes– methods– comments– import statements
• Declaring and creating objects– Java identifiers
• Standard Java classes
The First Java Program
• This first program displays a window on the
screen.– The size of the window is 300 x 200 pixels– The title is My First Java Program.
• The fundamental OOP concept illustrated by the program:– An object-oriented program uses objects.
The First Java Program
/* Chapter 2 Sample Program: Displaying a WindowFile: Ch2Sample1.java
*/import javax.swing.*;
class Ch2Sample1 {
public static void main(String[ ] args){
JFrame myWindow;
myWindow = new JFrame();
myWindow.setSize(300, 200); myWindow.setTitle(“My First Java Program”);
myWindow.setVisible(true);}
}
UML diagram for Ch2Sample1
• This diagram shows the messages sent to myWindow
Class diagram for Ch2Sample1
• This diagram shows the dependency relationship between the classes in the program.– The Ch2Sample1 class uses a JFrame object.
• To use an object in a program, first we declare and create and object. Then we send messages to it.
Declaring Objects
• When we declare an object, we must give it a name and say what class it belongs to.– syntax:
– the name must be a valid Java identifier
– the class must be defined and available– if multiple objects are declared, the names are separated by
commas
Java Identifiers
• A Java identifier is a sequence of letters, digits, underscores, and dollar signs used to name a class, object, method, etc.– The first character in a Java identifier must be a
letter.– Uppercase and lowercase letters are
distinguished.• myWindow, mywindow, MYWINDOW
– No spaces are allowed in an identifier.
Java Naming Conventions
• Use of an uppercase letter for the first letter of class names– class Account
• Use a lowercase letter for the first letter of object names.– object account
• Use all upper case letters for constants – final int WINDOW_HEIGHT
Instantiating Objects
• No object is created by the declaration.
• We create an object by invoking the new operation:
– Example
object declaration and object creation
• declaration – assigns a name to a
memory address that will store the location of an object
• instantiation – sets aside memory for
the objects data
– stores the address of the objects data in the named memory location
Diagram to show memory state
• program diagram (or object diagram)
• state-of-memory diagram– uses similar notation to UML but includes extra
symbols
Another instantiation example
• The state after two new commands are executed.
– allocating a new object means the address of the original is forgotten
– an object with no references to it is garbage
Methods and Messages
• Syntax for sending messages
– The expressions “sending a message” and “calling a method” are synonymous.
– The object that receives a message must possess a corresponding method.
– Example:
Program Components
• Comments
• Import Statement
• Class Declaration
• Method Declaration
Comments
• Comments are used to state the purpose of the program, explain the meaning of code, and provide other descriptions to help programmers use and understand the code.– A single-line comment is preceeded by //– A comment is any sequence of text that begins
with the marker /* and ends with the marker */.– A javadoc comment begins with /** and ends
with */
Using Classes from Java Library
• In Java, classes are grouped into packages, and the Java system comes with numerous packages.
• To use a class from a package, we refer to the class in the program by using the following syntax:
<package name>.<class name>
• This notation is called dot notation.
import statements
• Using the fully qualified name of a class can be cumbersome.
• Using the import statement solves this problem.
import javax.swing.*;
• Now we can just use the name of the class
Class definition
• A Java program is composed of one or more classes.
• The syntax for declaring a class is
– A class member is either a data value or a method.
– Most classes will contain some of each
class <class name> {<class member declarations>
}
The main class
• One of the classes in the program must be designated as the main class.
• If we designate a class as a main class, we must define a method called main, because when a Java program is executed, the main method of a main class is executed first.
• The main class is sometimes called a driver class
Method Definition
• Syntax
• Example:public static void main(String[ ] args){
JFrame myWindow;
myWindow = new JFrame();
myWindow.setSize(300, 200); myWindow.setTitle(“My First Java Program”);
myWindow.setVisible(true);}
methodheadermethod bodyparametersmethod namemodifierreturn type
<modifiers> <return type> <method name>( parameters) { <method body> }
Template for Java Program
/* program information*/
import javax.swing.*;
class Ch2Sample1 {
public static void main(String[ ] args){
JFrame myWindow;
....}
}
commentsimport statementsclass declaraionmethod declarationmethod bodyclass body
Edit-Compile-Run Cycle
Editing
• Step One: Edit the program.– Type in the program, using a text editor, and
save the program to a file. – Use the name of the main class and the
suffix .java for the filename. – This file is called a source file.
Compiling
• Step 2: Compile the source file.– The process of compiling the source file creates
the bytecode file.• the bytecode file is binary
– The name of the compiler-generated bytecode file will have the suffix .class while its prefix is the same as the source file’s.
– The class file will not be created if there are syntax errors in your program
• You'll get a list of error messages
Running the Program
• Step 3: Execute the bytecode file.– A java interpreter will go through the bytecode
file and execute the instructions in it. – If your program is error-free, a window will
appear on the screen.– If an error occurs while running the program,
the interpreter will catch it and stop its execution.