Are you trying to decide between C# and Java? Are you proficient in one and would like to
know how it compares to the other one? Are you trying to decide which one to learn?
Hi, it's Chuck here with another language comparison video! Yeah,
that's right i'm going to go there. Judging from some of the comments on my last comparison video
some viewers apparently feel like i insulted their spouse! Now, i've been designing and implementing
applications for nearly four decades and i like curly-brace languages in particular C++,
Java and C# and i really enjoy coding in all of them. This comparison is simply to help you make
an informed decision. First, there's a basic difference in philosophy. Java architect James
Gosling insisted on inventing a simple language when it came to java back in the mid-early 90s.
Capabilities would be built into class libraries and tools. Java has roughly 50 keywords or
so - relatively small language compared to most other languages. C#, on the other hand has over a
hundred keywords and contextual keywords and many more syntactical features. A driving force behind
c sharp philosophically is to minimize code size and eliminate tedious or boilerplate code. So,
you kind of think of it like C# is like a luxury vehicle with every bell and whistle
no need for any aftermarket or add-ons because everything is already on it java on the other
hand is kind of like a stripped-down jeep or volkswagen beetle with thousands literally
thousands of third-party add-ons and you can configure whatever you want now gosling and oracle
parted ways back in 2010 and since then oracle has added some new bells and whistles to java and kind
of standardized some of the third-party items so that configuration and building applications is a
little bit more straightforward and simpler but it's still a smaller simpler language
with an enormous ecosystem of third-party tools at the same time microsoft with the
introduction of.net core has opened up to encourage more third-party componentry
now let's start with what they have in common they both compile to an intermediate code and execute
within a runtime they both have run anywhere capability they're both strongly typed they're
both object oriented languages with a single base class called object and they're limited to single
inheritance both have value type primitives and primarily reference based as far as the rest of
your variables are concerned they both support generics they both support functional programming
and include lots of metadata in the binaries to support dynamic applications in fact this class
will compile in either language properties in java are supported through a programming convention
of centering getter functions on the other hand c-sharp has a specific syntax for properties so we
could re-code that class for c-sharp specifically creating designated properties now a lot of cases
in properties in our code the properties are trivial simply exposing some backing field
in that case c sharp provides us even a more concise syntax called auto properties now another
subtle but significant difference is that java primitives are not objects if you need to treat
a primitive as an object you have to use a wrapper class to make that primitive appear as a reference
type as an object type for instance like in a generic parameter in c sharp the primitives are
simply aliases for net types and all net types are derived from objects c sharp supports the
definition of both value types and reference types using either struct or class and both
are first class citizens and both can be used as generic parameters c sharp primitives can also
be declared as nullable using language syntax the question mark now we can do the same thing in java
using the class optional or the generic optional so you see what i mean c sharp provides a syntax
for a feature while java accomplishes the feature via some class now speaking of type specifiers
java has three class enum and interface c sharp has several it has class interface
and enum and it also has struct record delegate and an implicit dynamic type
basically a tuple now java supports the concepts of record and tuple but again it's done via
classes value and reference type semantics in java is fixed in other words the primitives are value
types classes are reference types and there's no changing that in c-sharp you can choose whether a
value type is passed by reference or passed by value and in c-sharp you can define using struct
value types and using class reference types now i think one of the most significant differences
is that c sharp supports operator overloading this means that the programmer can use standard
comparison operators to compare any object type that defines those operators this is a common
pitfall for java developers because the equality operator double equal is syntactically legal which
compares the references not the values java developers are reminded to call the dot equals
or dot compare methods both languages support functional programming and lambda expressions java
basically repurposed the single abstract method interface and called that a functional interface
while c sharp has a type definition called a delegate to define a function signature to use
in functional callbacks or functional definitions java also supports events event triggering
and subscription but again just like the properties it does that through naming conventions
c sharp has a dedicated keyword and a dedicated syntax for eventing now there is also a difference
in exception handling java initially promoted the concept of checked exceptions making a programmer
to declare the exception types that might emit from the method however in c sharp there
is no mechanism for that you couldn't do it if you wanted to the trend in c plus and actually even
in java is to move away from checked exceptions in most situations some java frameworks like spring
for instance catch the checked exceptions and rethrow unchecked exceptions checked exceptions
sounded like a good idea at the time but ended up really making code more brittle now there are
subtle differences in how things work under the hood and we don't want to get too specific here
because every version the runtimes change and so we don't want to get into that too deep java's
garbage collection initially was optimized for desktop applications and therefore was lazy there
was no need to clean up memory allocations if the application wasn't going to live very long anyway
while.net utilized what's called a generational garbage collection scheme that's more well suited
for server-based applications oracle did introduce generational garbage collection for java starting
in version 8. all right now generics are also handled differently java compiles generics using
object as the template parameter and enforces the type safety in the application code at compile
time so in reality there's only one instance of any generic class c sharp generates a new
type for each generic variant now this only really matters if your code is using reflection
to determine a type at run time now another big difference between java and c sharp is something
called language integrated query or link this is a feature in c sharp that allows us to create
queries against any kind of data set a collection an in-memory collection or
any ado.net database driver honestly can be used it's accomplished through some features in c-sharp
called extension methods java does not have extension methods now i hate to bring this up
because there are so many variables that it's really hard to measure with any certainty
i promise you any programmer can get the answer they want to get but here we go anyway i just ran
a very simple unscientific cpu intensive loop to calculate pi the c-sharp version
ran in about 400 milliseconds and the java version took just under twice as long about
750 milliseconds or so now don't read too much into that and don't get excited java programmers
we used to have a saying back in the late 90s you know java developers would ask about performance
and i'd say why why all of a sudden you become concerned about performance you're using java
aren't you it's fast enough and the same thing's true with c sharp it's fast enough if you really
want blazing speed go code and c plus plus most applications aren't all that performance critical
in determining performance though if you are really trying to measure that
you have to consider a lot of other factors like network and database speed both platforms handle
threading and multi-core architecture and all that kind of stuff really well from little things like
implicit variable types with the var keyword to bigger things like functional programming
the programmer experience is really pretty similar if the java developer can step into an existing
technology stack then they can start right out of the box just like the net developer there
are some scenarios that favor one over the other let's say that i need to build something that's
going to really easily deploy and run anywhere i would choose java the net core platform was
also run anywhere but it's not quite as easy to do if i need to develop a phone application for
the iphone or for android i would choose c sharp on xamarin or maui it's also a better choice for
game development like under unity otherwise it really doesn't make that much difference
choose based on your existing developer resources all right i'm sure i ruffled some
feathers why don't you give it to me in the comments section make sure you hit the like
button make sure that you tell your friends about this video and subscribe to our channel
that's how we can bring you more great content going forward i'm chuck mccalla thanks