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

Show parent comments

1

u/teacher_cs Jun 22 '21

C# has "arrays of structs", whereas Java has only "arrays of objects". The former is more cache-friendly, which makes a huge performance difference on modern processors.

1

u/[deleted] Jun 22 '21

Java has array of primitives types, which are a block of primitives.

Whilst the value types are missing for Java. The fact is an array of objects might actually have good cache locality if they happen to be adjacent in memory.

The problem with both C#&Java is that performance is dynamic and hard to reason about.

It’s not enough to discount Java, but I do wish that value types would be in both languages.

1

u/teacher_cs Jun 22 '21

Java has array of primitives types, which are a block of primitives.

You can fall back on that, but it's usually not elegant.

Whilst the value types are missing for Java. The fact is an array of objects might actually have good cache locality if they happen to be adjacent in memory.

You need a separate memory allocation for each object, so the objects will generally not be adjacently located.

The problem with both C#&Java is that performance is dynamic and hard to reason about.

When I have the time and the mood - I'm neither a C# nor a Java programmer - I'll make some test programs.

It’s not enough to discount Java, but I do wish that value types would be in both languages.

It was not my intention to discount Java, but it seems to me at least as important an argument as all the others that have been mentioned.

1

u/[deleted] Jun 22 '21

You need a separate memory allocation for each object, so the objects will generally not be adjacently located.

If you're allocating everything in a loop, then yes they will be adjacently located (and in the case of somthing like G1GC, might not even be moved). Plus there's nothing stopping a GC moving everything to be co-located.

If you're the allocations are actually spread-out over time, then in C# you'll be paying an object copy, which depending on the size of the struct would range from negligible to significant.

tbh, having value types is a strength of C#, but I don't think we'd need to really go into each feature. Plus this question is a bit of a troll anyway, they always pop up every so often.