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!
=== Keywords and backward compatibility === As the languages evolved, the language designers for both languages have faced situations where they wanted to extend the languages with new keywords or syntax. New keywords in particular may break existing code at source level, i.e. older code may no longer compile, if presented to a compiler for a later version of the language. Language designers are keen to avoid such regressions. The designers of the two languages have been following different paths when addressing this problem. Java language designers have avoided new keywords as much as possible, preferring instead to introduce new syntactic constructs that were not legal before or to reuse existing keywords in new contexts. This way they didn't jeopardize backward compatibility. An example of the former can be found in how the {{mono|for}} loop was extended to accept iterable types. An example of the latter can be found in how the {{mono|extends}} and (especially) the {{mono|super}} keywords were reused for specifying type bounds when generics were introduced in Java 1.5. At one time (Java 1.4) a new keyword {{mono|assert}} was introduced that was not reserved as a keyword before. This had the potential to render formerly valid code invalid, if for instance the code used {{mono|assert}} as an identifier. The designers chose to address this problem with a four-step solution: 1) Introducing a compiler switch that indicates if Java 1.4 or later should be used, 2) Only marking {{mono|assert}} as a keyword when compiling as Java 1.4 and later, 3) Defaulting to 1.3 to avoid rendering prior (non 1.4 aware code) invalid and 4) Issue warnings, if the keyword is used in Java 1.3 mode, to allow changes in the code. C# language designers have introduced several new keywords since the first version. However, instead of defining these keywords as ''global'' keywords, they define them as ''context sensitive'' keywords. This means that even when they introduced (among others) the {{mono|partial}} and {{mono|yield}} keywords in C# 2.0, the use of those words as identifiers is still valid as there is no clash possible between the use as keyword and the use as identifier, given the context. Thus, the present C# syntax is fully backward compatible with source code written for any prior version without specifying the language version to be used. {| class="wikitable" |- ! keyword !! feature, example usage |- | {{mono|checked}}, {{mono|unchecked}} || In C#, {{mono|checked}} statement blocks or expressions can enable run-time checking for [[arithmetic overflow]].<ref>{{cite web |url=http://www.25hoursaday.com/CsharpVsJava.html |title=A Comparison of Microsoft's C# Programming Language to Sun Microsystems' Java Programming Language: D. Now for Something Completely Different: 14. Overflow Detection |author=Dare Obasanjo |year=2007 |archive-url=https://web.archive.org/web/20120922081727/http://www.25hoursaday.com/CsharpVsJava.html |archive-date=22 September 2012 |access-date=11 September 2012 |df=dmy-all}}</ref> |- | {{mono|get}}, {{mono|set}} || C# implements [[Property (programming)|properties]] as part of the language syntax with their optional corresponding {{mono|get}} and {{mono|set}} accessors, as an alternative for the [[accessor method]]s used in Java, which is not a language feature but a coding-pattern based on method name conventions. |- | {{mono|goto}} || C# supports the <code>[[goto]]</code> keyword. This can be useful on occasion, for example for implementing [[finite-state machine]]s or for [[Code generation (compiler)|generated code]], but use of a more structured method of [[control flow]] is usually recommended (see [[Goto#Criticism|criticism of the goto statement]]). Java does not support the {{mono|goto}} statement (but {{mono|goto}} is a reserved word). However, Java does support labeled {{mono|break}} and {{mono|continue}} statements, which in certain situations can be used when a {{mono|goto}} statement might otherwise be used. <syntaxhighlight lang="csharp"> switch (color) { case Color.Blue: Console.WriteLine("Color is blue"); break; case Color.DarkBlue: Console.WriteLine("Color is dark"); goto case Color.Blue; // ... } </syntaxhighlight> |- | {{mono|lock}} || In C#, the {{mono|lock}} keyword is a shorthand for synchronizing access to a block of code across threads (using a {{mono|Monitor}}), wrapped in a {{mono|try}} ... {{mono|finally}} block. |- | {{mono|out}}, {{mono|ref}} || C# has support for output and reference [[parameter (computer science)|parameters]]. These allow returning multiple output values from a method, or passing values by reference. |- | {{mono|strictfp}} || Java uses <code>[[strictfp]]</code> to guarantee the results of floating point operations remain the same across platforms. |- | {{mono|switch}} || In C#, the [[switch statement]] also operates on strings and longs. Fallthrough is allowed for empty statements and possible via 'goto case' for statements containing code. Java's switch statement operates on strings (since [[Java 7]]) but not the {{mono|long}} primitive type, and falls through for all statements (excluding those with '{{mono|break}}').<ref>{{cite web |url=http://www.25hoursaday.com/CsharpVsJava.html |title=A Comparison of Microsoft's C# Programming Language to Sun Microsystems' Java Programming Language: An Ever So Slight Feeling of Dèjà Vu: 4. {{sic|nolink=y|reason=spelling and capitalization errors in source|switch Stat|ment}} |author=Dare Obasanjo |year=2007 |archive-url=https://web.archive.org/web/20120922081727/http://www.25hoursaday.com/CsharpVsJava.html |archive-date=22 September 2012 |access-date=7 September 2012 |df=dmy-all}}</ref> |- | {{mono|synchronized}} || In Java, the {{mono|synchronized}} keyword is a shorthand for synchronizing access to a block of code across threads (using a {{mono|Monitor}}), wrapped in a {{mono|try}} ... {{mono|finally}} block. |- | {{mono|throws}} || Java requires every method to declare the checked exceptions or superclasses of the checked exceptions that it can throw. Any method can also optionally declare the unchecked exception that it throws. C# has no such syntax. <syntaxhighlight lang=Java> public int readItem() throws java.io.IOException { // ... } </syntaxhighlight> |- | {{mono|using}} || In C#, {{mono|using}} causes the {{mono|Dispose}} method (implemented via the {{mono|IDisposable}} [[Interface (object-oriented programming)|interface]]) of the object declared to be executed after the code block has run or when an exception is thrown within the code block. <syntaxhighlight lang="csharp"> // Create a small file "test.txt", write a string, // ... and close it (even if an exception occurs) using (var file = new StreamWriter("test.txt")) { file.Write("test"); } </syntaxhighlight> In Java SE 7 a similar construct has been added<ref>{{cite web |url=http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html |title=The try-with-resources Statement (The Java Tutorials > Essential Classes > Exceptions) |publisher=Docs.oracle.com |date=28 February 2012 |access-date=24 February 2015}}</ref> called try-with-resources: <syntaxhighlight lang=Java> try (BufferedReader br = new BufferedReader(new FileReader(path))) { return br.readLine(); } </syntaxhighlight> |}
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)