Java與c#的一些細節區別

來源:互聯網
上載者:User

實習中用的語言是c#,第一次接觸到這種語言,然後寫的過程中,發覺和Java幾乎一摸一樣,好像根本是無縫切換,但細節仍有很大的區別,稱有空總結一波裡面的部分細節實現。

ps. 我寫c#過程中,發覺c#有很多優秀的特性,寫起來在方便很多,比如lambda,linkq等

1.Lambda VS DelegateJava底層實現:
 1 /** 2  * @Auther: Chang 3  * @Date: 2018/9/2 4  */ 5 public class JavaVsCSharp { 6     public static void main(String[] args) { 7  8         testLambda(() -> System.out.println("Hello World")); 9     }10 11     private static void testLambda(Print pt) {12         pt.print();13     }14 }15 16 @FunctionalInterface17 interface Print {18     void print();19 }

 

結果是列印出Hello World

 

我們對class檔案進行反編譯試一下,javap -c -p JavaVsCSharp.class

 

內部動態產生了一個私人的靜態方法

1 private static void lambda$main$0() {2     // 空3 }

然後然後。。。。我就不會調試了。。。。。。網上的調試我沒看懂,怎麼跑來跑去的(菜是一種罪過)

不過最後最後就產生了一個內部類,和一個內部方法

 1 private static void lambda$main$0() { 2     System.out.println("Hello World!!!"); 3 } 4  5 static final class JavaVsCSharp$$Lambda$1 implements Print { 6     private JavaVsCSharp$$Lambda$1() { 7     } 8  9     @Override10     public void print() {11         lambda$main$0();12     }13 }

 

c#底層實現:

轉自:你知道C#中的Lambda運算式的演化過程嗎?

說白了,c#就是一個delegate委託,類似於c中的函數指標。每一個Lambda對應著一個函數方法,delegate指向這個函數方法,現在Lambda把它簡化了,即不用重新寫一遍了。 

 

2.Linq VS Stream運算式Java Stream實現:
 1 ** 2  * @Auther: Chang 3  * @Date: 2018/9/2 4  */ 5 public class JavaVsCSharp { 6     public static void main(String[] args) { 7  8         List<Integer> list = new ArrayList<>(); 9         for (int i = 0; i < 10; i++) {10             list.add(new Random().nextInt(100));11         }12         print(list, "原資料");13         list = list.stream().sorted(Comparator.naturalOrder()).distinct()14                 .filter(x -> x > 50 || x % 5 == 0).collect(Collectors.toList());15         print(list, "處理後資料");16     }17 18     private static void print(List<Integer> list, String s) {19         System.out.print(s + ":  ");20         list.forEach(x -> System.out.print(x + "  "));21         System.out.println();22     }23 }

 

我們可以看到list得到stream流介面以後,就有很多方法可以調用了

 

 

Stream這個介面的實現方法都在 ReferencePipeline 實現完了。(話說不知為何,感覺這個類的實現很亂,代碼風格差的特別多)

 

c#linq實現:
 1 class IntroToLINQ 2 {         3     static void Main() 4     { 5         // The Three Parts of a LINQ Query: 6         //  1. Data source. 7         int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 }; 8  9         // 2. Query creation.10         // numQuery is an IEnumerable<int>11         var numQuery =12             from num in numbers13             where (num % 2) == 014             select num;15 16         // 3. Query execution.17         foreach (int num in numQuery)18         {19             Console.Write("{0,1} ", num);20         }21     }22 }

 

是不是很像SQL語句,用起來也特別的清晰明了。這點可以說c#甩了Java幾條街。

由於很少用Linq,我的VS也到期了,難受,官方教程瞭解下 => 官方Linq教程

 

後言:可能c#一些東西沒有深入到底層,因為VS到期,並且已經很少用了。所以哪裡有不對的地方,老哥們,還希望指出,謝謝

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.