Download presentation
Presentation is loading. Please wait.
1
Getting Started with Java
Chapter 2 Getting Started with Java
2
Topics Components of a Java Program Declaring and creating objects
classes methods comments import statements Declaring and creating objects Java identifiers Standard Java classes
3
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.
4
The First Java Program /* Chapter 2 Sample Program: Displaying a Window File: 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); }
5
UML diagram for Ch2Sample1
This diagram shows the messages sent to myWindow
6
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.
7
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
8
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.
9
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
10
Instantiating Objects
No object is created by the declaration. We create an object by invoking the new operation: Example
11
object declaration and object creation
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
12
Diagram to show memory state
program diagram (or object diagram) state-of-memory diagram uses similar notation to UML but includes extra symbols
13
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
14
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:
15
Program Components Comments Import Statement Class Declaration
Method Declaration
16
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 */
17
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.
18
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
19
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
20
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
21
Method Definition Syntax Example:
22
Template for Java Program
23
Edit-Compile-Run Cycle
24
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.
25
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
26
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.