Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java."— Presentation transcript:

1 Introduction to Java Appendix A

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

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

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

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

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

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

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

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

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

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

12 Appendix A: Introduction to Java12 Operators

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

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

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

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

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

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

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

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

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

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

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

24 Appendix A: Introduction to Java24 The StringBuffer Class Stores character sequences Unlike a String object, the contents of a StringBuffer object can be changed

25 Appendix A: Introduction to Java25 StringTokenizer Class We often need to process individual pieces, or tokens, in a string

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

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

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

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

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

31 Appendix A: Introduction to Java31 Input/Output using Class JOptionPane (continued)

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

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

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

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


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

Similar presentations


Ads by Google