C# developers learning Java, what are the biggest differences one may overlook?

By | July 6, 2019

Question

For c# developers that are staring out to learn Java, are there any big underlying differences between the two languages that should be pointed out?

Maybe some people may assume things to be the same, but there are some import aspects that shouldn’t be overlooked? (or you can really screw up!)

Maybe in terms of OOP constructs, the way GC works, references, deployment related, etc.

Solution

A few gotchas off the top of my head:

  • Java doesn’t have custom value types (structs) so don’t bother looking for them
  • Java enums are very different to the “named numbers” approach of C#; they’re more OO. They can be used to great effect, if you’re careful.
  • byte is signed in Java (unfortunately)
  • In C#, instance variable initializers run before the base class constructor does; in Java they run after it does (i.e. just before the constructor body in “this” class)
  • In C# methods are sealed by default. In Java they’re virtual by default.
  • The default access modifier in C# is always “the most restrictive access available in the current context”; in Java it’s “package” access. (It’s worth reading up on the particular access modifiers in Java.)
  • Nested types in Java and C# work somewhat differently; in particular they have different access restrictions, and unless you declare the nested type to be static it will have an implicit reference to an instance of the containing class.

Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *