• Java与c#的一些细节区别


    实习中用的语言是c#,第一次接触到这种语言,然后写的过程中,发觉和Java几乎一摸一样,好像根本是无缝切换,但细节仍有很大的区别,称有空总结一波里面的部分细节实现。

    ps. 我写c#过程中,发觉c#有很多优秀的特性,写起来在方便很多,比如lambda,linq等

    1.Lambda VS Delegate

    Java底层实现:

     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 @FunctionalInterface
    17 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     @Override
    10     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 numbers
    13             where (num % 2) == 0
    14             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过期,并且已经很少用了。所以哪里有不对的地方,老哥们,还希望指出,谢谢

  • 相关阅读:
    编程之美-2.18 数组分割
    话题模型
    暂时跳过的Leetcode题目
    LDA主题模型
    二叉树非递归的统一实现
    取余和取模运算
    IDM非补丁破解方法
    两种建立堆的方法HeapInsert & Heapify
    非阻塞connect:Web客户程序
    非阻塞connect
  • 原文地址:https://www.cnblogs.com/wenbochang/p/9574412.html
Copyright © 2020-2023  润新知