Java - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Java

Description:

Calls each delegate when an event occurs. Java has to use a full object ... static void Main() { DelegateDemo t = new DelegateDemo ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 23
Provided by: julie57
Category:
Tags: event | java | main | racing

less

Transcript and Presenter's Notes

Title: Java


1
Java C
  • Summarise key similarities differences
  • Memory management type safety
  • Interface
  • Delegates, struct, properties, reference
    parameter
  • Evaluate the similarities differences
  • Discuss different languages approach to OO

2
Overview
  • C and Java similarities
  • Garbage collection
  • Run-time checking
  • Types (casts), Array Bounds checking
  • Single inheritance
  • C and Java differences
  • Delegates
  • Exceptions
  • Value Reference variables
  • Properties

3
C Properties
  • C classes have methods properties
  • Good programming hides properties (why)
  • Only access properties through methods
  • In C can access properties by assignment
  • But actually uses Getter Setter functions
  • Just syntactic sugar simpler to read
  • Better linguistic match to property concept
  • Assignment indicates that something is being
    changed

4
Properties Example
  • public class MyClass
  • private long m_long
  • //public property
  • public long Number
  • get
  • return (m_long)
  • set
  • m_long value
  • public MyClass()
  • m_long 0
  • MyClass m new MyClass()
  • m.Number 3
  • long m m.Number

The real data member is private
In Java, the getter setter would be explicit
methods Method call syntax is used.
5
Event Handling
  • Event-generating objects keep a list of handlers
  • When an event occurs,
  • run through the list calling a suitable method
  • Java Handlers are objects
  • C Handlers are pointers to methods
  • Delegates

6
Delegates
  • Like a function pointer in C
  • But can also be an instance method pointer
  • Keeps track of the owning object
  • Used for callbacks pass function to server
  • E.g. events use a delegate as an event handler
  • Component can keep a list of delegates
  • Calls each delegate when an event occurs
  • Java has to use a full object
  • But can create anonymous objects
  • Inline definition of method implementations for
    interface

7
Java Event Handlers
  • public class MyButtonListener extends Applet
    implements ActionListener
  • private Button m_acceptButton
  • private Button m_rejectButton
  • public MyButtonListener()
  • m_acceptButton new Button("OK")
  • m_acceptButton.AddActionListener(this)
  • m_rejectButton new Button("Cancel")
  • m_rejectButton.AddActionListener(this)
  • // code to add these Buttons to me and for
    layout
  • //ActionListener events
  • public void actionPerformed(ActionEvent e)
  • if (e.getSource() m_rejectButton)
  • //write rejection code
  • else if (e.getSource()
    m_acceptButton)
  • //write acceptance code

Add this object to the buttons list of handlers
The interface for the handler
The method that anything implementing
ActionListener must provide
8
C Delegate Event Handlers
  • public class MyButtonListener Form
  • public MyButtonListener()
  • Button accept new Button()
  • accept.Text "OK"
  • accept.Click new EventHandler(AcceptClick
    )
  • Button reject new Button()
  • reject.Text "Cancel"
  • reject.Click new EventHandler(RejectClick
    )
  • //Event handlers for buttons
  • private void AcceptClick(object sender,
    EventArgs e)
  • private void RejectClick(object sender,
    EventArgs e)

Added to the list of function pointers
function pointer No ()
Overloaded operator
9
Creating a Delegate
  • Defining a Delegate type
  • public delegate void EventHandler(object sndr,
    EventArgs e)
  • Creating Using a Delegate object
  • public class Button
  • public String Text
  • public EventHandler Click new EventHandler()
  • public ClickTheButton(EventArgs e)
  • Click(this, e)

Essentially a class with a list of method pointers
All the method pointers will be called with these
parameters
10
Evaluation of Delegates
  • More type-safe than function pointers
  • No need to create objects to pass functions
    around
  • class DelegateDemo
  • delegate int DelegateProcessInt(int i)
  • public int Square(int i) return ii
  • public int Cube(int i) return iii
  • void ShowResult(DelegateProcessInt d, String
    s,int i)
  • Console.WriteLine("0 of 1 is 2", s, i,
    d(i))
  • static void Main()
  • DelegateDemo t new DelegateDemo()
  • t.ShowResult(new DelegateProcessInt (t.Square),
    "Square", 7)
  • t.ShowResult(new DelegateProcessInt (t.Cube),
    "Cube", 7)

Equivalent to printf
11
Inheritance
  • C Java support only single inheritance
  • Can prevent class being a parent sealed/final
  • C doesnt automatically override methods
  • Methods only virtual if virtual keyword used
  • Must explicitly use override keyword
  • Prevents a child method being used if a new
    method with same name added to ancestor
  • Can hide ancestor method use new keyword
  • Greater flexibility
  • But is it really valuable?

12
Interfaces
  • Like a class with just method signatures
  • Used in design-by-contract
  • A factory object can return any object that
    implements the interface
  • i.e. implements all the methods
  • Avoids ambiguity in multiple inheritance
  • Java allows constants in interface
  • C allows different implementation of methods
    with same name from different interfaces

13
Struct
  • C provides Struct
  • A lightweight class stored on the stack
  • Cant inherit from a Struct
  • Normal classes are stored on the heap
  • Struct variables are values
  • Class variables are references
  • MyStruct theStruct new MyStruct(1,2)
  • MyClass theClass new MyClass(1,2)

1
2
1
2
theStruct
theClass
14
Destructors
  • C Java have finalizers
  • Called by garbage collector when freeing object
  • Called at random time
  • May not be called if program aborted
  • Not good for general resource management
  • C defines the IDispose interface
  • Class implements IDispose
  • That is, provides a dispose() method
  • Explicitly call dispose() when finished with
    object
  • Must ensure calling dispose twice isnt a problem

15
Exceptions Similarities
  • Handles system and application errors
  • Structured, uniform, and type-safe approach
  • All exceptions derived from one base clas
  • In C, an exception can be a value of any type
  • A finally block can provide termination code
  • Executes in normal and exceptional conditions
  • Well defined exceptions for system errors
  • overflow, divide-by-zero, null dereferences

16
Exceptions Differences
  • Java is fussier about exceptions
  • RunTimeException inherits from Exception
  • A method that could throw an Exception that isnt
    a RunTimeException must declare this
  • void get(String url) throws MalFormedURLException
  • Allows compiler to check use
  • Allows IDE to prompt programmer

17
Access Protection
  • Both use private, protected, public
  • Differences
  • Java protected accessible to classes in package
  • C also has
  • internal accessible to classes in namespace
  • protected internal like Java protected
  • Java is inconsistent
  • public private based on classes
  • protected based on classes packages

18
Other Similarities
  • Built-in support for concurrency
  • Threads synchronisation
  • Differences in the approach
  • Support for Unicode (16-bit chars)
  • Support for modularity
  • C namespaces
  • Java packages

19
Arrays
  • C
  • Arrays are contiguous blocks of variables
  • C Java
  • Arrays are objects in their own right
  • Methods properties for handling collections
  • E.g. to determine the number of elements
  • Java only has 1-d arrays
  • But you can create arrays of arrays
  • C has 1-d and 2-d arrays

20
Other C Features
  • Can overload operators
  • Can pass parameters by reference
  • Java cant write void swap(int a, int b)
  • Automatically converts value types to object
  • int i 3
  • WriteLine(i.toString())
  • object o i
  • int j (int) o
  • value types can be used with object containers
  • Since the value is copied, can have odd effects

21
The Language Race
  • Both languages are still growing
  • C version 2 Java 1.5 added generics
  • Different approaches
  • Java approach meant no changes to JVM
  • C had more sophisticated synchronisation
  • Java 1.5 added more primitives in a library
  • C is integrating database technology (LINQ)
  • http//msdn2.microsoft.com/en-us/netframework/aa90
    4594.aspx
  • Methods of accessing information from non-OO
    source
  • XML
  • Databases

22
Summary
  • Considerable similarities
  • Subtle not so subtle differences
  • Java designed for portability
  • Java Virtual Machine
  • C designed for multi-language programming
  • Can easily be used with VB, C
  • Common Runtime System
Write a Comment
User Comments (0)
About PowerShow.com