Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Comparison of C Sharp and Java
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Functional programming == {| class="wikitable" style="width:80%;" |- ! style="width:40%;"| [[#Functional programming|Functional programming]] !! style="width:30%;"|Java !! style="width:30%;"|C# |- |Method references || {{yes}}<ref name="oracle1"/> || {{yes}} |- |[[Closure (computer science)|Closures]] || {{some|All lambdas do not introduce a new level of scope. All referenced variables must be effectively final}} || {{yes}} |- |[[Anonymous function|Lambda expressions]] || {{yes}}<ref>{{cite web |url=http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html |title=Lambda Expressions (The Java Tutorials > Learning the Java Language > Classes and Objects) |publisher=Docs.oracle.com |date=28 February 2012 |access-date=24 February 2015}}</ref> || {{yes}} |- |[[Abstract syntax tree|Expression trees]] || {{no}} || {{yes}} |- |Generic query language/API || {{yes}}; Java Stream API [[Monad (functional programming)|(Monad)]]<ref>{{cite web |url=https://docs.oracle.com/javase/tutorial/collections/streams/index.html |title=Lesson: Aggregate Operations (The Java Tutorials > Collections) |publisher=Docs.oracle.com |date=28 February 2012 |access-date=24 February 2015}}</ref> || {{yes}} |- |Tail recursion compiler optimizations || {{no}}<ref>{{Cite web |url=https://blogs.oracle.com/javamagazine/post/curly-braces-java-recursion-tail-call-optimization |title=Curly Braces #6: Recursion and tail-call optimization |date=2022-11-07 |access-date=2024-07-17 |last=Bruno |first=Eric |archive-url=https://web.archive.org/web/20240717183057/https://blogs.oracle.com/javamagazine/post/curly-braces-java-recursion-tail-call-optimization |archive-date=2024-07-17 |url-status=live}}</ref> || {{some|Only on x64}}<ref>{{cite web |url=http://blogs.msdn.com/b/clrcodegeneration/archive/2009/05/11/tail-call-improvements-in-net-framework-4.aspx |title=Tail Call Improvements in .NET Framework 4 |author=Grant Richins |work=[[MSDN Blogs]] |date=11 May 2009}}</ref> |} === Closures === A closure is an inline function that captures variables from its lexical scope. C# supports closures as anonymous methods or [[Anonymous function#C#lambda expressions|lambda expressions]] with full-featured [[Closure (computer science)|closure]] semantics.<ref>{{cite web |url=http://msdn.microsoft.com/en-ca/magazine/cc301810.aspx | work = MSDN Magazine |title=An Introduction to Delegates |date=April 2001 |access-date =23 December 2008 | last = Richter | first = Jeffrey |author-link = Jeffrey Richter}}</ref><ref>{{cite web |url=http://diditwith.net/PermaLink,guid,235646ae-3476-4893-899d-105e4d48c25b.aspx | work = Did it with .NET |title=What's in a Closure? | last = Campbell | first = Dustin |date=9 February 2007 |access-date=23 December 2008 |archive-url=https://web.archive.org/web/20140815085318/http://diditwith.net/PermaLink,guid,235646ae-3476-4893-899d-105e4d48c25b.aspx |archive-date=15 August 2014 |url-status=dead |df=dmy-all}}</ref> In Java, anonymous inner classes will remain the preferred way to emulate closures until Java 8 has become the new standard. This is a more verbose construction. This approach also has some differences compared to real closures, notably more controlled access to variables from the enclosing scopes: only final members can be referenced. Java 8, however introduces lambdas that fully inherit the current scope and, in fact, do not introduce a new scope. When a reference to a method can be passed around for later execution, a problem arises about what to do when the method has references to variables/parameters in its lexical scope. C# closures can access any variable/parameter from its lexical scope. In Java's anonymous inner classes, only references to final members of the lexical scope are allowed, thus requiring the developer to mark which variables to make available, and in what state (possibly requiring boxing). ===Lambdas and expression trees=== C# and Java feature a special type of in-line [[Closure (computer science)|closures]] called [[Anonymous function|lambdas]]. These are anonymous methods: they have a signature and a body, but no name. They are mainly used to specify local function-valued arguments in calls to other methods, a technique mainly associated with [[functional programming]]. C#, unlike Java, allows the use of lambda functions as a way to define special data structures called expression trees. Whether they are seen as an executable function or as a data structure depends on compiler [[type inference]] and what type of variable or parameter they are assigned or cast to. Lambdas and expression trees play key roles in [[Language Integrated Query]] (LINQ).
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)