35
Introduction to Java Appendix A

Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java

Embed Size (px)

Citation preview

Page 1: Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java

Introduction to Java

Appendix A

Page 2: Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java

Appendix A: Introduction to Java 2

Chapter Objectives

• To understand the essentials of object-oriented programming in Java

• To learn about the primitive data types of Java• To understand how to use the control structures of Java• To learn how to use predefined classes such as Math,

JOptionPane, String, StringBuffer, and StringTokenizer• To learn how to write and document your own Java

classes

Page 3: Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java

Appendix A: Introduction to Java 3

Chapter Objectives (continued)

• To understand how to use arrays in Java• To learn how to perform I/O in Java using simple dialog

windows• To learn how to perform I/O in Java using streams

Page 4: Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java

Appendix A: Introduction to Java 4

The Java Environment and Classes

• Platform independent• Object oriented• Can be embedded in Web pages• JVM is a software computer that runs inside an actual

computer• Java code is first translated into Java byte code

instructions which are then interpreted by the JVM

Page 5: Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java

Appendix A: Introduction to Java 5

Compiling and Executing a Java Program

Page 6: Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java

Appendix A: Introduction to Java 6

Classes and Objects

• The class is the fundamental programming unit• Every program is written as a collection of classes• Class definitions are stored in separate files with the

extension .java and the file name must be the same as the class name

• A class is a named description for a group of entities• A class is a general description of a group of entities that

all have the same characteristics; each entity is an object

Page 7: Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java

Appendix A: Introduction to Java 7

The Java API

• Java consists of small core language augmented by an extensive collection of packages

• Each package contains a collection of related Java classes, such as:• Swing• AWT• util

Page 8: Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java

Appendix A: Introduction to Java 8

The import Statement and the Main Method

• Import statement tells the compiler to make the names defined in a specified package accessible to the code file

• The main function identifies where the JVM begins execution of an application program

• Keywords “public static void” tell the compiler that main is accessible outside of the class, is static, and does not return a value

Page 9: Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java

Appendix A: Introduction to Java 9

Primitive Data Types and Reference Variables

• Java distinguishes two kinds of entities• Primitive types• Objects

• Primitive type data is stored in primitive type variables• Objects are associated with reference variables which

store an object’s address

Page 10: Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java

Appendix A: Introduction to Java 10

Primitive Data Types

• Represent numbers, characters, and Boolean values• Integers: byte, short, int, and long• Real numbers: float and double• Characters: char

Page 11: Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java

Appendix A: Introduction to Java 11

Primitive Data Types (continued)

Page 12: Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java

Appendix A: Introduction to Java 12

Operators

Page 13: Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java

Appendix A: Introduction to Java 13

Type Compatibility and Conversion

• Widening conversion: operations involving mixed-type operands, the numeric type of the smaller range is converted to the numeric type of the larger range

• In an assignment operation, a numeric type of smaller range can be assigned to a numeric type of larger range

Page 14: Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java

Appendix A: Introduction to Java 14

Referencing and Creating Objects

• You can declare reference variables that reference objects of specified types

• Two reference variables can reference the same object• The new operator creates an instance of a class• A constructor executes when a new object is created

Page 15: Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java

Appendix A: Introduction to Java 15

Java Control Statements

• A group of statements executed in sequence is written as a compound statement delimited by braces

• The statements execute in the order in which they are listed

• Control Statements alter the sequential flow of execution

Page 16: Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java

Appendix A: Introduction to Java 16

Java Control Statements (continued)

Page 17: Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java

Appendix A: Introduction to Java 17

Java Control Statements (continued)

Page 18: Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java

Appendix A: Introduction to Java 18

Methods

• Programmers use methods to define a group of statements that perform a particular operation

• The modifier static indicates a static or class method• A method that is not static is an instance method• All method arguments are call-by-value• If the argument is a primitive type, its value is passed to

the method• The method can’t modify the argument value and

