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!
=== Unified type system === Both languages are statically typed with class-based object orientation. In Java the [[primitive types]] are special in that they are not [[object-oriented]] and they could not have been defined using the language itself. They also do not share a common ancestor with reference types. The Java [[reference type]]s all derive from a common root type. C# has a unified [[type system]] in which all types (besides unsafe pointers<ref name="unsafe pointers">{{cite web |url=https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/unsafe-code#pointer-types |title=Unsafe code, pointer types, and function pointers |publisher=learn.microsoft.com |date=29 May 2022 |access-date=14 April 2023}}</ref>) ultimately derive from a common root type. Consequently, all types implement the methods of this root type, and extension methods defined for the {{mono|object}} type apply to all types, even primitive {{mono|int}} literals and [[Delegate (CLI)|delegates]]. This allows C#, unlike Java, to support objects with encapsulation that are not reference types. In Java, compound types are synonymous with reference types; methods cannot be defined for a type unless it is also a ''class'' reference type. In C# the concepts of encapsulation and methods have been decoupled from the reference requirement so that a type can support methods and encapsulation without being a reference type. Only reference types support [[virtual method]]s and specialization, however. Both languages support many [[built-in type]]s that are copied and passed by value rather than by reference. Java calls these types [[primitive type]]s, while they are called ''simple types'' in C#. The primitive/simple types typically have native support from the underlying processor architecture. The C# simple types implement several [[Interface (object-oriented programming)|interfaces]] and consequently offer many methods directly on instances of the types, even on the literals. The C# type names are also merely ''aliases'' for [[Common Language Runtime]] (CLR) types. The C# {{code|System.Int64}} type is exactly the same type as the {{mono|long}} type; the only difference is that the former is the canonical [[.NET]] name, while the latter is a C# alias for it. Java does not offer methods directly on primitive types. Instead, methods that operate on primitive values are offered through companion [[primitive wrapper class]]es. A fixed set of such wrapper classes exist, each of which wraps one of the fixed set of primitive types. As an example, the Java {{mono|Long}} type is a [[reference type]] that wraps the primitive {{mono|long}} type. They are ''not'' the same type, however.
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)