What are the most relevant differences between C# and Java?

Asked

Viewed 28,542 times

37

I wanted to know the most relevant differences that I should know exist between these two languages. Why am I asking this here? Because normally in my day-to-day when I ask this question, my colleagues always demonstrate a "favoritism" towards one of these languages, although that was not my question to them.

What I do know is that they are similar languages. C# attracts me because of its IDE, Visual Studio, and Java in turn attracts me because of its proximity to Android and being the longest in the market.

NOTE: Preferably responses from people who worked with two languages.

  • 6

    Discussion at the goal: http://meta.pt.stackoverflow.com/q/4801/101

  • With the growth of C# accompanied by the innovations of . NET Core, ASP.NET CORE, Blazor, Razor and the like. No doubt my personal choice is C#.

1 answer

59


In fact who works with only one will be biased towards her.

They really are very similar, but the culture around them is very different. For example, Java tries to be more explicit, C# tries to be more concise. But it’s very common for some people to preach the Java culture in C# (it’s amazing how they get to talk in C# culture when they’re actually talking about common Java practices that have been imported by some people who don’t understand the difference).

Java has a more publicly active community. For better or for worse. In some markets there are more openings, which does not mean that there are more positions. After all, in certain cultures it is more common to change jobs than in others. Nothing very different, but Java is a little more comprehensive in some things, but more dispersed too, it is very common to duplicate efforts, competition of tools (which can be good or bad) and different ways of doing the same thing. This has changed a little.

License and ownership

Microsoft does not own C#, it only created the language, the property is the .NET Foundation, so it’s not in the public domain, but it’s also not in a company, it’s almost in the public domain, it’s even better because there’s a responsible one (the public domain has bad legal implications).

Oracle owns Java, has a history of processing who adopts its technologies, either by Sun with Java or by Oracle itself. Currently Google is in trouble because it lost a process that it can no longer use Java without paying fortunes and will still have to pay for the time it has used. I hope that this reverts in the second instance because it changes everything in the sector and will harm everyone, but we have already seen that the product is not in good hands. It is no use to open source if the technology as a whole is proprietary and whoever adopts it can be processed.

Paradigm

Java cares more about object orientation although it’s essentially imperative and now it’s going a little bit to functional.

C# more clearly marks its multi-paradigmatism, although declaring (or declaring) object-oriented, it has very strong procedural imperative and violates object orientation when it is pragmatic. Although it is not an essentially functional language it has parts of this very strong paradigm and the evolution of language refers to be increasingly functional. It also adheres well to the orientation to contracts, events, patterns, it is a little easier to be oriented to aspects, it is a little more declarative, in short, it embraces more styles. And she really appreciates the performance.

Ecosystem

Besides the language that has specificities that please one person more than another, the ecosystem makes a difference, no doubt. I’m not gonna get in too deep.

It’s not just C# that has good IDE, Java as well. Many like C Eclipse and has in it, or in plugins for him, things that I’d like to see on VS. I find the VS a bit stripped and inferior, but when together with Resharper and others plugins, he really looks great. The "full" VS is free. Not all good plugins are. Have other very good options.

It is common to have more tools and libraries in Java than in C#, especially free and open source. But this is changing little by little.

Not always having several means good. It can mean that no one meets the needs well. Having many choices can be distressing, or be the ideal solution. For example, Java has "trocentos" frameworks for web. Of course few are well used and recommended. C# currently only recommends ASP.NET MVC (in Core version) and any other is considered legacy.

Almost everything that is admittedly good in Java someone makes a port for C# or something inspired by what exists for Java. It is rare for the programmer to be on hand. The contrary is also true, but to a lesser degree, for good or for evil.

There are libraries, standard or not, for Java that are better, and the same can be said for C#. I would not know to weigh what comes out winning here, and I think more secondary. It has solutions for everything in both.

Platforms

Another point is that C# rotates in all 32 mobile platforms mainstream. Java only runs in one, and better, of course - but it’s not quite pure Java and the most modern. There are attempts to change this, so this deficiency will be cured, if it has not already been, but I do not hear about Java on iOS, even if there is a solution people do not seem to use. Outside Swift and Kotlin it seems that Java is still used a little on one of the platforms and C# has become the default language when you want both platforms natively.

On desktop platforms, C# now runs with the same quality in all. Java is still annoying to Windows (it may have improved at some points). It is true that C# does not go well in cross-platform GUI - it has options but is not ideal. But it is very questionable how successfully Java does this. Both have confusing and legacy GUI options.

Evolution

C# started copying a lot of Java, that was a joke. Today it has been quite the opposite.

C# opened the code more freely and completely before Java. The biased do not recognize this because one point they considered strong was the freedom that Java had, which was never that whole truth.

C# started earlier to improve modularization and decrease dependency on standard CLR. JVM has proposals for this (I think it now has option like this).

In fact, several Java improvements occurred when C# appeared showing that it came with several more modern things, from language constructs to performance improvements. The best thing that happened to Java was the emergence of C#.

