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!
=== Arrays and collections === [[Array data structure|Arrays]] and [[Collection (abstract data type)|collections]] are concepts featured by both languages. {| class="wikitable" style="width:80%;" |- ! style="width:40%;"| [[Array data type|Arrays]] and [[Collection (abstract data type)|Collections]] !! style="width:30%;"|Java !! style="width:30%;"|C# |- | [[Abstract data type]]s || {{yes}} || {{yes}} |- | One-dimensional, zero-based index arrays || {{yes}} || {{yes}} |- | Multidimensional arrays, rectangular (single array)|| {{no}} || {{yes}} |- | Multidimensional arrays, jagged (arrays of arrays)|| {{yes}} || {{yes}} |- | Non-zero based arrays || {{no}} || {{some}} |- | Unified arrays and collections || {{no}} || {{yes}} |- | [[Associative array|Maps/dictionaries]] || {{yes}} || {{yes}} |- |Sorted dictionaries || {{yes}} || {{yes}}<ref>{{cite web |url=https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.sorteddictionary-2 |title=SortedDictionary<TKey,TValue> Class (System.Collections.Generic) |publisher=learn.microsoft.com |access-date=20 April 2023}}</ref> |- |Sets || {{yes}} || {{yes}} |- |Sorted sets || {{yes}} || {{yes}}<ref>{{cite web |url=https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.sortedset-1 |title=SortedSet<T> Class (System.Collections.Generic) |publisher=learn.microsoft.com |access-date=20 April 2023}}</ref> |- |Lists/vectors || {{yes}} || {{yes}} |- | [[Queue (data structure)|Queues/stacks]] || {{yes}} || {{yes}} |- | [[Priority queue]] || {{yes}} || {{yes}}<ref>{{cite web |url=https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.priorityqueue-2?view=net-8.0 |title=PriorityQueue<TElement,TPriority> Class (System.Collections.Generic) |publisher= learn.microsoft.com |access-date=20 April 2023}}</ref> |- |Bags/multisets || {{no|Third-party library|style=background: #8CF;}} || {{yes}} |- |Concurrency optimized collections || {{yes}} || {{yes}}<ref>{{cite web |title=System.Collections.Concurrent Namespace |url=https://learn.microsoft.com/en-us/dotnet/api/system.collections.concurrent |publisher=learn.microsoft.com |access-date=20 April 2023}}</ref> |} The syntax used to declare and access arrays is identical, except that C# has added syntax for declaring and manipulating multidimensional arrays. {| class="wikitable" style="width:90%;" |- ! style="width:50%;"| Java !! style="width:50%;"| C# |- | Arrays are implicitly direct specializations of {{mono|Object}}. They are not unified with collection types. | Arrays in C# are implicit specializations of the {{code|System.Array}} class that implements several collection [[Interface (object-oriented programming)|interfaces]]. |- | Arrays and collections are completely separate with no unification. Arrays cannot be passed where sequences or collections are expected (though they can be wrapped using {{code|Arrays.asList}}). | Arrays can be passed where sequences ({{mono|IEnumerable}}s) or collections/list [[Interface (object-oriented programming)|interfaces]] are expected. However, the collection operations that alter the number of elements (insert/add/remove) will throw exceptions as these operations are unsupported by arrays. |- | The {{mono|for}} statement accepts either arrays or {{mono|Iterable}}s. All collections implement {{mono|Iterable}}. This means that the same short syntax can be used in for-loops. | The {{mono|foreach}} statement iterates through a sequence using a specific implementation of the {{mono|GetEnumerator}} method, usually implemented through the {{mono|IEnumerable}} or {{code|IEnumerable<T>}} [[Interface (object-oriented programming)|interface]].<ref>{{cite web |url=https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/foreach-in |title=foreach, in (C# reference) |year=2018 |publisher=Microsoft |archive-url=https://web.archive.org/web/20190112220056/https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/foreach-in |archive-date=12 January 2019 |quote=The foreach statement executes a statement or a block of statements for each element in an instance of the type that implements the {{mono|System.Collections.IEnumerable}} or {{mono|System.Collections.Generic.IEnumerable<T>}} interface. |access-date=26 January 2019 |url-status=live}}</ref> Because arrays always implicitly implement these [[Interface (object-oriented programming)|interfaces]], the loop will iterate through arrays also. |- |colspan=2|In both languages arrays of reference types are covariant. This means that a {{code|String[]}} array is assignable to variables of {{code|Object[]}}, as {{mono|String}} is a specialization of (assignable to) {{mono|Object}}. In both languages, the arrays will perform a type check when inserting new values, because type safety would otherwise be compromised. This is in contrast to how generic collections have been implemented in both languages. |- | No [[Two-dimensional array|multidimensional arrays]] (rectangular arrays), but arrays of references to arrays ([[jagged array]]s). | [[Two-dimensional array|Multidimensional arrays]] (rectangular arrays), and arrays of references to arrays ([[jagged array]]s). |- | Arrays cannot be resized (though use of the {{code|System.arraycopy()}} method can allow for multi-step array resizing) | Arrays can be resized while preserving existing values using the {{code|Array.Resize()}} static array method (but this may return a new array). |- | Implemented as a [[retrofit]] for the {{code|java.util}} library having extra features, like data structures like sets and linked sets, and has several algorithms to manipulate elements of a collection, like finding the largest element based on some {{code|Comparator<T>}} object, finding the smallest element, finding sublists within a list, reverse the contents of a list, shuffle the contents of a list, create immutable versions of a collection, performs sorts, and make binary searches.<ref>{{cite web |url=http://www.25hoursaday.com/ |title=A Comparison of Microsoft's C# Programming Language to Sun Microsystems' Java Programming Language: C. An Ever So Slight Feeling of Dèjà Vu: 6. Collections |author=Dare Obasanjo |year=2007 |publisher=Dare Obasanjo |archive-url=https://web.archive.org/web/20120919093308/http://25hoursaday.com/ |archive-date=19 September 2012 |quote=The Java collections framework not only has methods that enable one to access unsafe collections in a thread safe manner, but contains thread-safe versions of most of the data structures as well. The Java collections framework has a number of algorithms for manipulating the elements within the data structures including algorithms that can do the following; find the largest element based on some Comparator, find the smallest element, find sublists within a list, reverse the contents of a list, shuffle the contents of a list, creates immutable versions of a collection, performs sorts, and binary searches. |access-date=10 September 2012 |url-status=dead |df=dmy-all}}</ref> | The C# collections framework consists of classes from the {{code|System.Collections}} and the {{code|System.Collections.Generic}} namespaces with several useful [[Interface (object-oriented programming)|interfaces]], abstract classes, and data structures.<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: C. An Ever So Slight Feeling of Dèjà Vu: 6. Collections |author=Dare Obasanjo |publisher=Dare Obasanjo |archive-url=https://archive.today/20130102015335/http://www.25hoursaday.com/CsharpVsJava.html |archive-date=2 January 2013 |date=March 2007 |quote=The C# collections framework consists of the classes in the System. Collections and the System.Collections.Generic namespaces. The {{mono|Systems.Collections}} namespace contains [[Interface (object-oriented programming)|interfaces]] and abstract classes that represent abstract data types such as {{mono|IList, IEnumerable, IDictionary, ICollection}}, and {{mono|CollectionBase}} that enable developers to manipulate data structures independently of how they are actually implemented as long as the data structures inherit from the abstract data types. The System.Collections namespace also contains some concrete implementations of data structures such as {{mono|ArrayList, Stack, Queue, HashTable}} and {{mono|SortedList}}. All four of the concrete data structure implementations enable one to obtain synchronized wrappers to the collection that allows for access in a thread-safe manner. The {{mono|System.Collections.Generic}} namespace has generic implementations of the key data structures in the System.Collections namespace including generic {{mono|List<T>}}, {{mono|Stack<T>}}, {{mono|Queue<T>}}, {{mono|Dictionary<K,T>}} and {{mono|SortedDictionary<K,T>}} classes. |access-date=10 September 2012 |url-status=dead }}</ref> NET 3.5 added {{code|System.Linq}} namespace that contains various extension methods for querying collections, such as {{mono|Aggregate}}, {{mono|All}}, {{mono|Average}}, {{mono|Distinct}}, {{mono|Join}}, {{mono|Union}} and many others. Queries using these methods are called [[Language Integrated Query]] (LINQ). |} Multidimensional arrays can in some cases increase performance because of increased [[Memory locality|locality]] (as there is one pointer dereference instead of one for every dimension of the array, as it is the case for jagged arrays). However, since all array element access in a multidimensional array requires multiplication/shift between the two or more dimensions, this is an advantage only in very random access scenarios. Another difference is that the entire multidimensional array can be allocated with a single application of operator {{mono|new}}, while jagged arrays require loops and allocations for every dimension. However, Java provides a syntactic construct for allocating a jagged array with regular lengths; the loops and multiple allocations are then performed by the virtual machine and need not be explicit at the source level. Both languages feature an extensive set of collection types that includes various ordered and unordered types of lists, maps/dictionaries, sets, etc.
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)