Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT, www.cs.njit.edu/~elsa/635 Object-Oriented Programming • • • • Class is a form of abstract type Instances of class called objects Operators of class called methods Applying a method to an object in the class called message passing – Messages take object as implicit argument Copyright 2002 Elsa L. Gunter Variables and Methods • Classes have two kinds of methods and two kinds of variables: –Instance methods and instance variables –Class methods and class variable Copyright 2002 Elsa L. Gunter Instance Variables and Methods • Instance variables – Instance variables hold state of single object – Different objects have different (copies of) instance variables • Instance methods: – Instance methods act on a single object, usually acting on instance variables Copyright 2002 Elsa L. Gunter Class Variables and Methods • Class variables hold state that is shared in common among all objects of class • Class methods act on attributes of the whole class through class variables • Class methods can’t access instance variables Copyright 2002 Elsa L. Gunter Inheritance • Inheritance allows all variables and methods exported from parent class to be part of a derived class or subclass Copyright 2002 Elsa L. Gunter Inheritance • Export information: –public: everybody sees –private: only seen inside class –protected: exported only to subclasses Copyright 2002 Elsa L. Gunter Inheritance • If a method is added to a subclass with the same name (and usually signature) as in the parent class the new version overrides inherited version in subclass Copyright 2002 Elsa L. Gunter Inheritance Polymorphism and Dynamic Binding • Method of superclass may be overridden in subclass • Dynamic binding: when a method of superclass is applied to object of subclass, methods of subclass are used in computing result, instead of methods of superclass • Java has dynamic binding by default • C++ method must be marked as virtual for dynamic binding to be used with it Copyright 2002 Elsa L. Gunter Virtual (or Abstract) Methods • Virtual method: has declaration but no function body • Subclass must override all virtual methods with methods with fully concrete bodies to be instantiated Copyright 2002 Elsa L. Gunter Single Versus Multiple Inheritance • Single inheritance: Class may only be a subclass of one parent • Multiple inheritance: Class may be immediate subclass of two or more parents • Problem with multiple inheritance: class A:B,C {…} What if we have both B.m and C.m? Which method is A.m? Answer in most languages: B.m Copyright 2002 Elsa L. Gunter Introduction to Java • Recently introduced (1995) • Modern and sound design (Gosling) • Very rapid growth (and heavy hype) • Killer application: web-based computing (applets) Copyright 2002 Elsa L. Gunter Introduction to Java • Originally designed for smart appliances such as interactive TVs and “set-top boxes” –Priorities: small, secures, portable –Makes it good for running on the web Copyright 2002 Elsa L. Gunter Platform Independence • Machine-independent semantics and security model • Key concept: Java Virtual Machine – Each platform implements Java Virtual Machine – Java compiles to Java byte code that runs on (is interpreted by) Java Virtual Machine Copyright 2002 Elsa L. Gunter Getting Java • For MS Windows: – http://java.sun.com/j2se/1.3/ or – http://www.cis.njit.edu/~elsa/635/j2sd k-1_3_0_02-win.exe • Installation instructions found – http://java.sun.com/j2se/1.3/installwindows.html Copyright 2002 Elsa L. Gunter Getting Started With Java • Will assume you have adjusted your PATH variable as described in instructions • Start up editor (eg Notepad) • Create file called “Hello.java” –Use quotes so that editor doesn’t add its own extension (like .txt) Copyright 2002 Elsa L. Gunter Getting Started With Java • Put code on next slide in file and save • In MS_DOS window in same directory, run javac Hello.java to compile code • In MS_DOS window in same directory, run java Hello Copyright 2002 Elsa L. Gunter Sample Java Code (“Hello.java”) class Hello { public static void main (String args[]) { System.out.println ("So at last we get to Java!\n"); } } Copyright 2002 Elsa L. Gunter Sample Java Code (“Hello.java”) To execute: C:\My Documents\635\java>javac Hello.java C:\My Documents\635\java>java Hello So at last we get to Java! Copyright 2002 Elsa L. Gunter Terminolgoy • Class: Hello • Class method: main – Only class method of class Hello Copyright 2002 Elsa L. Gunter Terminolgoy • • • • • Object: System Method of System: out Object: System.out Method of System.out: println Object: “So at last we get to Java!“ of class String of immutable strings Copyright 2002 Elsa L. Gunter Java Declarations • Declarations state type and other attributes of identifier private int i, j; • Declaration has three parts: –Modifiers: private –Type: int –List of identifiers: i, j Copyright 2002 Elsa L. Gunter Java Method Declaration public static void main (String args []) • public and static describe scope and sharing characteristics of method main Copyright 2002 Elsa L. Gunter Java Method Declaration • Input of main is an array of strings (objects of class String) • Return type of main is void, i.e. no return value • Formal input parameter is args (not actually ever used here) Copyright 2002 Elsa L. Gunter Initialization • If an identifier is declared without being initialized, Java used default initialization • To initialize identifier, follow its declaration by = and an expression: int lo = 1; int hi = 1; Copyright 2002 Elsa L. Gunter Java Built-in Data Types Data type Size(bits) Default initial value boolean 8 false byte 8 0 char 16 ‘x0’ short 16 0 Copyright 2002 Elsa L. Gunter Java Built-in Data Types Data type int long float double Copyright 2002 Elsa L. Gunter Size(bits) Default initial value 32 0 64 0 32 0.0F 64 0.0D boolean and char Literals • true and false are not (andcannot be cast into) integers • Character literals are enclosed in single quotes: ‘a’ • Uses same convention for special characters as C: \n for newline, \\ for backslash, \’ for single quote Copyright 2002 Elsa L. Gunter Assignment and Equality • Assignment operator: = • Assigns value to identifier • Value and type of assignment operation is resulting value and type of left-hand identifier int nA, nB; nB = (nA = 4) / 2 • Do not confuse with binary boolean equality operator == Copyright 2002 Elsa L. Gunter Boolean Operators • Comparison operators >, <, >=, <=, ==, != return boolean values, not integers • Logical boolean operations: – And: E1 & E2 ( && for short circuit) – Or: E1 | E2 ( || for short circuit) – Exclusive Or: E1 ^ E2 Copyright 2002 Elsa L. Gunter