Top Banner
Java vs C# Johan Lindfors Microsoft AB
42

Java vs C# Johan Lindfors Microsoft AB. What are we to cover? Java vs C# Similarities and differencies Specific issues Deployment of applications.

Jan 18, 2018

Download

Documents

Jesse Lloyd

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
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Java vs C# Johan Lindfors Microsoft AB. What are we to cover?  Java vs C#  Similarities and differencies  Specific issues  Deployment of applications.

Java vs C#

Johan LindforsMicrosoft AB

Page 2: Java vs C# Johan Lindfors Microsoft AB. What are we to cover?  Java vs C#  Similarities and differencies  Specific issues  Deployment of applications.

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

Page 3: Java vs C# Johan Lindfors Microsoft AB. What are we to cover?  Java vs C#  Similarities and differencies  Specific issues  Deployment of applications.

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

Page 4: Java vs C# Johan Lindfors Microsoft AB. What are we to cover?  Java vs C#  Similarities and differencies  Specific issues  Deployment of applications.

Code architecture

Javasourcecode

C#sourcecode

Javabytecode

CIL

.classfile

.modulefile

.jarfile

.dllfile

Compiles to Lives in Grouped into

Page 5: Java vs C# Johan Lindfors Microsoft AB. What are we to cover?  Java vs C#  Similarities and differencies  Specific issues  Deployment of applications.

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#

Page 6: Java vs C# Johan Lindfors Microsoft AB. What are we to cover?  Java vs C#  Similarities and differencies  Specific issues  Deployment of applications.

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

Page 7: Java vs C# Johan Lindfors Microsoft AB. What are we to cover?  Java vs C#  Similarities and differencies  Specific issues  Deployment of applications.

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

Page 8: Java vs C# Johan Lindfors Microsoft AB. What are we to cover?  Java vs C#  Similarities and differencies  Specific issues  Deployment of applications.

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

Page 9: Java vs C# Johan Lindfors Microsoft AB. What are we to cover?  Java vs C#  Similarities and differencies  Specific issues  Deployment of applications.

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!”)); }}

Page 10: Java vs C# Johan Lindfors Microsoft AB. What are we to cover?  Java vs C#  Similarities and differencies  Specific issues  Deployment of applications.

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

Page 11: Java vs C# Johan Lindfors Microsoft AB. What are we to cover?  Java vs C#  Similarities and differencies  Specific issues  Deployment of applications.

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

Page 12: Java vs C# Johan Lindfors Microsoft AB. What are we to cover?  Java vs C#  Similarities and differencies  Specific issues  Deployment of applications.

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

Page 13: Java vs C# Johan Lindfors Microsoft AB. What are we to cover?  Java vs C#  Similarities and differencies  Specific issues  Deployment of applications.

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

Page 14: Java vs C# Johan Lindfors Microsoft AB. What are we to cover?  Java vs C#  Similarities and differencies  Specific issues  Deployment of applications.

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

Page 15: Java vs C# Johan Lindfors Microsoft AB. What are we to cover?  Java vs C#  Similarities and differencies  Specific issues  Deployment of applications.

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

Page 16: Java vs C# Johan Lindfors Microsoft AB. What are we to cover?  Java vs C#  Similarities and differencies  Specific issues  Deployment of applications.

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>

Page 17: Java vs C# Johan Lindfors Microsoft AB. What are we to cover?  Java vs C#  Similarities and differencies  Specific issues  Deployment of applications.

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

Page 18: Java vs C# Johan Lindfors Microsoft AB. What are we to cover?  Java vs C#  Similarities and differencies  Specific issues  Deployment of applications.

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

Page 19: Java vs C# Johan Lindfors Microsoft AB. What are we to cover?  Java vs C#  Similarities and differencies  Specific issues  Deployment of applications.

class JButton { ... public void addMouseListener(MouseListener added); public void removeMouseListener(MouseListener removed); ...}

Java Event Sources

Java event sources call event methods on registered listeners

Page 20: Java vs C# Johan Lindfors Microsoft AB. What are we to cover?  Java vs C#  Similarities and differencies  Specific issues  Deployment of applications.

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

