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!
== Expressions and operators == {| class="wikitable" style="width:80%;" |- ! style="width:40%;"| Expressions and operators !! style="width:30%;"|Java !! style="width:30%;"|C# |- |Arithmetic operators || {{yes}} || {{yes}} |- |Logical operators || {{yes}} || {{yes}} |- |Bitwise logic operators || {{yes}} || {{yes}} |- |Conditional || {{yes}} || {{yes}} |- |String concatenation || {{yes}} || {{yes}} |- |[[String interpolation]] || {{Yes | style=background: #8CF;}}; Since Java 21 ([[String interpolation#Java|details]]) || {{Yes}}<ref>{{Cite web |url=https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated |title=String interpolation using $ |date=8 April 2023 |publisher=learn.microsoft.com |access-date=20 April 2023}}</ref> |- |Verbatim (here-)strings || {{Yes}}<ref>{{cite web |url=https://openjdk.java.net/jeps/378 |title=JEP 378: Text Blocks |access-date=5 August 2020}}</ref> || {{yes}}<ref>{{cite web |url=http://www.25hoursaday.com/ |title=A Comparison of Microsoft's C# Programming Language to Sun Microsystems' Java Programming Language: D. Now for Something Completely Different: 13. Verbatim Strings |author=Dare Obasanjo |year=2007 |publisher=Dare Obasanjo |archive-url=https://web.archive.org/web/20120919093308/http://25hoursaday.com/ |archive-date=19 September 2012 |access-date=11 September 2012 |url-status=dead |df=dmy-all}}</ref> |- |Casts || {{yes}} || {{yes}} |- |[[Boxing (programming)|Boxing]] || {{yes}}; implicit || {{yes}}; implicit |- |[[Auto-unboxing|Unboxing]] || {{yes}}; implicit || {{yes}}; explicit |- |Lifted operators || {{no | style=background: #8CF;}}; See {{mono|java.util.Optional}} || {{yes}} |- |Overflow control || {{no}} || {{yes}} |- |Strict floating point evaluation || {{yes}}; opt-in/out || {{yes}}; opt-in<ref>{{cite web |url=https://msdn.microsoft.com/en-us/library/aa289157%28v=vs.71%29.aspx#floapoint_topic4 |title=Microsoft Visual C++ Floating-Point Optimization |author=Eric Fleegal |year=2004 |publisher=MSDN |access-date=1 January 2016 }}</ref> |} ===Boxing and unboxing=== Both languages allow [[Autoboxing|automatic boxing]] and unboxing, i.e. they allow for implicit casting between any primitive types and the corresponding reference types. In C#, the primitive types are subtypes of the Object type. In Java this is not true; any given primitive type and the corresponding wrapper type have no specific relationship with each other, except for autoboxing and unboxing, which act as [[syntactic sugar]] for interchanging between them. This was done intentionally, to maintain backward compatibility with prior versions of Java, in which no automatic casting was allowed, and the programmer worked with two separate sets of types: the primitive types, and the wrapper (reference) type hierarchy.<ref name="Jcp.org">{{cite web |url=http://www.jcp.org/en/jsr/detail?id=14 |title=The Java Community Process(SM) Program β JSRs: Java Specification Requests β detail JSR# 14 |publisher=Jcp.org |access-date=24 February 2015}}</ref> This difference has the following consequences. First of all, in C#, primitive types can define methods, such as an override of Object's {{code|ToString()}} method. In Java, this task is accomplished by the [[primitive wrapper class]]es. Secondly, in Java an extra cast is needed whenever one tries to directly [[Reference (computer science)|dereference]] a primitive value, as it will not be boxed automatically. The expression {{java|((Integer)42).toString()}} will convert an integer literal to string in Java while {{java|42.ToString()}} performs the same operation in C#. This is because the latter one is an instance call on the primitive value {{mono|42}}, while the former one is an instance call on an object of type {{Javadoc:SE|package=java.lang|java/lang|Integer}}. Finally, another difference is that Java makes heavy use of boxed types in [[Generic programming|generics]] (see [[#Generics|below]]).
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)