Introduction to Java Appendix A Introduction to Java

  • Slides: 35
Download presentation
Introduction to Java Appendix A: Introduction to Java

Introduction to Java Appendix A: Introduction to Java

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

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, JOption. Pane, String. Buffer, and String. Tokenizer • To learn how to write and document your own Java classes Appendix A: Introduction to Java 2

Chapter Objectives (continued) • To understand how to use arrays in Java • To

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 Appendix A: Introduction to Java 3

The Java Environment and Classes • • Platform independent Object oriented Can be embedded

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 Appendix A: Introduction to Java 4

Compiling and Executing a Java Program Appendix A: Introduction to Java 5

Compiling and Executing a Java Program Appendix A: Introduction to Java 5

Classes and Objects • The class is the fundamental programming unit • Every program

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 Appendix A: Introduction to Java 6

The Java API • Java consists of small core language augmented by an extensive

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 Appendix A: Introduction to Java 7

The import Statement and the Main Method • Import statement tells the compiler to

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 Appendix A: Introduction to Java 8

Primitive Data Types and Reference Variables • Java distinguishes two kinds of entities •

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 Appendix A: Introduction to Java 9

Primitive Data Types • • Represent numbers, characters, and Boolean values Integers: byte, short,

Primitive Data Types • • Represent numbers, characters, and Boolean values Integers: byte, short, int, and long Real numbers: float and double Characters: char Appendix A: Introduction to Java 10

Primitive Data Types (continued) Appendix A: Introduction to Java 11

Primitive Data Types (continued) Appendix A: Introduction to Java 11

Operators Appendix A: Introduction to Java 12

Operators Appendix A: Introduction to Java 12

Type Compatibility and Conversion • Widening conversion: operations involving mixed-type operands, the numeric type

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 Appendix A: Introduction to Java 13

Referencing and Creating Objects • You can declare reference variables that reference objects of

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 Appendix A: Introduction to Java 14

Java Control Statements • A group of statements executed in sequence is written as

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 Appendix A: Introduction to Java 15

Java Control Statements (continued) Appendix A: Introduction to Java 16

Java Control Statements (continued) Appendix A: Introduction to Java 16

Java Control Statements (continued) Appendix A: Introduction to Java 17

Java Control Statements (continued) Appendix A: Introduction to Java 17

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

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 Appendix A: Introduction to Java 18

Methods (continued) • If the argument is of a class type, the value of

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 Appendix A: Introduction to Java 19

The Class Math • Provides a collection of methods that are useful for performing

The Class Math • Provides a collection of methods that are useful for performing common mathematical operations Appendix A: Introduction to Java 20

Escape Sequences • An escape sequence is a sequence of two characters beginning with

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 Appendix A: Introduction to Java 21

The String Class • String class defines a data type that is used to

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 Appendix A: Introduction to Java 22

Comparing Objects • You can’t use the relational operators or equality operators to compare

Comparing Objects • You can’t use the relational operators or equality operators to compare the values stored in strings or other objects Appendix A: Introduction to Java 23

The String. Buffer Class • Stores character sequences • Unlike a String object, the

The String. Buffer Class • Stores character sequences • Unlike a String object, the contents of a String. Buffer object can be changed Appendix A: Introduction to Java 24

String. Tokenizer Class • We often need to process individual pieces, or tokens, in

String. Tokenizer Class • We often need to process individual pieces, or tokens, in a string Appendix A: Introduction to Java 25

Wrapper Classes for Primitive Types • Sometimes we need to process primitive-type data as

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. Appendix A: Introduction to Java 26

Defining Your Own Classes • Unified Modeling Language is often used to represent a

Defining Your Own Classes • Unified Modeling Language is often used to represent a class • Standard means of documenting class relationships widely used in industry Appendix A: Introduction to Java 27

Defining Your Own Classes (continued) • The modifier private sets the visibility of each

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 Appendix A: Introduction to Java 28

Arrays • In Java, an array is also an object • The elements are

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] Appendix A: Introduction to Java 29

Input/Output using Class JOption. Pane • Prior to Java 2, it was fairly difficult

Input/Output using Class JOption. Pane • Prior to Java 2, it was fairly difficult to perform input/output operations • Java 2 provides JOption. Pane which facilitates the display of dialog windows for input and message windows for output Appendix A: Introduction to Java 30

Input/Output using Class JOption. Pane (continued) Appendix A: Introduction to Java 31

Input/Output using Class JOption. Pane (continued) Appendix A: Introduction to Java 31

Converting Numeric Strings to Numbers • A dialog window always returns a reference to

Converting Numeric Strings to Numbers • A dialog window always returns a reference to a string • Therefore, a conversion is required Appendix A: Introduction to Java 32

Input/Output using Streams • An input stream is a sequence of characters representing program

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 Appendix A: Introduction to Java 33

Chapter Review • A Java program is a collection of classes • JVM enables

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 String. Buffer classes are used to reference objects that store character strings Appendix A: Introduction to Java 34

Chapter Review (continued) • Be sure to use methods such as equals and compare.

Chapter Review (continued) • Be sure to use methods such as equals and compare. To 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 JOption. Pane 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 Appendix A: Introduction to Java 35