Upload
abedi
View
35
Download
1
Embed Size (px)
DESCRIPTION
Java vs C# Michał Prządka Tomasz Nowak. KONSPEKT. 1. Rys historyczny 2. Charakterystyka platformy Java i .NET 3. Hello world 4. Przegląd konstrukcji języków 5. Środowiska programistyczne 6. Rynki zbytu 7. Podsumowanie. Java: powstaje w 1990 – projekt Oak (Sun) - PowerPoint PPT Presentation
Citation preview
Java vs C#
Michał Prządka Tomasz Nowak
KONSPEKT
1. Rys historyczny
2. Charakterystyka platformy Java i .NET
3. Hello world
4. Przegląd konstrukcji języków
5. Środowiska programistyczne
6. Rynki zbytu
7. Podsumowanie
Rys historyczny• Java:
– powstaje w 1990 – projekt Oak (Sun)– Pierwsza dostępna implementacja: 1995 (Java
1.0)– obecnie: Java 6
• C#:– powstaje w 2000– głowny architektem jest Anders Hejlsberg– silne nawiązania do Javy, C++, Smalltalka i
Delphi– obecnie: C# 2.0
• Sourceforge.net (21 stycznia 2007):– # projektów w Javie: 21 831– # projektów w C#: 4 844
Charakterystyka platformy Java i .NET
JVM vs CLR
Automatyczne zarządzanie pamięcią
Wielosystemowość vs
Monosystemowość
Hello world - made by Java
class Hello {
public static void main(String[] args) {
System.out.println("Hello world");}
}
Przegląd konstrukcji języków
Podobieństwa
• Składnia i słowa kluczowe• Wszystko jest obiektem• Brak metod globalnych• Wielodziedziczenie• Obsługa wyjątków• Enumeratory• Szablony• Dokumentowanie kodu
Różnice
! ( C# == JAVA )
switch - caseswitch(foo){
case "A": Console.WriteLine("A seen"); break;
case "B": case "C":
Console.WriteLine("B or C seen");
break; case "D": Console.WriteLine("D seen"); case "E": Console.WriteLine("E seen");
break; }
Pakietypackage Company;
import java.util.*;
import Carnage4life.MyOtherClass;
{
public class MyClass
{
int x;
void doStuff(){}
}
}
Zdefiniowana w osobnym pliku (w innym pakiecie/katalogu)
Jako katalog
Jako plik
Przestrzenie nazwusing System; namespace Company{
public class MyClass {
int x; void doStuff(){}
} namespace Carnage4life{
public class MyOtherClass {
int y; void doOtherStuff(){} public static void Main(string[] args){ Console.WriteLine("Hey, I can nest namespaces"); }
}}
…
Metody wirtualnepublic class Parent{
public void DoStuff(string str){ Console.WriteLine("In Parent.DoStuff: " + str); }
} public class Child: Parent{
public void DoStuff(string str){ Console.WriteLine("In Child.DoStuff: " + str); }
}
Child ch = new Child();ch.DoStuff("Test"); ((Parent) ch).DoStuff("Second Test");
Wish you were here
( czyli czego brakuje w Javie )
Parametry ref i out
public static void ChangeMe(out string s){ s = "Changed"; }
public static void Swap(ref int x, ref int y){ int z = x; x = y; y = z; }
public static void Main(string[] args){
int a = 5, b = 10; string s; Swap(ref a, ref b); ChangeMe(out s); Console.WriteLine("a := " + a + ", b := " + b + ", s = " + s);
}
Włączenie/wyłączenie sprawdzania poprawności
public static void Main(string[ ] args){
int num = 5000; byte a = (byte) num; checked{
byte b = (byte) num; } unchecked{
byte c = (byte) num; }
}
Właściwości
class Woman…
private int age; public int Age
{get { return age - 10; }set { age = value; }
}…
w.Age = 35;Console.Writeln( w.Age );
Indeksatorypublic class Stack{
public object this[int index]{
get { return GetNode(index).Value; }set { GetNode(index).Value = value; }
}…
}
Stack s= new Stack();…s[0]=10;Console.WriteLine(s[0]);
Operatory
class MyNumber{
private int value; public MyNumber(int value){
this.value = value; } public static MyNumber operator+(MyNumber n1, MyNumber n2){
return new MyNumber(n1.value + n2.value); }
…}
Wskaźniki
int[] array = {9, 1, 3, 6, 11, 99, 37, 17, 0, 12};
fixed( int* iptr = array )
{
Sort(iptr, array.Length);
}
Wskaźnikipublic static unsafe void Swap(int* a, int*b){
int temp = *a;*a = *b;
*b = temp; }
public static unsafe void Sort(int* array, int size){
for(int i= 0; i < size - 1; i++) for(int j = i + 1; j < size; j++)
if(array[i] > array[j]) Swap(&array[i], &array[j]);
}
Struktury
struct Point {
public int x; public int y; public Point( int x, int y){ this.x = x; this.y = y; } public static void Main(string[] args){
Point start = new Point(5, 9); …
} …
Zdarzenia, delegety
class EvenNumberEvent : EventArgs
{
internal int number;
public EvenNumberEvent(int number) : base()
{ this.number = number; }
}
class Publisher{
public delegate void EvenNumberSeenHandler(object sender, EventArgs e); public event EvenNumberSeenHandler EvenNumHandler; protected void OnEvenNumberSeen(int num){
if(EvenNumHandler!= null) EvenNumHandler(this, new EvenNumberEvent(num));
} public void RunNumbers(){
Random r = new Random((int) DateTime.Now.Ticks); for(int i=0; i < 20; i++){
int current = (int) r.Next(20); Console.WriteLine("Current number is:" + current); if((current % 2) == 0) OnEvenNumberSeen(current);
} }
}
public class EventTest{
public static void EventHandler(object sender, EventArgs e){
Console.WriteLine("\t\tEven Number Seen:" + ((EvenNumberEvent)e).number);
} public static void Main(string[] args){
Publisher pub = new Publisher(); pub.EvenNumHandler += new
Publisher.EvenNumberSeenHandler(EventHandler); pub.RunNumbers();
pub.EvenNumHandler -= new
Publisher.EvenNumberSeenHandler(EventHandler); }
}
Wish you were here
( czyli czego brakuje w C# )
Klasy anonimowe (w C# - metody anonimowe)
Button buttonC = new Button("C");
buttonC.addMouseListener( new MouseListener()
{
public void windowClosing(WindowEvent e){
System.out.println(
"Close button clicked");
System.exit(0);
}//end windowClosing
}//end class definition
);//end addWindowListener
To klasy lokalne, które nie mają nazwy...
Wyjątki typu checked
public void readFile() throws IOException {
File f = new ...
...
...
}
Wyjątki, które muszą być obsłużone – albo przez try i catch albo przez throws w sygnaturze metody – np. IOException
Przykłady wyjątków nie-checked – wszystkie typu RuntimeException – np. NullPointerException, ArrayOutOfBounds
Rynki zbytu
• Desktop
• Web
• Mobile
Środowiska programistyczne
Java C#
Last but not least
Bibliografia
• http://www.25hoursaday.com/CsharpVsJava.html
• http://en.wikipedia.org/wiki/Comparison_of_C_Sharp_and_Java
Podsumowanie