1 / 35

Programming Language Concepts (CIS 635)

Programming Language Concepts (CIS 635). Elsa L Gunter 4303 GITC NJIT, www.cs.njit.edu/~elsa/635. Static Binding in Java. Modifier final used to make a method (or variable) be statically bound, instead of dynamically bound When used on variable (or member), variable becomes a constant

Download Presentation

Programming Language Concepts (CIS 635)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT, www.cs.njit.edu/~elsa/635

  2. Static Binding in Java • Modifier final used to make a method (or variable) be statically bound, instead of dynamically bound • When used on variable (or member), variable becomes a constant • Final methods cannot be overridden in any subclass • Final class has no subclasses

  3. Dynamic Binding in C++ versus Java • Dynamic binding default in Java • Static binding default in C++ • In C++ use virtual keyword on method to cause it to be dynamically bound

  4. Abstract Classes in C++ • A purely virtual method is used for methods that are not to be implemented in base class • Indicated by giving a body of 0 • Purely virtual methods must be overridden in subclasses • Class is abstract if it contains purely virtual functions

  5. Abstract Classes in Java • Abstract class has modifier abstract • May contain abstract methods, prefixed by modifier abstract, and having no body • Abstract classes have no instance objects • Subclass must either override all abstract methods, or be abstract itself

  6. Abstract Classes in Java – Example • Before we had : public class CustomerAccount { … protected int fee () {return 0;} protected int interest () {return 0;}… } • Want to force fee and interest to be overridden

  7. Abstract Classes in Java – Example • Use abstract methods (and class) abstract public class CustomerAccount { … abstract protected int fee (); abstract protected int interest (); … }

  8. Abstract Classes in Java – Example • Can use ChargeAccount just as before • Credit account must supply a fee method

  9. Interfaces in Java • Abstract class specification • Defined with keyword interface instead of class • Purpose to supply export information • All methods are public and abstract (public and abstract keywords not used)

  10. Interfaces in Java • Class may implement several interfaces, but my extend only one class • Parameter to method may have type of an interface • Object from any class that implements an interface may be used • Class that implements an interface is a subtype of interface

  11. Packages in Java • Package is a collection of classes • Intent: classes in package work together • If a member of a class is not marked public, private, or protected, then it is visible to all other classes in package, but not outside

  12. Packages in Java • Class is added to package by being preceded by directive package <module name>; at start of source file • Access classes by <module name>.<class name> • All classes member of some package (noname package, if none specified)

  13. Java Class IOTest • Modified from “Learn Java Now” ny Stephen R. Davis, Microsoft Press import java.io.*; public class IOTest { public static void main(String args[]) { try { //Read input from the keyboard byte bArray[] = new byte[128]; System.out.println("Enter something:");

  14. Java Class IOTest // Reads in an array of bytes System.in.read(bArray); // Convert array into a String before // outputting it String s = new String(bArray, 0); System.out.println("You entered: " + s); }

  15. Java Class IOTest catch(IOException ioe) { System.out.println(ioe.toString()); ioe.printStackTrace(); } }}

  16. Example • What is the result for: 3 + 4 * 5 + 6 • Possible answers: • 41 = ((3 + 4) * 5) + 6 • 47 = 3 + (4 * (5 + 6)) • 29 = (3 + (4 * 5)) + 6 = 3 + ((4 * 5) + 6) • 77 = (3 + 4) * (5 + 6)

  17. Operator Precedence • Operators of highest precedence evaluated first (bind more tightly). • Precedence for infix binary operators given in following table

  18. Example • If assignment returns the assigned value, what is the result of x = 5; y = (x = 3) + x;

  19. Example • If assignment returns the assigned value, what is the result of x = 5; y = (x = 3) + x; • Possible answers: 6 or 8 • Depends on language, and sometimes compiler • C allows compiler to decide • SML forces left-to-right evaluation

  20. Control Structures • Expression Evaluation • Determined by operator evaluation order and operand evaluation order • Operators: • Most operators are either infix or prefix (some languages have postfix) • Order of evaluation determined by operator precedence and associativity

  21. Example Again • In most language, 3 + 4 * 5 + 6 = 29 • In APL, all infix operators have same precedence • Thus we still don’t know what the value is

  22. Example • What is the value of: 7 – 5 – 2 • Possible answers: • In Pascal, C++, SML assoc. left 7 – 5 – 2 = (7 – 5) – 2 = 0 • In APL, associate to right 7 – 5 – 2 = 7 – (5 – 2) = 4

  23. Special Associativity • In languages with built in support for infix exponent operator, it is standard for it to associate to the right: 2 ** 3 ** 4 = 2 ** (3 ** 4) • In ADA, exponentiation in non-associative; must use parentheses

  24. Operand Evaluation Order • Example: A := 5; f(x) = {A := x+x; return x}; B := A + f(A); • What is the value of B? • 10 or 15?

  25. Solution to Operand Evaluation Order • Disallow all side-effect • “Purely” functional languages try to do this – Miranda, Haskell • Problem: I/O, error conditions such as overflow are inherently side-effecting

  26. Solution to Operand Evaluation Order • Disallow all side-effects in expressions but allow in statements • Problem: not applicable in languages with nesting of expressions and statements

  27. Solution to Operand Evaluation Order • Fix order of evaluation • SML does this – left to right • Problem: makes some compiler optimizations hard to impossible • Leave it to the programmer to be sure the order doesn’t matter • Problem: error prone

  28. Short-circuit Evaluation • Boolean expressions: • Example: x <> 0 andalso y/x > 1 • Problem: if andalso is ordinary operator and both arguments must be evaluated, then y/x will raise an error when x = 0

  29. Boolean Expressions • Most languages allow (some version of) if…then…else, andalso, orelse not to evaluate all the arguments • if true then A else B • doesn’t evaluate B

  30. Boolean Expressions • if false then A else B • doesn’t evaluate A • if b_exp then A else B • Evaluates b_exp, then applies previous rules

  31. Boolen Expressions • Bexp1 andalso Bexp2 • If Bexp1 evaluates to false, doesn’t evaluate Bexp2 • Bexp1 orelse Bexp2 • If Bexp1 evaluates to true, doesn’t evaluate Bexp2

  32. Short-circuit Evaluation – Other Expressions • Example: 0 * A = 0 • Do we need to evaluate A? • In general, in f(x,y,…,z) are the arguments to f evaluated before f is called and the values are passed, or are the unevaluated expressions passed as arguments to f allowing f to decide which arguments to evaluate and in which order?

  33. Eager Evaluation • If language requires all arguments to be evaluated before function is called, language does eager evaluation and the arguments are passed using pass by value (also called call by value) or pass by reference

  34. Lazy Evaluation • If language allows function to determine which arguments to evaluate and in which order, language does lazy evaluation and the arguments are passed using pass by name (also called call by name)

  35. Lazy Evaluation • Lazy evaluation mainly done in purely functional languages • Some languages support a mix • Effect of lazy evaluation can be implemented in functional language with eager evaluation

More Related