C# is getting better with each version, Java is chasing. In some cases it is even doing better than C#. In general, those who do it later do it better because they have learned from the mistake of being pioneers. There are cases that can no longer be solved without making the code incompatible, there are mistakes that can not be fixed in the beginning. C# already suffers from it, too. I think C# was supposed to be better than it is. It’s recognized that they had the opportunity to fix more Java errors than they did, surely many functional features would be better if the language had waited a couple of years to be released.

Today both are very mature.

Item by item

I would add that in most cases the choice will be made either by pleasure or by some political and non-technical aspect. Both have defects and qualities, both solve well any type of problem that this type of language can solve, after all they are of the same paradigm. Both run essentially in the same places, with few differences. For web both are very good, one with several frameworks and others with less, but better stabilised.

I did a review considering Java 14 and C# 9 (some are speculations).

Ambience

  • C# has preached use in a unique environment, it seems that Java has sought this path as well. Before both had several ways to execute their codes and although having some advantages was very confusing. Java has many suppliers and this can be complicated. Java running in certain environments is not the same as Java running in others, that is, they have promised something they cannot accomplish.

  • Java has a slightly better developed environment, the Jitter and the garbage collector and other mechanisms are more advanced. By language deficiencies they need to be to not lag behind. C# has improved, but slower because it is not so necessary. Java has more alternative options.

  • It is difficult to say which is faster, it depends on each case, how the tests are done. It is possible to prove specific cases, but not in general. What may be true in one version may not be true in another. But in each version C# has gotten better and it is increasingly rare for Java to have some advantage.

  • The Java memory consumption is brutally higher. I won’t go into details, fit specific questions, already have something on the site.

  • C# has much more metadata that Java.

  • Both have strong reflection, but the way of using is quite different.

  • To serialization is done very differently (this has been changing and will change more, C# will have a fantastic shape).

  • The environmental safety system is more complete in C# (in . NET Framework).

Typing

  • In C# everything derives from Object, in Java the primitive not.

  • Java still does not allow you to create your own types by value (structs). Java only has the few primitive types ready. They say you’ll get (for years, never deliver), I do not know how much can integrate well to the current type system.

  • So there are no reference types that are allocated to the stack.

  • Java does not allow creating anonymous types.

  • C# prefers the types to be by value whenever it makes sense, like the DateTime. Java puts almost everything as a reference, which causes greater memory consumption, processing and pressure in GC.

  • Java has no numerical types without signal.

  • Java cannot turn on and off numerical burst check in the language.

  • Java can ensure that the behavior of floating point is the same on all platforms (there are negative consequences).

  • Java only has the type decimal in the library and only in long form (occupies more memory). There are some other types that do not exist in the language.

  • C# has arrays rectangular (multidimensional).

  • C# has no facilities to do substring taking advantage of the string existing. This causes memory leakage. Java abandoned the practice, but the cost of having the facility remained. C# now has more correct ease than Java tried to adopt.

  • Java does not have purely static classes, but can access static members directly through the instance while C# only through the class.

  • The enums of Java are more object-oriented and most heavily typed. Although give to simulate them in the C#, is bad and what the language actually uses is worse than Java.

  • Java cannot pass "primitive" types by reference. You have to box to do this.

  • Java does not have cancellable types, can simulate, but it’s not convenient and the library doesn’t deal with it so it wouldn’t have much value.

  • Java does not have partial types and methods.

  • Java does not allow more than one public class per file.

  • Java has no way of having optional dynamic types that shall be verified only at runtime. This helps with interoperability with other languages and COM, for example.

  • C# has real tuples with named members allowing the return of multiple results in methods, deconstruction and other uses.

  • C# has Pattern matching well powerful and already advanced. Java is preparing this.

  • C# and Java are deploying Records.

Object orientation

  • Java has the virtual methods by default. C# chose not to have to facilitate performance and encapsulation. It could have gone further and left the class sealed by default also, missed this opportunity. Java will soon have optionally sealed classes with choice of what it can inherit, which is quite interesting.

  • In Java the override is implicit (there is syntax to help compiler) whereas in C# it needs to be explicit.

  • There are differences in how the Hiding method both. C# has the new to make that clear.

  • Java does not call finishers inherited.

  • Java does not allow creating (overloading) operators for types. With this gets overloaded :P the syntax of the code. C# did it better than C++. The wrong use of the resource in C++ made Java not have it. But engineers must find a solution, not abandon the project. It includes cast implicit and explicit.

  • Java does not have estates in language, conventions are used.

  • With that Java does not have indexers which is an overloaded operator that functions as a property.

  • C# does not have internal classes which is what Java uses to simulate the lack of delegates. Much less has anonymous internal classes, there is even nesting classes, but this is done in a static way.

  • Java does not have extension methods.

  • C# cannot declare final static fields on the interface, but it is planned.

  • C# does not have methods default within interfaces. You have to create the interface and the extension method. Now you have.

  • C# allows implement a method specifically for an interface.

  • There is difference in the initialization order of objects and types.

  • Java does not have initiators of objects and collections.

Methods

Exceptions

  • C# does not have checked exceptions. Java is essentially the only language that implements this. Even among the Javeiros there is no consensus if it is good to have them.

  • Java does not have filter exceptions.

  • Java tends to use the exception for normal flow control, C# avoids this, even because its mechanism, when it generates exception, is slower, some people import this culture from Java, but this is wrong.

  • In Java the exception can only be one statement as opposed to C# which can be an expression.

Functional

  • The use of Amble Java is much more limited to C#. They improved the syntax, but not semantics. That’s why C# exists, but I won’t talk about the fight between Sun and Microsoft here.

  • Java does not have events. You have to implement the Observer standard in hand, which can give you more flexibility. Obviously you can do the same in C#, if you need.

  • Java does not have expression trees.

  • Java does not have LINQ. Now there’s something that helps a little (Java Streams), but far from the total ease that LINQ offers. I’ve seen some pretty weird things in Java.

  • Java does not have generators (iterators).

  • Java has no form of type inference.

Modules

  • The whole operation of modules of both is very different. Java has packets, C# has namespaces and assemblies. The first is to separate types into groups of names, from families. The other works similarly to the java package, but does not have a semantic nomenclature in the language. The organization is more flexible and does not depend on how it is deployed.

  • With this the visibility of C# can be restricted in a more granular way.

  • In C# os assemblies can be friends with others and allow extra access.

  • Java cannot rename the imported package (alias) for ease of use.

Operators

  • Java has no operators to help with nullity (?? and ?.).

  • Java does not have the operator nameof.

  • C# has two types of cast explicit.

Extra facilities

  • Java does not have conditional processing directives (#if). C# does not have a preprocessor, is integrated into the language and is limited to what can be done in C/C++.

  • C# has more facilities to work with asynchronicity, including in language.

  • Java may have synchronized methods by language, C# can only have blocks (though it may be the whole method) or with the use of libraries or annotations. It seems to be only a syntactic difference.

  • The genericity of C# does not use type Erasure, which is a great help for performance and general use. There is a genericity semantics, while in Java it is just a compiler trick. It seems to me that the restriction of variance occurs in the right place.

  • C# allows more uses, variety of types and restrictions in generic types.

  • Java can not do interpolation of string. Neither has Verbatim strings (looks like you’ll have).

  • Java does not try to simplify the syntax of many things, especially of methods of a line that in C# can use the syntax of lambda, even though it’s not.

  • Java does not support goto, despite having it as a keyword.

  • C# can be compiled at runtime, has a scripting and a REPL. The .NET Compiler Platform is an advance for language, library and to facilitate the increase of the entire ecosystem of tools. Java allows something in this sense much more limited.

  • Java does not allow code unsafe what hinders interoperability with other languages and performance in some situations. Java has JNI for interoperability. It is rare that this is a good solution. Some find it bad to allow unsafe code in the language and that this makes the language less secure, but it is far from true, one needs to better understand the resources to criticize. I never actually wore the unsafe, But I love knowing he’s there when I need him. Java has a project to improve this, but it will not have to be as good by the limitation of the current language.

  • So Java cannot use pointers, nor managed in specific areas, as they are in C# in point cases where they are most suitable. And C# has introduced increasingly better mechanisms where it uses the pointer advantage without having the disadvantage.

  • Java has soft/phantom Ferences.

  • Java does not optimize Tail recursion. C# makes in x64.

  • C# allows representation of binary literals and use of separators for better readability.

  • C# fears the switch with expression, Java will have.

  • C# has an asynchronicity mechanism that is reference in the market, java promises that it will have some mechanism that will serve the same purpose.

  • Java seems to be one step ahead in the use of NVM. This can be bad because it is not yet known how best to use this new technology.

Completion

I’m not gonna waste my time with small syntactical differences. Nor will I go into detail when one implements something a little better than the other, but without much relevance. Entering the field of opinion, I find the Java style guide worse than C#.

Everything you can do in one language you can do in the other, and it’s not so different between them, but the many small differences end up being important in the end.

You may think I was biased by showing a lot of stuff that C# has and Java doesn’t, but that’s the way it is. Java is a simpler language, it has its merit, but it is simple to learn, not so much to use.

I often see people say "how nice that my favorite language nay has such a resource, "only they love it when the language puts it on afterwards, and they think it’s even better than the other. Others get stubborn, this kind of attitude is not good for professionals, no one should fall in love with technologies.

Jon Skeet is admittedly very good in both languages and works with both. If he could choose which one to use fully, clearly it would be C#. My answer was very much based on his answer and others in the OS.

Neal Gafter has been very involved in the creation and evolution of Java for more than 10 years and is now almost matching that time involved with the evolution of C#, and I think he prefers the C#.

More references here and here. In the Wikipedia.

I accept suggestions/edits. I may have forgotten something.

  • 3

    Very enlightening. Helped me guide my studies. In college I’m learning C#, but my intention is Java. However, with this information it was felt that not both languages do not have so much antagonism. And as said at the end, it is important.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.