Page 21: Java vs C# Johan Lindfors Microsoft AB. What are we to cover?  Java vs C#  Similarities and differencies  Specific issues  Deployment of applications.

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

Page 22: Java vs C# Johan Lindfors Microsoft AB. What are we to cover?  Java vs C#  Similarities and differencies  Specific issues  Deployment of applications.

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

Page 23: Java vs C# Johan Lindfors Microsoft AB. What are we to cover?  Java vs C#  Similarities and differencies  Specific issues  Deployment of applications.

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

Page 24: Java vs C# Johan Lindfors Microsoft AB. What are we to cover?  Java vs C#  Similarities and differencies  Specific issues  Deployment of applications.

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

Page 25: Java vs C# Johan Lindfors Microsoft AB. What are we to cover?  Java vs C#  Similarities and differencies  Specific issues  Deployment of applications.

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

Page 26: Java vs C# Johan Lindfors Microsoft AB. What are we to cover?  Java vs C#  Similarities and differencies  Specific issues  Deployment of applications.

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

Page 27: Java vs C# Johan Lindfors Microsoft AB. What are we to cover?  Java vs C#  Similarities and differencies  Specific issues  Deployment of applications.

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

Page 28: Java vs C# Johan Lindfors Microsoft AB. What are we to cover?  Java vs C#  Similarities and differencies  Specific issues  Deployment of applications.

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

Page 29: Java vs C# Johan Lindfors Microsoft AB. What are we to cover?  Java vs C#  Similarities and differencies  Specific issues  Deployment of applications.

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

Page 30: Java vs C# Johan Lindfors Microsoft AB. What are we to cover?  Java vs C#  Similarities and differencies  Specific issues  Deployment of applications.

<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

Page 31: Java vs C# Johan Lindfors Microsoft AB. What are we to cover?  Java vs C#  Similarities and differencies  Specific issues  Deployment of applications.

RemotingServer

Request

CreateUse

Dispose

RemoteObject

RemotingClient

Client activation

Page 32: Java vs C# Johan Lindfors Microsoft AB. What are we to cover?  Java vs C#  Similarities and differencies  Specific issues  Deployment of applications.

RemotingServer

Request

CreateUse

Dispose

RemoteObject

RemotingClient

Server activation – Single Call

Page 33: Java vs C# Johan Lindfors Microsoft AB. What are we to cover?  Java vs C#  Similarities and differencies  Specific issues  Deployment of applications.

RemotingServer

Request

CreateUse

Dispose

RemoteObject

RemotingClient

Server activation – Singleton

RemotingClient

Request

Use

Page 34: Java vs C# Johan Lindfors Microsoft AB. What are we to cover?  Java vs C#  Similarities and differencies  Specific issues  Deployment of applications.

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

Page 35: Java vs C# Johan Lindfors Microsoft AB. What are we to cover?  Java vs C#  Similarities and differencies  Specific issues  Deployment of applications.

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

Page 36: Java vs C# Johan Lindfors Microsoft AB. What are we to cover?  Java vs C#  Similarities and differencies  Specific issues  Deployment of applications.

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

Page 37: Java vs C# Johan Lindfors Microsoft AB. What are we to cover?  Java vs C#  Similarities and differencies  Specific issues  Deployment of applications.

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

Page 38: Java vs C# Johan Lindfors Microsoft AB. What are we to cover?  Java vs C#  Similarities and differencies  Specific issues  Deployment of applications.

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

Page 39: Java vs C# Johan Lindfors Microsoft AB. What are we to cover?  Java vs C#  Similarities and differencies  Specific issues  Deployment of applications.

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 …

Page 40: Java vs C# Johan Lindfors Microsoft AB. What are we to cover?  Java vs C#  Similarities and differencies  Specific issues  Deployment of applications.

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

Page 41: Java vs C# Johan Lindfors Microsoft AB. What are we to cover?  Java vs C#  Similarities and differencies  Specific issues  Deployment of applications.
Page 42: Java vs C# Johan Lindfors Microsoft AB. What are we to cover?  Java vs C#  Similarities and differencies  Specific issues  Deployment of applications.

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