身をかがめる虎、隠しドラゴン

Java vs. C#...永遠の議論よりも優れているものは何でしょうか? いいえ、この記事は次のベンチマーク専用ではありません。また、聖戦でもありません。質問する価値さえありません。



各タスクには独自のツールがあります。 たとえば、C#とRubyを比較するのは意味がありません。 それらの意図する目的は完全に異なっており、性質も同じです。 ただし、哲学で最も近いのはC#とJavaです。



非常に多くの場合、Javaで書いている同僚は、C#が提供する(!!!)ものが多い(!!!)ことすら気づいていません。



主観性なしでC#とJava 調べ、1つまたは別の機能の内部構造を調べることに興味がある場合は、先に進んでください。



history少しの歴史



C#言語は2001年に登場し、その開発は1999年に開始されました。 それは、Java 1.4に非常に似ていました。 ただし、私たちが知っている最新のC#は、バージョン2.0(Java 5のリリース時間に対応)で検討し始める必要があります。



C#はJavaから多くを奪うという意見があります。 しかし、私はこれに強く反対します。 私の意見では、C#は主にCが「オブジェクトを持つ」、またはC ++が「人間の顔を持つ」です。



記事とその中の議論がこの声明を確認することを願っています。

ウィキペディアを再告知しません。 したがって、私たちはすぐに先に進み、重要な違いと利点を検討します。



最初にJVMと共通言語ランタイム(CLR)自体の機能を確認し、次に構文シュガーC#を確認します。



エピソードI:バイトコード



.NETとJavaの両方がバイトコードを使用します。 もちろん、フォーマット自体とは別に、1つの非常に重要な違いがあります-多態性です。



CIL(Common Intermediate Language、通称MSIL、通称IL)は、ポリモーフィック(一般化された)命令を持つバイトコードです。



そのため、Javaがさまざまなタイプの操作の各タイプに個別の命令を使用する場合(たとえば、 fadd -2つのfloatの追加、 iadd -2つの整数の追加)、各タイプの操作のCILには、ポリモーフィックパラメータを持つ命令が1つだけあります(たとえば、1つの命令だけがあります-add、floatとintegerの両方を追加します)。 適切なx86命令の生成を決定する問題は、JITにかかっています。



両方のプラットフォームでの命令の数はほぼ同じです。 JavaCILバイトコードコマンドのリストを比較すると、Javaには206とCIL 232があることがわかりますが、Javaには多くのコマンドが単純に互いの機能を繰り返すことを忘れないでください。



エピソードIII:ジェネリック(パラメーター化された型||パラメトリック多型)



ご存じのように、Javaは型消去メカニズムを使用します。 ジェネリックのサポートはコンパイラーのみが提供し、ランタイムは提供しません。コンパイル後、型自体に関する情報は利用できません。



たとえば、次のコード:



List<String> strList = new ArrayList<String>();
List<Integer> intList = new ArrayList<Integer>();

bool areSame = strList.getClass() == intList.getClass();
System.out.println(areSame);

      
      





true.



T java.lang.Object.



List<String> strList = new ArrayList<String>();
strList.add("stringValue");

String str = strList.get(0);

      
      





:



List list = new ArrayList();
list.add("stringValue");
String x = (String) list.get(0);

      
      





, .



.NET, , generics compile-time, run-time.



generics Java? , .NET’a:





.. generics , . boxing/unboxing.



IV: Types



Java -. . : (integer, float ..) java.lang.Object. generics Java primitive types.



- - everything is object.



. .



C# . – struct.



:



public struct Quad
{
    int X1;
    int X2;
    int Y1;
    int Y2;
}

      
      





generics C# value types (int, float ..)



Java :



List<Integer> intList = new ArrayList<Integer>(); 

      
      





:



List<Integer> intList = new ArrayList<int>(); 

      
      





C# .



.NET.



.NET 3 : value, reference pointer types.



, value type – primitive type Java. System.Object, , ( : value type , , boxing).



Reference type – , reference types Java.



Pointer type – .NET’a. , CLR !



:



struct Point
{
    public int x;
    public int y;
}

unsafe static void PointerMethod()
{
    Point point;
    Point* p = &point;
    p->x = 100;
    p->y = 200;

    Point point2;
    Point* p2 = &point2;
    (*p2).x = 100;
    (*p2).y = 200;
}

      
      





C++ , ?



V: C#



, C#:



C# , .. GetXXX, SetXXX. , object.PropertyX, object.GetPropertyX.



:



public class TestClass
{
    public int TotalSum
    {
        get
        {
            return Count * Price;
        }
    }

    //  -    
    public int Count
    {
        get; 
        set;
    }

    public int Price
    {
        get
        {
            return 50;
        }
    }
}

      
      





:



public class TestClass
{

    /*
    *  
    */
    private int <Count>k__BackingField;
    //  -    
    public int Count
    {
        get { return <Count>k__BackingField; }
        set { <Count>k__BackingField = value; }
    }
}

      
      





