NJIT CS 602 - Object Oriented Software Development Using Java

Unformatted text preview:

Slide 1CS 602 Java and the WebElements of JavaJava UniquenessStandardizationVariables and TypesVariable (a memory location)ConstructorsPoint example (text, p. 104)Singleton ClassesSingleton ClassReference TypesVariable ScopeFlow ControlClass DeclarationsClass and Method ModifiersSpecialized Method Modifiersmain MethodPrint Command args (NetBeans)Things to understandMore things to understandSlide 22Slide 23Code Examples from textBreak RecordsBubble SortCopyCopy Text FileDigital Clock page 1Digital Clock page 2MaxMinMaximum (with try-catch)String CompareWord: String TokenizerString HandlingGarbage collectionBibliographyGeorge BlankUniversity LecturerCS 602Java and the WebObject Oriented Software Development Using JavaChapter 4Elements 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.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.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.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.Variable (a memory location)addressa value(a default value)a namea typeThe address or value may be passed as a parameterConstructors•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).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.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.Singleton ClassNote the private static constructor that prevents multiple instances.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.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 ;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 controlClass 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.Class and Method Modifiers•<none> –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 subclassesA field may be a class or method.Specialized Method Modifiers•synchronized – an atomic thread in a multithread environment•native –implemented in C and C++main Method•Every program must have a main method, declared aspublic 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.Print Command args (NetBeans)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. 91More 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. 103More 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-118More 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.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!BreakRecordsBubbleSortCopyCopy Text


View Full Document

NJIT CS 602 - Object Oriented Software Development Using Java

Download Object Oriented Software Development Using Java
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Object Oriented Software Development Using Java and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Object Oriented Software Development Using Java 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?