have the modification remain after return from the method

Page 19: Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java

Appendix A: Introduction to Java 19

Methods (continued)

• If the argument is of a class type, the value of the reference variable is passed, not the value of the object itself

• Reference variables point to the object and any modification to the object will remain after return from the method

Page 20: Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java

Appendix A: Introduction to Java 20

The Class Math

• Provides a collection of methods that are useful for performing common mathematical operations

Page 21: Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java

Appendix A: Introduction to Java 21

Escape Sequences

• An escape sequence is a sequence of two characters beginning with the character \

• Represents characters or symbols that have a special meaning in Java

Page 22: Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java

Appendix A: Introduction to Java 22

The String Class

• String class defines a data type that is used to store a sequence of characters

• You cannot modify a String object• If you attempt to do so, Java will create a new object

that contains the modified character sequence

Page 23: Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java

Appendix A: Introduction to Java 23

Comparing Objects

• You can’t use the relational operators or equality operators to compare the values stored in strings or other objects

Page 24: Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java

Appendix A: Introduction to Java 24

The StringBuffer Class

• Stores character sequences• Unlike a String object, the contents of a StringBuffer

object can be changed

Page 25: Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java

Appendix A: Introduction to Java 25

StringTokenizer Class

• We often need to process individual pieces, or tokens, in a string

Page 26: Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java

Appendix A: Introduction to Java 26

Wrapper Classes for Primitive Types

• Sometimes we need to process primitive-type data as objects

• Java provides a set of classes called wrapper classes whose objects contain primitive-type values: Float, Double, Integer, Boolean, Character, etc.

Page 27: Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java

Appendix A: Introduction to Java 27

Defining Your Own Classes

• Unified Modeling Language is often used to represent a class• Standard means of documenting class relationships

widely used in industry

Page 28: Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java

Appendix A: Introduction to Java 28

Defining Your Own Classes (continued)

• The modifier private sets the visibility of each variable or constant to private visibility• These data fields can be accessed only within the

class definition• Only class members with public visibility can be

accessed outside of the class• Constructors initialize the data fields within a class

Page 29: Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java

Appendix A: Introduction to Java 29

Arrays

• In Java, an array is also an object• The elements are indexes and are referenced using a

subscripted variable of the form arrayname[subscript]

Page 30: Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java

Appendix A: Introduction to Java 30

Input/Output using Class JOptionPane

• Prior to Java 2, it was fairly difficult to perform input/output operations

• Java 2 provides JOptionPane which facilitates the display of dialog windows for input and message windows for output

Page 31: Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java

Appendix A: Introduction to Java 31

Input/Output using Class JOptionPane (continued)

Page 32: Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java

Appendix A: Introduction to Java 32

Converting Numeric Strings to Numbers

• A dialog window always returns a reference to a string• Therefore, a conversion is required

Page 33: Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java

Appendix A: Introduction to Java 33

Input/Output using Streams

• An input stream is a sequence of characters representing program data

• An output stream is a sequence of characters representing program output

• The console keyboard stream is System.in• The console window is associated with System.out

Page 34: Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java

Appendix A: Introduction to Java 34

Chapter Review

• A Java program is a collection of classes• JVM enables a Java program written for one machine to

execute on any other machine that has a JVM• Java defines a set of primitive data types that are used

to represent numbers, characters, and Boolean data• The control structures of Java are similar to those found

in other languages• The Java String and StringBuffer classes are used to

reference objects that store character strings

Page 35: Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java

Appendix A: Introduction to Java 35

Chapter Review (continued)

• Be sure to use methods such as equals and compareTo to compare the contents of two String objects

• You can declare your own Java classes and create objects of these classes using the new operator

• A class has data fields and instance methods• Array variables can reference array objects• Class JOptionPane can be used to display dialog

windows for data entry and message windows for output• The stream classes in package java.io read strings from

the console and display strings to the console