9

I may have to switch to Java for new project. I have very little knowledge about Java, because I've mainly studied and used C#, and I'm afraid of the differences between these two language/platform should likely to cause me many problems.

Which are the pitfalls/gotchas I should care about?

Michael K
  • 15,659
Deltax76
  • 1,041

10 Answers10

36

Here are some important Java gotchas when coming from C#:

See also

Related questions

On some topics listed above:

On general Java gotchas:

13

One obvious pitfall is comparing strings with C# style string1 == string2 (Java compares only references) instead of Java style string1.equals(string2).

Another one is that private is the default access modifier in C#, package in Java.

Also ToString() methods are not automatically localized by current culture in Java.

12

The one that got me was Java substring args are beginIndex, endIndex while C# Subtring args are startIndex, length. Thats enough of a difference to make it annoying and a good probability of getting index out of bounds either way you switch.

krock
  • 221
10
  • You get no LINQ
  • You get no good - looking UI (no WPF)
  • No properties
  • You get dancing Egyptians
  • You get APIs without examples and good documentation

Hm.

10
  • Java enums are way more powerful/complicated, they are in fact real classes instead of named integers.
  • inner classes in java are more powerful (and they behave different)
  • no delegates, only functional objects
  • the constructor chaining thingy has a completely different syntax in both languages, i tend to fail every time i have to do that in c#
  • Java has extends for subclassing and implements for interfaces, which is quite nice. C# instead relays on a naming convention which says that interfaces begin with an uppercase I in their name. I dont like that convention, since i can never be sure if someone else fails.
  • java autoboxing can bite you in the a**
  • java type erasure does really make things more complicated
6

The biggest meta-pitfall is to assume that the Java language and libraries behave the same as similar-looking stuff in C#. Do the tutorials, read the javadocs, don't assume ...

Another meta-pitfall is to assume that the fact that you can do something in Java equally as easily/nicely as you can/could in C#. It is not true. Java is a much older language, and mistakes were made ...

And the last meta-pitfall is to think that complaining about stuff being missing / different in Java on SO will get you universally sympathetic / supportive responses!

Stephen C
  • 25,388
  • 6
  • 66
  • 89
3

Beware the differences in default access modifiers. Also note that all non-static methods in Java are virtual (unless you mark them as final).

Although it's somewhat out of date, I've found this to be a great reference.

Comparison of C# and Java, by Dare Obasanjo

user5631
  • 591
2

depends on what kind of program you are working on. Wikipedia has this article and it's quite extensive. (also check out the "external links" section at the end) http://en.wikipedia.org/wiki/Comparison_of_Java_and_C_Sharp.

Also, I read this article when I switched from C# to Java http://www.25hoursaday.com/CsharpVsJava.html and it was very useful.

Louis Rhys
  • 6,182
0

I think your question is subjective. All can't be explained here. I suggest you to read Java Puzzlers, By Joshua Bloch and Neal Gafter. You can learn more and be safe from pitfalls.

-1

In Java language the objective equivalents of primitive types such as int, char, are not "value types" (e.g. Integer is a reference type). In C# the System.Int32 is a structure.