Some details of Java and C # differ

Source: Internet
Author: User

Internship in the language is C #, the first time to contact the language, and then write the process, found and Java almost a touch, as if it is a seamless switch, but the details are still very different, said the empty summary of a wave inside the details of the implementation.

PS. I wrote C # process, found that C # has a lot of excellent features, written in a lot of convenience, such as LAMBDA,LINKQ

1.Lambda VS Delegatejava Bottom-level implementations:
1 /**2 * @Auther: Chang3 * @Date: 2018/9/24  */5  Public classJavavscsharp {6      Public Static voidMain (string[] args) {7 8Testlambda ((), System.out.println ("Hello World"));9     }Ten  One     Private Static voidTestlambda (Print pt) { A pt.print (); -     } - } the  - @FunctionalInterface - InterfacePrint { -     voidprint (); +}

The result is a print out Hello World

We try to decompile the class file, Javap-c-P Javavscsharp.class

Internally dynamically generated a private static method

1 Private Static void lambda$main$0() {2     ///  empty 3 }

And then .... I would not have debugged ... Online debugging I don't understand, how to run to run (food is a sin)

In the end, however, an inner class is generated, and an internal method

1 Private Static voidLambda$main$0() {2System.out.println ("Hello World!!!");3 }4 5 Static Final classjavavscsharp$ $Lambda $ImplementsPrint {6     Privatejavavscsharp$ $Lambda $() {7     }8 9 @OverrideTen      Public voidprint () { OneLambda$main$0(); A     } -}

C # Bottom-level implementations:

Turn from: Do you know the evolution of lambda expressions in C #?

To be blunt, C # is a delegate delegate, similar to a function pointer in C. Each lambda corresponds to a function method, delegate points to this function method, and now lambda simplifies it without having to re-write it again.

2.Linq VS Stream Expression Java Stream implementation:
1**2*@Auther: Chang3* @Date: 2018/9/24*/5  Public classJavavscsharp {6      Public Static voidMain (string[] args) {7 8list<integer> list =NewArraylist<>();9          for(inti = 0; I < 10; i++) {TenList.add (NewRandom (). Nextint (100)); One         } APrint (list, "original data"); -List =List.stream (). Sorted (Comparator.naturalorder ()). DISTINCT () -. filter (x > x | | x% 5 = = 0). Collect (Collectors.tolist ()); thePrint (list, "post-processing data"); -     } -  -     Private Static voidPrint (list<integer>list, String s) { +System.out.print (S + ":")); -List.foreach (X-System.out.print (x + "")); + System.out.println (); A     } at}

We can see that after the list gets the stream stream interface, there are many ways to call the

Stream the implementation of this interface is done in referencepipeline . (I don't know why, I feel this kind of implementation is very messy, code style is very bad)

C#LINQ implementation:
1 classintrotolinq2 {        3     Static voidMain ()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.Ten         //Numquery is an ienumerable<int> One         varNumquery = A              fromNuminchnumbers -             where(num%2) ==0 -             Selectnum; the  -         //3. Query execution. -         foreach(intNuminchnumquery) -         { +Console.Write ("{0,1}", num); -         } +     } A}

is not very like the SQL statement, the use is also particularly clear. This can be said C # dumped Java a few streets.

Since I rarely use LINQ, my vs also expires, uncomfortable, official tutorial learn about the under-the-Official LINQ tutorial

later: Maybe C # some things do not go deep into the bottom, because the VS expires, and has been rarely used. So where there's the wrong place, old buddy, also want to point out, thank you

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.