C/C++. . – callback , .



.NET – .



DLR (Dynamic Language Runtime – .NET), dynamic C# 4 – . C# dynamic.



Da Vinci Java, .. VM.



C#:



public class TestClass
{
    public delegate int BinaryOp(int arg1, int arg2);

    public int Add(int a, int b)
    {
        return a + b;
    }

    public int Multiply(int first, int second)
    {
        return first * second;
    }

    public void TestDelegates()
    {
        BinaryOp op = new BinaryOp(Add);
        int result = op(1, 2);
        Console.WriteLine(result);
        //: 3

        op = new BinaryOp(Multiply);
        result = op(2, 5);
        Console.WriteLine(result);
        //: 10
    }
}

      
      





C:



int Add(int arg1, int arg2) 
{
    return arg1 + arg2;
}

void TestFP() 
{
     int (*fpAdd)(int, int); 
     fpAdd = &Add; //  
     int three = fpAdd(1, 2); //    
}

      
      





, ? C ( float arg1, float arg2), C# — . C# , .



- . , EventDispatcher’, Publisher/Subscriber. . .



:



public class MyClass
{
    private string _value;

    public delegate void ChangingEventhandler(string oldValue);

    public event ChangingEventhandler Changing;

    public void OnChanging(string oldvalue)
    {
        ChangingEventhandler handler = Changing;
        if (handler != null) 
            handler(oldvalue);
    }

    public string Value
    {
        get
        {
            return _value;
        }
        set
        {
            OnChanging(_value);
            _value = value;
        }
    }

    public void TestEvent()
    {
        MyClass instance = new MyClass();
        instance.Changing += new ChangingEventhandler(instance_Changing);
        instance.Value = "new string value";
        //   instance_Changing
    }

    void instance_Changing(string oldValue)
    {
        Console.WriteLine(oldValue);
    }
}

      
      





– , .. .



:



public class TestClass
{
    public delegate int BinaryOp(int arg1, int arg2);

    public void TestDelegates()
    {
        BinaryOp op = new BinaryOp(delegate(int a, int b)
                                   {
                                       return a + b;
                                   });
        int result = op(1, 2);
        Console.WriteLine(result);
        //: 3
    }
}

      
      





?



-.



- — , , .



public class TestClass
{
    public delegate int BinaryOp(int arg1, int arg2);

    public void TestDelegates()
    {
        BinaryOp op = new BinaryOp((a, b) => a + b);
        int result = op(1, 2);
        Console.WriteLine(result);
        //: 3
    }
}

      
      





? :



public class MyClass
{
    /*
     *   
     */

    public void TestEvent()
    {
        MyClass instance = new MyClass();
        instance.Changing += (o) => Console.WriteLine(o);
        instance.Value = "new string value";
        //  Console.WriteLine
    }
}

      
      





, (!!!). , C# , .. .



-, LINQ (Language Integrated Query).



LINQ? MapReduce LINQ?



public static class MyClass
{
    public void MapReduceTest()
    {
        var words = new[] {"...some text goes here..."};
        var wordOccurrences = words
            .GroupBy(w => w)
            .Select(intermediate => new
            {
                Word = intermediate.Key,
                Frequency = intermediate.Sum(w => 1)
            })
            .Where(w => w.Frequency > 10)
            .OrderBy(w => w.Frequency);
    }
}

      
      





SQL- :



public void MapReduceTest()
{
    string[] words = new string[]
        {
            "...some text goes here..."
        };
    var wordOccurrences =
        from w in words
        group w by w
        into intermediate
        select new
            {
                Word = intermediate.Key,
                Frequency = intermediate.Sum((string w) => 1)
            }
        into w
        where w.Frequency > 10
        orderby w.Frequency
        select w;
}

      
      





LINQ (GroupBy().Select().Where() ..), –



new
    {
        Word = intermediate.Key,
        Frequency = intermediate.Sum(w => 1)
     }

      
      





… ? – .



var. C++ 11 auto.



:



public void MapReduceTest()
{
    string[] words = new string[] { "...some text goes here..." };
    var wordOccurrences = Enumerable.OrderBy(Enumerable.Where(Enumerable.Select(Enumerable.GroupBy<string, string>(words, delegate (string w) {
        return w;
    }), delegate (IGrouping<string, string> intermediate) {
        return new { Word = intermediate.Key, Frequency = Enumerable.Sum<string>(intermediate, (Func<string, int>) (w => 1)) };
    }), delegate (<>f__AnonymousType0<string, int> w) {
        return w.Frequency > 10;
    }), delegate (<>f__AnonymousType0<string, int> w) {
        return w.Frequency;
    });
}

      
      





[ ]



C#, .. .



, C# 5 – , , C++ 11!





C# Java , (.NET Java). — .



C# — Java. Microsoft, COOL (C-like Object Oriented Language). C/C++? .



, ( ) , .



!




All Articles