I've been working with C# and more generally the .Net framework for a couple of years now. I often heard about the similarity between C# & the Java language and would like to learn more about the second one.
Switching from C to C++ can be both easy, as there are many similarities between the two languages, and hard, as there are many differences that require forgetting what you know and habits that you may have developed from programming in C.
C is the root of all programming languages. If you have a good grip on C, then you can go on learning other programming languages like Python, Java, JavaScript. Nowadays, R programming language is also used by many top companies.
A great first step is to simply use C++ as “a better C,” which means that you can program in the C subset of C++ and find the experience better than in C because C++ provides extra type-checking and sometimes extra performance even for plain C code. Of course, C++ also provides much more!
Well, while C# and Java are superficially alike there are a number of small differences that might bite you. Generally I think the opposite direction—going from Java to C#—is less problematic. This is mainly due to C# being a more complex language so you might find many simplifications from common Java patterns but the other way around might be a little painful.
Things to look out for (partial list, not guaranteed to be exhaustive):
Different ...
Naming conventions. In Java only type names start with a capital letter (i. e. PascalCase), everything else uses camelCase. Not very hard to adhere to, though.
Also interfaces generally don't start with I
. On the other hand you have to implement them with a different keyword. Doesn't really help in the middle of the code, though.
Class library :-)
While obvious, this has been the thing I spent most time on when learning a language. When dealing with a known paradigm the syntax differences are quickly sorted out, but getting to know the standard library / class library / framework takes some time in some cases :-)
Exception handling. Java has so-called checked exceptions which means that an exception must either be caught or declared upwards. Usually this means that you have
catch (SomeException ex) { ex.printStackTrace(); }
pretty often in your code1 :-)
int
, float
, char
, &c. and classes such as String
. Doesn't matter much since they implemented auto-boxing, but sometimes it's still annoying to wrap int
in Integer
.virtual
by default whereas c# methods are not.foreach (a in b)
→ for (a : b)
internal
and protected internal
don't exist. But unqualified members are visible to other classes in the same package (sort of internal
, but then again not quite).==
in Java. You have to use .equals()
. While in C# ==
on strings is value equality, in Java ==
is always reference equality.No ...
Foo getFoo()
/void setFoo(Foo foo)
pattern which C# generates silently behind your back when using properties but you have to do it explicitly in Java. Generally, to keep the language itself simpler many things in Java are just conventions. Still, most of the time you're better off adhering to them :-)myList.get(5)
instead of the array-like syntax myList[5]
. Just a mild inconvenience, though.Object
s remain. Also wildcards in generics can be hard to resolve sometimes when the compiler complains about all of the four ?
in your generics having different types. (Though to be fair: That was a case where I would have needed type information at runtime anyway so I reverted back to Object
s).General advice: Grab a friend with Java experience and let him glance over your code. While he probably can't tell you everything you should take care of when you directly ask him that question, he can spot strange things in code just fine and notify you of that. This has greatly helped me learning Java (although I learned Java first and then C#, so it might be different).
1 Yes, I know many catch blocks look different, but still, this is probably the archetypical one and not even that rare.
2Quaere, JaQue, JaQu, Querydsl
3 There's lambdaj, though. Thanks for pointing that out, Esko.
I honestly think the biggest hurdle for many C# developers trying to learn Java is learning a new IDE. Visual Studio is great, and when you're coding in C# for a long period of time, you get used to it. When having to move over to Eclipse or Netbeans, you suddenly feel lost. How do I set a breakpoint? Where's the immediate window? How do I create a windows app? etc etc... I know this sounds crazy, but I'm telling you, people get very attached to their IDE's and have a tough time getting used to new ones...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With