Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Going from C# to Java [closed]

Tags:

java

c#

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.

  • Have you got any specific advice to learn Java when coming from C# ?
  • Any common errors a C# programmer would do when starting Java ?
  • Any documentation showing the habits you can keep and the ones you must change (still in a C# to Java optic, so something a bit more specific then a C# vs Java comparison) ?
like image 288
Jla Avatar asked Jan 21 '10 14:01

Jla


People also ask

Is going from C to C++ easy?

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.

What can we learn after 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.

How can I learn C++ after C?

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!


2 Answers

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 :-)

    • Patterns. Well, not quite, it's still the same stuff. But C# supports some patterns at the language level, while you still have to implement them yourself in Java. No events, but the Observer pattern (very prevalent in Swing—whenever you see a Listener, you know what to do :-))
    • 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 :-)

    • Types. While .NET has normal objects and value types, they both are objects and support methods, properties, &c. Java has a dichotomy of primitive types, such as 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.
    • Polymorphism: All Java methods are virtual by default whereas c# methods are not.
  • Minor syntactic differences.
    • foreach (a in b)for (a : b)
    • Different access keywords. Things like 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).
    • String comparison isn't done with == in Java. You have to use .equals(). While in C# == on strings is value equality, in Java == is always reference equality.
  • No ...

    • Properties. In Java this is generally done with the 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 :-)
    • Operator overloading. Deemed a hazard to the righteous programmer they weren't implemented for fear of abuse. Don't need them too often anyway, not even in C#, but sometimes they are nice and then you're missing something.
    • Indexers. You always have to access list items through myList.get(5) instead of the array-like syntax myList[5]. Just a mild inconvenience, though.
    • LINQ (though there exist implementations2 but it's not as nicely integrated), or lambda functions3 (no delegates anyway, but anonymous classes), extension methods, or partial classes (yes, that's a painful one when dealing with Swing, unless you're very disciplined), and a few more things.
    • Multidimensional arrays. You can use jagged arrays (arrays of arrays), buttrue multidimensionality isn't there.
  • Generics are compile-time only, at runtime only Objects 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 Objects).

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.

like image 121
12 revs, 2 users 98% Avatar answered Oct 09 '22 05:10

12 revs, 2 users 98%


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...

like image 24
BFree Avatar answered Oct 09 '22 03:10

BFree