Presentation is loading. Please wait.

Presentation is loading. Please wait.

George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.

Similar presentations

Presentation on theme: "George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4."— Presentation transcript:

1 George Blank University Lecturer

2 CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4

3 Elements of Java This chapter is a catalog of the Java language. It contains a lot of information in a highly compressed format. Some students may wish to use another supplementary Java textbook that has a lot of exercises to practice different features of the language instead of having so many at once. There are lots of such texts in bookstores.

4 Java Uniqueness Purely object-oriented—no compromises! Primitive types are not objects No multiple inheritance (simulate with interfaces) No GOTO statement 16 bit international characters only (Unicode) Exception handling Automatic memory management (garbage collection) Multithreaded programming.

5 Standardization In many computer languages, there are differences between implementations of the language on different platforms or with different compilers. For example, C long integers might have different maximum and minimum values. It is a strength of Java that everything is standardized on all platforms.

6 Variables and Types A variable is a location in memory where values can be stored and referenced. Variables are associated with types, which have particular data sizes and a restricted set of valid operations that can be performed on them. Variable declarations assign an identifier, a type, and may declare an initial value. Many types have a default value. For example, an uninitialized integer defaults to 0.

7 Variable (a memory location) address a value (a default value) a name a type The address or value may be passed as a parameter

8 Constructors A constructor is a special form of method that has only one purpose—to create an instance of a class. This is different from other methods, which add behavior to a class. It is good Java practice to supply a no-arguments constructor in addition to any that take parameters to allow the Java Virtual Machine to create instances of the class dynamically at run time. The text shows two constructors for Point (next slide).

9 Point example (text, p. 104) public class Point { public double x, y; public Point() { //no-arg constructor x = 0.0; y = 0.0; } public Point(double init_x, double init_y) { x = init_x; y = init_y; } … } Here, the no-arg constructor supplies default values for the point location.

10 Singleton Classes Classes that are not supposed to have more than one instance at a time, such as a top- level window or a system timer, can be limited as a singleton class, an instance of the Singleton design pattern that will be discussed in a future lecture. The following slide shows a singleton class.

11 Singleton Class Note the private static constructor that prevents multiple instances.

12 Reference Types References to classes, interfaces and arrays are implemented in Java as 32 bit pointers and are called reference types. While C and C++ pointers can be cast to other types, that is forbidden in Java. No direct manipulation of reference types is permitted. This reduces errors and increases security.

13 Variable Scope Local variables are only valid within the block in which they are declared. Blocks in Java open with { and close with } and can have other blocks embedded in them. Local variables are not automatically initialized. Variable declarations must be terminated with a ;

14 Flow Control return ends a method and returns control to the caller if and else are used for selection while, do-while and for are used for loops break and continue expand flow control

15 Class Declarations Class declarations define classes, which can be instantiated as objects and are assigned to a reference type. A class declaration includes a class name and fields, methods, and nested class declarations. The fields are known as the variables or attributes of a class.

16 Class and Method Modifiers –Field is accessible by all others within a package public –Field is accessible by any class private –Field is accessible only by itself static –Field is shared by all instances of a class abstract –Class contains only abstract methods, method defers implementation to subclasses final –Field may not be extended with subclasses A field may be a class or method.

17 Specialized Method Modifiers synchronized – an atomic thread in a multithread environment native –implemented in C and C++

18 main Method Every program must have a main method, declared as public static void main(String [] args) { … } The array of String arguments allows initialization values to be passed to the class at start up time. Only one class in the program requires a main method.

19 Print Command args (NetBeans)

20 Things to understand Here are some of the features of Java that students have had trouble with in the past, with page numbers from the text: String concatenation p. 84 Assignment p. 87 Casts p. 89 Initialization p. 91

21 More things to understand Multidimensional arrays p. 93 Expressions for object creation p. 94 Local variables and scope p. 95 The return statement p. 95 Break and continue p. 99 Class and method modifiers pp. 101-102 Accessibility p. 103

22 More things to understand Constructors p. 104 Accessors p. 106 Parameter Passing pp. 107-109 Class fields pp. 110-111 Singletons p. 114 This pp.114-115 Interfaces pp. 117-118

23 More things to understand String methods p. 119 Standard input, output and error pp. 123ff Wrappers pp. 128-129 Packages pp. 134-136 Exceptions pp. 139-148 The chapter summary contains many references to key concepts.

24 Code Examples from text Note: All the code that follows contains the following header which has been removed to fit on the screen: /* * Copyright (c) 1999-2002, Xiaoping Jia. * All Rights Reserved. */ Respect the author’s copyright!

25 Break Records

26 Bubble Sort

27 Copy

28 Copy Text File

29 Digital Clock page 1

30 Digital Clock page 2

31 MaxMin

32 Maximum (with try-catch)

33 String Compare An Interned String is a copy of a String that has the same content but is a different String.

34 Word: String Tokenizer

35 String Handling Understand the difference between the immutable String class and the mutable StringBuffer class. Strings have special privileges not shared by ordinary classes including the creation of string literals and the use of concatenation operators.

36 Garbage collection Garbage collection, especially remote garbage collection, is a big deal in Java. See the explanation on page 90. One reason that garbage collection is so important is that C does not have it, and a substantial number of problems in Microsoft Windows come from the failure to free resources no longer in use.

37 Bibliography Jia, Xiaoping, Object Oriented Software Development Using Java. Addison Wesley, 2003

Download ppt "George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4."

Similar presentations

Ads by Google