1 / 42

Java vs C#

Java vs C#. Johan Lindfors Microsoft AB. What are we to cover?. Java vs C# Similarities and differencies Specific issues Deployment of applications GUI applications, event handling Distributed applications, remoting Java on .NET Visual J# .NET Java Language Conversion Assistant.

liko
Télécharger la présentation

Java vs C#

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. Java vs C# Johan LindforsMicrosoft AB

  2. What are we to cover? • Java vs C# • Similarities and differencies • Specific issues • Deployment of applications • GUI applications, event handling • Distributed applications, remoting • Java on .NET • Visual J# .NET • Java Language Conversion Assistant

  3. What are we to cover? • Java vs C# • Similarities and differencies • Specific issues • Deployment of applications • GUI applications, event handling • Distributed applications, remoting • Java on .NET • Visual J# .NET • Java Language Conversion Assistant

  4. Javabytecode .classfile .jarfile Compiles to Lives in Grouped into CIL .modulefile .dllfile Code architecture Javasourcecode C#sourcecode

  5. Similarities • Hello world in 4 languages… import java.lang.System; public class HelloWorld { public static void main(String[] args) { System.out.println("Zdravo, zemjata!"); } } Java using System; public class HelloWorld { public static void Main(string[] args) { Console.WriteLine("Salut, monde!"); } } C#

  6. Keyword similarities • Complex type keywords • class / interface • new • Conditional and flow keywords • if / then / else • switch / case / default • for / while / do while • break / continue • return • Exception handling keywords • try / catch / finally / throw

  7. Keyword differences • Package and namespace keywords • import in Java = using in C# • package = namespace • Complex keywords • extends = : (colon) • implements = : • instanceof = is • super = base • final = sealed / readonly • native = extern • synchronized = lock

  8. Example of issues • Enums • No value types in Java • In C# we support both value and reference types • The compiler handles the strain • Iterations • Iterations are very similar in Java and in C#, but… • ”foreach” is a very powerful ally • Resources • Resource management is also similar, but… • ”using” is your true friend

  9. Other topics • Unmanaged code through ”unsafe” • Attributes • Use keywords as identifiers • @ prefix can be used to differentiate keywords from identifiers using System; public class @class { public string @public(string @string) { string @return = @string + @string; return @return; } public static void Main() { Console.WriteLine(new @class().@public(“Simple test!”)); } }

  10. What are we to cover? • Java vs C# • Similarities and differencies • Specific issues • Deployment of applications • GUI applications, event handling • Distributed applications, remoting • Java on .NET • Visual J# .NET • Java Language Conversion Assistant

  11. .DLL 0101 .DLL 0110 Application Deployment on Windows Imports System.Windows.Forms Public Class Door ... Sub lock() MessageBox.Show(...) End Sub ... Sub unlock() MessageBox.Show(...) End Sub End Class Imports System.Windows.Forms Public Class Door ... Sub lock() MessageBox.Show(...); End Sub ... Sub unlock() MessageBox.Show(...); End Sub End Class App Imports System.Windows.Forms Public Class Door ... Sub lock() MessageBox.Show(...) End Sub ... Sub unlock() MessageBox.Show(...) End Sub End Class Imports System.Windows.Forms Public Class Door ... Sub lock() MessageBox.Show(...); End Sub ... Sub unlock() MessageBox.Show(...); End Sub End Class

  12. Imports System.Windows.Forms Public Class Door ... Sub lock() MessageBox.Show(...) End Sub ... Sub unlock() MessageBox.Show(...) End Sub End Class Imports System.Windows.Forms Public Class Door ... Sub lock() MessageBox.Show(...); End Sub ... Sub unlock() MessageBox.Show(...); End Sub End Class .DLL 0101 .DLL 0110 Application backout on Windows App Imports System.Windows.Forms Public Class Door ... Sub lock() MessageBox.Show(...) End Sub ... Sub unlock() MessageBox.Show(...) End Sub End Class Imports System.Windows.Forms Public Class Door ... Sub lock() MessageBox.Show(...); End Sub ... Sub unlock() MessageBox.Show(...); End Sub End Class

  13. Imports System.Windows.Forms Public Class Door ... Sub lock() MessageBox.Show(...); End Sub ... Sub unlock() MessageBox.Show(...); End Sub End Class Imports System.Windows.Forms Public Class Door ... Sub lock() MessageBox.Show(...); End Sub ... Sub unlock() MessageBox.Show(...); End Sub End Class .jar JBC Imports System.Windows.Forms Public Class Door ... Sub lock() MessageBox.Show(...); End Sub ... Sub unlock() MessageBox.Show(...); End Sub End Class Imports System.Windows.Forms Public Class Door ... Sub lock() MessageBox.Show(...); End Sub ... Sub unlock() MessageBox.Show(...); End Sub End Class .jar JBC Application Deployment with Java App

  14. Imports System.Windows.Forms Public Class Door ... Sub lock() MessageBox.Show(...); End Sub ... Sub unlock() MessageBox.Show(...); End Sub End Class Imports System.Windows.Forms Public Class Door ... Sub lock() MessageBox.Show(...); End Sub ... Sub unlock() MessageBox.Show(...); End Sub End Class .jar JBC Imports System.Windows.Forms Public Class Door ... Sub lock() MessageBox.Show(...); End Sub ... Sub unlock() MessageBox.Show(...); End Sub End Class Imports System.Windows.Forms Public Class Door ... Sub lock() MessageBox.Show(...); End Sub ... Sub unlock() MessageBox.Show(...); End Sub End Class .jar JBC Application backout with Java App

  15. Imports System.Windows.Forms Public Class Door ... Sub lock() MessageBox.Show(...); End Sub ... Sub unlock() MessageBox.Show(...); End Sub End Class .module Imports System.Windows.Forms Public Class Door ... Sub lock() MessageBox.Show(...); End Sub ... Sub unlock() MessageBox.Show(...); End Sub End Class CIL 1.0.0.1 Imports System.Windows.Forms Public Class Door ... Sub lock() MessageBox.Show(...); End Sub ... Sub unlock() MessageBox.Show(...); End Sub End Class .module Imports System.Windows.Forms Public Class Door ... Sub lock() MessageBox.Show(...); End Sub ... Sub unlock() MessageBox.Show(...); End Sub End Class CIL .NET Upgrade 1.0.0.0 <configuration> <runtime> ... </runtime> </configuration> App

  16. .NET Upgrade backout 1.0.0.0 <configuration> <runtime> ... </runtime> </configuration> Imports System.Windows.Forms Public Class Door ... Sub lock() MessageBox.Show(...); End Sub ... Sub unlock() MessageBox.Show(...); End Sub End Class .module Imports System.Windows.Forms Public Class Door ... Sub lock() MessageBox.Show(...); End Sub ... Sub unlock() MessageBox.Show(...); End Sub End Class CIL App 1.0.0.1 Imports System.Windows.Forms Public Class Door ... Sub lock() MessageBox.Show(...); End Sub ... Sub unlock() MessageBox.Show(...); End Sub End Class .module Imports System.Windows.Forms Public Class Door ... Sub lock() MessageBox.Show(...); End Sub ... Sub unlock() MessageBox.Show(...); End Sub End Class CIL

  17. Global Assembly Cache <configuration> <runtime> ... </runtime> </configuration> 1.0.0.0 1.0.0.1 App 1.1.0.0 1.1.0.1 <configuration> <runtime> ... </runtime> </configuration> 2.0.0.0 App 2.1.0.0 GAC

  18. Java Events • Java events are represented by method names grouped into listener interfaces public interface java.awt.event.MouseListener { public void mouseClicked (MouseEvent e); public void mousePressed (MouseEvent e); public void mouseReleased (MouseEvent e); public void mouseEntered (MouseEvent e); public void mouseExited (MouseEvent e); }

  19. Java Event Sources • Java event sources call event methods on registered listeners class JButton { ... public void addMouseListener(MouseListener added); public void removeMouseListener(MouseListener removed); ... }

  20. Java Adapters • Adapter classes allow you to ignore events you're not interested in class MouseAdapter implements MouseListener { public void mouseClicked (MouseEvent e) {} public void mousePressed (MouseEvent e) {} public void mouseReleased (MouseEvent e) {} public void mouseEntered (MouseEvent e) {} public void mouseExited (MouseEvent e) {} }

  21. Java Event Handlers • Declare a handler class that extends the adapter and override the event methods of interest class Example { ... class OnMousePressedHandler extends MouseAdapter { public void mousePressed(MouseEvent e) { ... } } ... }

  22. Java Event Example • Create an instance of the handler class and add it to the event source class Example { ... public void init() { ... ok.addMouseListener(new OnMousePressedHandler()); } private class OnMousePressedHandler ... { ... } private JButton ok; }

  23. .NET Events • Event source names their events directly • Every event has a type class Button { ... public event EventHandler Click; public event MouseEventHandler MouseDown; public event MouseEventHandler MouseUp; public event EventHandler Enter; public event EventHandler Leave; ... }

  24. .NET Event Types • An event type is a method signature • The delegate keyword declares a type delegate void EventHandler( object sender, EventArgs sent ); delegate void MouseEventHandler( object sender, MouseEventArgs sent );

  25. .NET Event Handlers • Declare an event handling method matching the event delegate signature delegate void MouseEventHandler( object sender, MouseEventArgs sent ); class Example { void OnMouseDown( object sender, MouseEventArgs sent) { ... } }

  26. .NET Event Example • Create an event delegate instance using the name of the handler method and add it to the event source class Example { public Example() { ... ok.MouseDown += new MouseEventHandler(OnMouseDown); } private void OnMouseDown(object sender, MouseEventArgs sent){ ... } private Button ok; }

  27. Distributed Applications RMI/IIOP • Make calls between JVMs • Remote object implements a defined remote interface type used by the client • Pass primitive types and objects • Pass-by-value using Java serialization • Pass-by-reference for remote object references • Different transports available • Proprietary, TCP-based protocol (JRMP) • CORBA-compliant IIOP under J2EE • Firewall tunnelling options

  28. RMI Lifecycle • Remote objects registered in registry or JNDI • Client looks up remote object and uses it • Download stub class if required • Two lifecycle options • Server instantiates remote objects and registers them (server is no longer required) • Objects are activated when methods are invoked • RMI also used to communicate with EJBs

  29. .NET Remoting • Make calls between application domains • Remoting server links clients to objects • Creates a channel for a given port/protocol • Use TCP or HTTP transports • Pass objects by reference or by value • Remote objects must extend MarshalByRefObject • Objects by value must be Serializable • Need to handle network-related exceptions

  30. File-based configuration • Use application config file to change settings <channels> <channel displayName=“MyChannel" ref="tcp" /> </channels> <client url="tcp://elephant:6006"> <activated type=“MyNamespace.MyClass, MyAssembly"/> </client>

  31. Request Create Use Dispose Client activation Remoting Client Remoting Server Remote Object

  32. Request Create Use Dispose Server activation – Single Call Remoting Client Remoting Server Remote Object

  33. Request Request Create Use Use Dispose Server activation – Singleton Remoting Client Remoting Server Remote Object Remoting Client

  34. Advanced .NET Remoting • Client-activated object lifetime • Specified lifetime based on a lease • Server-activated object lifetime • Single call lasts for method duration • Singleton has a specified lifetime based on a lease • Asynchronous calls • Create delegates in the usual way • Pass them to use as callbacks • Wrap remote delegate in AsyncDelegate • Register to be notified of method completion

  35. What are we to cover? • Java vs C# • Similarities and differencies • Specific issues • Deployment of applications • GUI applications, event handling • Distributed applications, remoting • Java on .NET • Visual J# .NET • Java Language Conversion Assistant

  36. Visual J# .NET • The Java language in Visual Studio.NET • Easy transition for Java developer into .NET • Primarily developed to migrate Visual J++ application to .NET • Enables full use of… • .NET Framework • XML Web Services in ASP.NET • ADO.NET • Windows Forms • Full cross language integration

  37. What is the JLCA? • Java Language Conversion Assistant • Visual Studio .NET addon • Java language and classes converted to C# and .NET classes • Migrated application will only run on .NET Framework • Numbers right now… • 99% of language converted • 87% of classes • JBIMP.EXE converts Java bytecode into MSIL

  38. What have we covered? • Java vs C# • Similarities and differencies • Specific issues • Deployment of applications • GUI applications, event handling • Distributed applications, remoting • Java on .NET • Visual J# .NET • Java Language Conversion Assistant

  39. What have we missed? • A bunch of things… • EJB and Serviced Components • Threading and synchronization • Networking • XML processing • Security and Cryptography • Reflection • Database connectivity • Graphics • …

  40. More information • Microsoft and other • http://msdn.microsoft.com • http://www.gotdotnet.com • http://www.theserverside.com • MS Press • C# for Java developers • ISBN: 0-7356-1779-1 • .NET Remoting • ISBN: 0-7356-1778-3

  41. Definitions • GUI Graphical User Interface • CIL Common Intermediate Language • DLL Dynamic Link Library • GAC Global Assembly Cache • JLCA Java Language Conversion Assistant • JAR Java ARchive • JBC Java Byte Code • JRMP Java Remote Method Protocol • JNDI Java Naming and Directory Interface • ISBN International Standard Book Number

More Related