chapter 2 getting started with java. topics components of a java program –classes –methods...

26
Chapter 2 Getting Started with Java

Post on 20-Dec-2015

215 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Chapter 2 Getting Started with Java. Topics Components of a Java Program –classes –methods –comments –import statements Declaring and creating objects

Chapter 2

Getting Started with Java

Page 2: Chapter 2 Getting Started with Java. Topics Components of a Java Program –classes –methods –comments –import statements Declaring and creating objects

Topics

• Components of a Java Program– classes– methods– comments– import statements

• Declaring and creating objects– Java identifiers

• Standard Java classes

Page 3: Chapter 2 Getting Started with Java. Topics Components of a Java Program –classes –methods –comments –import statements Declaring and creating objects

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.

Page 4: Chapter 2 Getting Started with Java. Topics Components of a Java Program –classes –methods –comments –import statements Declaring and creating 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);}

}

Page 5: Chapter 2 Getting Started with Java. Topics Components of a Java Program –classes –methods –comments –import statements Declaring and creating objects

UML diagram for Ch2Sample1

• This diagram shows the messages sent to myWindow

Page 6: Chapter 2 Getting Started with Java. Topics Components of a Java Program –classes –methods –comments –import statements Declaring and creating objects

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.

Page 7: Chapter 2 Getting Started with Java. Topics Components of a Java Program –classes –methods –comments –import statements Declaring and creating objects

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

Page 8: Chapter 2 Getting Started with Java. Topics Components of a Java Program –classes –methods –comments –import statements Declaring and creating objects

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.

Page 9: Chapter 2 Getting Started with Java. Topics Components of a Java Program –classes –methods –comments –import statements Declaring and creating objects

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

Page 10: Chapter 2 Getting Started with Java. Topics Components of a Java Program –classes –methods –comments –import statements Declaring and creating objects

Instantiating Objects

• No object is created by the declaration.

• We create an object by invoking the new operation:

– Example

Page 11: Chapter 2 Getting Started with Java. Topics Components of a Java Program –classes –methods –comments –import statements Declaring and creating objects

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

Page 12: Chapter 2 Getting Started with Java. Topics Components of a Java Program –classes –methods –comments –import statements Declaring and creating objects

Diagram to show memory state

• program diagram (or object diagram)

• state-of-memory diagram– uses similar notation to UML but includes extra

symbols

Page 13: Chapter 2 Getting Started with Java. Topics Components of a Java Program –classes –methods –comments –import statements Declaring and creating objects

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

Page 14: Chapter 2 Getting Started with Java. Topics Components of a Java Program –classes –methods –comments –import statements Declaring and creating objects

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:

Page 15: Chapter 2 Getting Started with Java. Topics Components of a Java Program –classes –methods –comments –import statements Declaring and creating objects

Program Components

• Comments

• Import Statement

• Class Declaration

• Method Declaration

Page 16: Chapter 2 Getting Started with Java. Topics Components of a Java Program –classes –methods –comments –import statements Declaring and creating objects

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 */

Page 17: Chapter 2 Getting Started with Java. Topics Components of a Java Program –classes –methods –comments –import statements Declaring and creating objects

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.

Page 18: Chapter 2 Getting Started with Java. Topics Components of a Java Program –classes –methods –comments –import statements Declaring and creating objects

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

Page 19: Chapter 2 Getting Started with Java. Topics Components of a Java Program –classes –methods –comments –import statements Declaring and creating objects

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>

}

Page 20: Chapter 2 Getting Started with Java. Topics Components of a Java Program –classes –methods –comments –import statements Declaring and creating objects

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

Page 21: Chapter 2 Getting Started with Java. Topics Components of a Java Program –classes –methods –comments –import statements Declaring and creating objects

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> }

Page 22: Chapter 2 Getting Started with Java. Topics Components of a Java Program –classes –methods –comments –import statements Declaring and creating objects

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

Page 23: Chapter 2 Getting Started with Java. Topics Components of a Java Program –classes –methods –comments –import statements Declaring and creating objects

Edit-Compile-Run Cycle

Page 24: Chapter 2 Getting Started with Java. Topics Components of a Java Program –classes –methods –comments –import statements Declaring and creating objects

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.

Page 25: Chapter 2 Getting Started with Java. Topics Components of a Java Program –classes –methods –comments –import statements Declaring and creating objects

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

Page 26: Chapter 2 Getting Started with Java. Topics Components of a Java Program –classes –methods –comments –import statements Declaring and creating objects

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.