Java vs C#
Johan LindforsMicrosoft 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
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
Code architecture
Javasourcecode
C#sourcecode
Javabytecode
CIL
.classfile
.modulefile
.jarfile
.dllfile
Compiles to Lives in Grouped into
Similarities
Hello world in 4 languages…
import java.lang.System;public class HelloWorld { public static void main(String[] args) { System.out.println("Zdravo, zemjata!"); }}
using System;public class HelloWorld { public static void Main(string[] args) { Console.WriteLine("Salut, monde!"); }}
Java
C#
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
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
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
Other topics
Unmanaged code through ”unsafe” Attributes Use keywords as identifiers
@ prefix can be used to differentiate keywords from identifiersusing 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!”)); }}
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
Imports System.Windows.Forms
Public Class Door ... Sub lock() MessageBox.Show(...); End Sub ... Sub unlock() MessageBox.Show(...); End SubEnd Class
AppImports System.Windows.Forms
Public Class Door ... Sub lock() MessageBox.Show(...); End Sub ... Sub unlock() MessageBox.Show(...); End SubEnd Class
.DLL
0101
.DLL
0110
Imports System.Windows.Forms
Public Class Door ... Sub lock() MessageBox.Show(...) End Sub ... Sub unlock() MessageBox.Show(...) End SubEnd Class
Imports System.Windows.Forms
Public Class Door ... Sub lock() MessageBox.Show(...) End Sub ... Sub unlock() MessageBox.Show(...) End SubEnd Class
Application Deployment on Windows
AppImports System.Windows.Forms
Public Class Door ... Sub lock() MessageBox.Show(...); End Sub ... Sub unlock() MessageBox.Show(...); End SubEnd Class
.DLL
0101
.DLL
0110
Imports System.Windows.Forms
Public Class Door ... Sub lock() MessageBox.Show(...); End Sub ... Sub unlock() MessageBox.Show(...); End SubEnd Class
Imports System.Windows.Forms
Public Class Door ... Sub lock() MessageBox.Show(...) End Sub ... Sub unlock() MessageBox.Show(...) End SubEnd Class
Imports System.Windows.Forms
Public Class Door ... Sub lock() MessageBox.Show(...) End Sub ... Sub unlock() MessageBox.Show(...) End SubEnd Class
Application backout on Windows
App
.jar
JBC
.jar
JBC
Imports System.Windows.Forms
Public Class Door ... Sub lock() MessageBox.Show(...); End Sub ... Sub unlock() MessageBox.Show(...); End SubEnd Class
Imports System.Windows.Forms
Public Class Door ... Sub lock() MessageBox.Show(...); End Sub ... Sub unlock() MessageBox.Show(...); End SubEnd Class
Imports System.Windows.Forms
Public Class Door ... Sub lock() MessageBox.Show(...); End Sub ... Sub unlock() MessageBox.Show(...); End SubEnd Class
Imports System.Windows.Forms
Public Class Door ... Sub lock() MessageBox.Show(...); End Sub ... Sub unlock() MessageBox.Show(...); End SubEnd Class
Application Deployment with Java
App
.jar
JBC
.jar
JBC
Imports System.Windows.Forms
Public Class Door ... Sub lock() MessageBox.Show(...); End Sub ... Sub unlock() MessageBox.Show(...); End SubEnd Class
Imports System.Windows.Forms
Public Class Door ... Sub lock() MessageBox.Show(...); End Sub ... Sub unlock() MessageBox.Show(...); End SubEnd Class
Imports System.Windows.Forms
Public Class Door ... Sub lock() MessageBox.Show(...); End Sub ... Sub unlock() MessageBox.Show(...); End SubEnd Class
Imports System.Windows.Forms
Public Class Door ... Sub lock() MessageBox.Show(...); End Sub ... Sub unlock() MessageBox.Show(...); End SubEnd Class
Application backout with Java
App
Imports System.Windows.Forms
Public Class Door ... Sub lock() MessageBox.Show(...); End Sub ... Sub unlock() MessageBox.Show(...); End SubEnd Class
Imports System.Windows.Forms
Public Class Door ... Sub lock() MessageBox.Show(...); End Sub ... Sub unlock() MessageBox.Show(...); End SubEnd Class
Imports System.Windows.Forms
Public Class Door ... Sub lock() MessageBox.Show(...); End Sub ... Sub unlock() MessageBox.Show(...); End SubEnd Class
Imports System.Windows.Forms
Public Class Door ... Sub lock() MessageBox.Show(...); End Sub ... Sub unlock() MessageBox.Show(...); End SubEnd Class
.module
CIL
1.0.0.0
.module
CIL
1.0.0.1
<configuration> <runtime> ... </runtime></configuration>
.NET Upgrade
Imports System.Windows.FormsImports System.Windows.Forms
Public Class DoorPublic Class Door ...... Sub lock()Sub lock() MessageBox.Show(...);MessageBox.Show(...); End SubEnd Sub ...... Sub unlock()Sub unlock() MessageBox.Show(...);MessageBox.Show(...); End SubEnd SubEnd ClassEnd Class
AppImports System.Windows.FormsImports System.Windows.Forms
Public Class DoorPublic Class Door ...... Sub lock()Sub lock() MessageBox.Show(...);MessageBox.Show(...); End SubEnd Sub ...... Sub unlock()Sub unlock() MessageBox.Show(...);MessageBox.Show(...); End SubEnd SubEnd ClassEnd Class
.module
CIL
Imports System.Windows.Forms
Public Class Door ... Sub lock() MessageBox.Show(...); End Sub ... Sub unlock() MessageBox.Show(...); End SubEnd Class
Imports System.Windows.Forms
Public Class Door ... Sub lock() MessageBox.Show(...); End Sub ... Sub unlock() MessageBox.Show(...); End SubEnd Class
.module
CIL
1.0.0.0
1.0.0.1
.NET Upgrade backout
<configuration> <runtime> ... </runtime></configuration>
App
1.0.0.0<configuration> <runtime> ... </runtime></configuration>
App
<configuration> <runtime> ... </runtime></configuration>
1.0.0.1
1.1.0.0
1.1.0.1
2.0.0.0
2.1.0.0GAC
Global Assembly Cache
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);}
Java events are represented by method names grouped into listener interfaces
Java Events
class JButton { ... public void addMouseListener(MouseListener added); public void removeMouseListener(MouseListener removed); ...}
Java Event Sources
Java event sources call event methods on registered listeners
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) {}}
Java Adapters
Adapter classes allow you to ignore events you're not interested in
class Example { ... class OnMousePressedHandler extends MouseAdapter { public void mousePressed(MouseEvent e) { ... } } ...}
Java Event Handlers
Declare a handler class that extends the adapter and override the event methods of interest
class Example { ... public void init() { ... ok.addMouseListener(new OnMousePressedHandler()); } private class OnMousePressedHandler ... { ... } private JButton ok;}
Create an instance of the handler class and add it to the event source
Java Event Example
class Button{ ... public event EventHandler Click; public event MouseEventHandler MouseDown; public event MouseEventHandler MouseUp; public event EventHandler Enter; public event EventHandler Leave; ...}
Event source names their events directly Every event has a type
.NET Events
delegate void EventHandler( object sender, EventArgs sent );
delegate void MouseEventHandler( object sender, MouseEventArgs sent );
.NET Event Types
An event type is a method signature The delegate keyword declares a type
class Example{ void OnMouseDown( object sender, MouseEventArgs sent) {
... }}
delegate void MouseEventHandler( object sender, MouseEventArgs sent );
Declare an event handling method matching the event delegate signature
.NET Event Handlers
class Example { public Example() {
... ok.MouseDown += new MouseEventHandler(OnMouseDown); } private void OnMouseDown(object sender, MouseEventArgs sent){ ... } private Button ok;}
.NET Event Example
Create an event delegate instance using the name of the handler method and add it to the event source
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
Distributed Applications RMI/IIOP
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
RMI Lifecycle
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
.NET Remoting
<channels> <channel displayName=“MyChannel" ref="tcp" /></channels><client url="tcp://elephant:6006"> <activated type=“MyNamespace.MyClass, MyAssembly"/></client>
Use application config file to change settings
File-based configuration
RemotingServer
Request
CreateUse
Dispose
RemoteObject
RemotingClient
Client activation
RemotingServer
Request
CreateUse
Dispose
RemoteObject
RemotingClient
Server activation – Single Call
RemotingServer
Request
CreateUse
Dispose
RemoteObject
RemotingClient
Server activation – Singleton
RemotingClient
Request
Use
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
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
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
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
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
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 …
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
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
Top Related