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!
=== Data types === ==== Numeric types ==== ===== Signed integers ===== Both Java and C# support [[signedness|signed]] integers with bit widths of 8, 16, 32 and 64 bits. They use the same name/aliases for the types, except for the 8-bit integer that is called a {{mono|byte}} in Java and a {{mono|sbyte}} (signed byte) in C#. ===== Unsigned integers ===== C# supports [[signedness|unsigned]] in addition to the [[signedness|signed]] integer types. The unsigned types are {{mono|byte}}, {{mono|ushort}}, {{mono|uint}} and {{mono|ulong}} for 8, 16, 32 and 64 bit widths, respectively. Unsigned arithmetic operating on the types are supported as well. For example, adding two unsigned integers ({{mono|uint}}s) still yields a {{mono|uint}} as a result; not a long or signed integer. Java does not feature unsigned integer types. In particular, Java lacks a primitive type for an unsigned [[byte]]. Instead, Java's {{mono|byte}} type is [[sign extension|sign extended]], which is a common source of bugs and confusion.<ref>{{cite book |title=Java puzzlers : traps, pitfalls, and corner cases|year=2005 |publisher=Addison-Wesley|location=Upper Saddle River, NJ [u.a.]|isbn=978-0-321-33678-1|author=Joshua Bloch; Neal Gafter|edition=5. print.|page=36|quote=The lesson for language designers is that sign extension of byte values is a common source of bugs and confusion. The masking that is required to suppress sign extension clutters programs, making them less readable. Therefore, the byte type should be unsigned.}}<!-- |access-date=6 October 2012--></ref> Unsigned integers were left out of Java deliberately because [[James Gosling]] believed that programmers would not understand how unsigned arithmetic works.<blockquote>In programming language design, one of the standard problems is that the language grows so complex that nobody can understand it. One of the little experiments I tried was asking people about the rules for unsigned arithmetic in C. It turns out nobody understands how unsigned arithmetic in C works. There are a few obvious things that people understand, but many people don't understand it.<ref name="oracle.com"/><ref>{{cite web |url=http://www.artima.com/intv/gosling3P.html |title=James Gosling on Java, May 2001 |publisher=Artima.com |date=10 May 2001 |access-date=24 February 2015}}</ref></blockquote> Java versions 8 and 9 added some limited built-in unsigned integer operations, but they are only exposed as static methods on the primitive wrapper classes; they operate on signed primitive integer types, treating them as if they were unsigned.<ref>{{cite web |url=https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html |title=Integer (Java Platform SE 8) |publisher=Docs.oracle.com |access-date=20 Apr 2023}}</ref> ===== High-precision decimal numbers ===== C# has a type and literal notation for high-precision (28 decimal digits) decimal arithmetic that is appropriate for financial and monetary calculations.<ref>{{cite web |title=decimal |url=http://msdn.microsoft.com/en-us/library/364x0z75(v=vs.110).aspx | work=C# Reference |date=19 August 2012 |publisher=Microsoft}}</ref><ref name="thedecimaltype"/><ref>{{cite book | last=Mok | first=Heng Ngee |title=From Java to C? : a developer's guide |year=2003 |publisher=Addison-Wesley|location=Harlow, England | isbn=978-0-321-13622-0 | chapter=9.5. The decimal type}}</ref> Contrary to the {{mono|float}} and {{mono|double}} data types, decimal fractional numbers such as 0.1 can be represented exactly in the decimal representation. In the float and double representations, such numbers often have non-terminating binary expansions, making those representations more prone to round-off errors.<ref name=thedecimaltype>{{cite book | last=Sestoft | first=Jon Jagger, Nigel Perry, Peter |title=C? annotated standard |year=2007 |publisher=Elsevier/Morgan Kaufmann Publishers | location=Amsterdam | isbn=978-0-12-372511-0 | chapter=11.1.7 The decimal type}}</ref> While Java lacks such a built-in type, the Java library does feature an ''arbitrary precision'' decimal type. This is not considered a language type and it does not support the usual arithmetic operators; rather it is a reference type that must be manipulated using the type methods. See more about arbitrary-size/precision numbers [[#Advanced numeric types|below]]. ==== Advanced numeric types ==== Both languages offer library-defined [[arbitrary-precision arithmetic]] types for arbitrary-size integers and decimal point calculations. Only Java has a data type for arbitrary precision decimal point calculations. Only C# has a type for working with [[complex number]]s. In both languages, the number of operations that can be performed on the advanced numeric types is limited compared to the built-in [[IEEE 754]] floating point types. For instance, none of the arbitrary-size types support [[square root]] or [[logarithm]]s. C# allows library-defined types to be integrated with existing types and operators by using custom implicit/explicit conversions and operator overloading. See example in section ''[[#Integration of library-defined types|Integration of library-defined types]]'' ==== Characters ==== Both languages feature a native {{mono|char}} (character) datatype as a simple type. Although the {{mono|char}} type can be used with bit-wise operators, this is performed by promoting the {{mono|char}} value to an integer value before the operation. Thus, the result of a bitwise operation is a numeric type, not a character, in both languages. ==== Built-in compound data types ==== Both languages treat [[String (computer science)|strings]] as ([[immutable object|immutable]]) objects of reference type. In both languages, the type contains several methods to manipulate strings, parse, format, etc. In both languages [[regular expression]]s are considered an external feature and are implemented in separate classes. Both languages' libraries define classes for working with dates, times, [[time zone]]s, and [[calendar]]s in different cultures. Java provides {{code|java.util.Date}}, a mutable reference type with millisecond precision, and (since Java 8) the {{code|java.time}} package (including classes such as {{mono|LocalDate}}, {{mono|LocalTime}}, and {{mono|LocalDateTime}} for date-only, time-only, and date-and-time values), a set of immutable reference types with [[nanosecond]] precision.<ref>{{cite web |url=https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html |title=Package java.time (Java Platform SE 8) |publisher=docs.oracle.com |access-date=20 April 2023}}</ref> In contrast, the C# {{code|System.DateTime}} is an immutable struct value type for date-and-time information with 100-nanosecond precision; the .NET 6 API also added {{code|System.DateOnly}} and {{code|System.TimeOnly}}, similar structures for date-only or time-only operations.<ref>{{cite web |url=https://learn.microsoft.com/en-us/dotnet/standard/datetime/how-to-use-dateonly-timeonly |title=How to use the DateOnly and TimeOnly structures |date=12 January 2023 |publisher=learn.microsoft.com |access-date=20 April 2023}}</ref> C# additionally defines a {{code|System.TimeSpan}} type for working with time periods; Java 8 provides the {{code|java.time.Duration}} class for the same purpose. Both languages support date and time arithmetic according to different cultures and time zones.
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)