r/learnjava • • Jun 21 '21

Why learn Java when there's C#?

I am genuinely curious, as it seems C# has superseded Java in many regards. The only reason I can think of using Java is backward compatibility.

EDIT: To clarify, the main selling point of Java was cross-platform, but nowadays most frameworks and toolsets are to some degree cross-platform, including .NET Core, which is the new main framework of C#.

0 Upvotes

36 comments sorted by

View all comments

6

u/[deleted] Jun 21 '21

as it seems C# superseded Java in many regards

Why do you say that?

-2

u/kres0345 Jun 21 '21

this is the first thing I could find, https://softwareengineering.stackexchange.com/a/125718/290637

I am aware of that it only addresses the things C# beats Java in.

  • Instead of a lot of noise (EJB, private static class implementations, etc) you get elegant and friendly native constructs such as Properties and Events.

  • I am aware that it only addresses the things C# beats Java in.generics), and you can perform reflection on them.

  • It supports native resource-management idioms (the using statement). Java 7 is also going to support this, but C# has had it for a way longer time.

  • It doesn't have checked exceptions :) (debatable whether this is good or bad)

  • It's deeply integrated with Windows, if that's what you want.

  • It has Lambdas and LINQ, therefore supporting a small amount of functional programming.

  • It allows for both generic covariance and contravariance explicitly.

  • It has dynamic variables, if you want them.

  • Better enumeration support, with the yield statement.

  • It allows you to define new value (or non-reference) types.

I've only used Java for app development since I like Android Studio more than Xamarin, but in the time I've used Java I keep finding myself looking for features that are present in C# but aren't in Java, like properties (much nicer than manual getter and setter methods), enums, the simple syntax of LINQ and generics.

6

u/sweetno Jun 21 '21

Usually it comes down to the point "Deeply integrated with Windows" which doesn't play well with all these Linux servers everywhere.

1

u/kres0345 Jun 21 '21

Yea, that's the cross-platform part I mentioned in the edit of my OP. As another redditor mentioned, the post is apparently old. C# isn't deeply "integrated with windows", the original DotNet Framework has got it's last version update and is already a couple major releases behind, the new main framework is .NET Core a cross-platform alternative

1

u/[deleted] Jun 21 '21

[removed] — view removed comment

0

u/kres0345 Jun 21 '21

Well that's because the multiple codebases are built-into the language and hidden from the programmer. I'm sure you can find libraries that present cross-platform interfaces for doing things

3

u/DuncanIdahos9thGhola Jun 21 '21

Well that's because the multiple codebases are built-into the language and hidden from the programmer.

WTF does that even mean?

I'm sure you can find libraries that present cross-platform interfaces for doing things

Not really. There's not many libraries out there compared to Java out there.

0

u/kres0345 Jun 22 '21

"WTF does that even mean?"

Well, it isn't magic that communicates with the different platforms, at some level it would have to bifurcate into different "codebases" addressing the specifics of the system.

I would like to emphasize that I already don't like the parent commenters use of the word "codebase", I think it implies there's a need to write the entire codebase twice with minor differences, this is very naïve. Having an if-statement check the platform and run a line of code instead of another according to the platform is hardly called a different "codebase".

What I meant is that this is hidden away in the standard library of Java, while in C# you'd just such a library yourself to address different platform needs.

5

u/desrtfx Jun 21 '21

enums

Java's enums are superior to C#'s implementation by lengths.

Java has had generics for quite some time already.

Java has had streams and lambdas since version 8 - we are currently at 16 with 17 round the corner.

You cannot talk Java if your sole experience is Android and your sole backing is an article that is talking close to 10 versions behind current.

Android is a subset of a fairly old Java framework.

-1

u/kres0345 Jun 21 '21

Can you elaborate on enums.

C# has also evolved over the years, it's not like C# has remained static while Java has evolved.

Making an app isn't my "sole" experience, just the most recent. I've made minecraft mods and plugins as well. And I've also dabbled in Java before I became acquainted with C#

2

u/desrtfx Jun 21 '21

Java enums are full fledged final classes with fields, with methods.

Please, google "Oracle Java Enum tutorial" and see for yourself. I'm on mobile and linking from here is difficult.

Sure, C# has evolved over time, but so and to an even greater degree did Java.

Again, I stick to what I said before: you don't have real Java experience and you never have properly learnt Java and therefore are not qualified and informed enoughtfor the statements/claims you make.

LINQ is on one hand highly praised but even more cursed in professional development since it is fairly slow and a huge memory hog.

0

u/kres0345 Jun 21 '21

I just read this page https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

Honestly, I still prefer the elegant C# way. In C# an enum is simple, it's just an identifier optionally paired with a primitive-type. If I were to implement the planet example, I would probably just use objects with inheritance to make it even more extensible, though I get that it's unlikely for another planet to appear within our solar system. That way the system would be open for extension and closed for modification (in case I suddenly need to specify a new parameter for a planet, like day duration).

Another way would be having a static helper class of methods taking an enums as input and outputting a wide range of information of the given planet.

Also, as I stated originally I don't know much about Java (that's why I made the post).

3

u/[deleted] Jun 21 '21

I think you’ve missed the point with Enums. C# enums have their place, especially with the ability to make them flag fields.

But the Java enums allow you to have effectively a finite list, of immutable classes of static data. They can be very useful.

C# enums are much closer to C style enums, where as Java enums are more abstract.

Neither is necessary stater, just different.