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,539
  • 9
  • 61
  • 93
Delta76
  • 1,041
  • 2
  • 8
  • 19
  • 3
    There are *many* differences between C# and Java and every single one is a potential "answer" to this question. However, I doubt that would be very useful to you or others. Asking a more specific, real question would yield more useful answers. Alternatively, try asking for *references or guides* for switching from C# to Java rather than the (effectively endless) differences. –  Jul 15 '10 at 09:42
  • In other words, try asking a ["why" or "how" question](http://meta.stackexchange.com/questions/57226/should-we-have-a-list-of-x-close-reason) about a specific problem instead. For example, asking for references, guides, or books is like asking "how can I switch from C# to Java", or asking about specific code you don't understand is a "why does this do X instead of Y" question. –  Jul 15 '10 at 10:12
  • Consider making this community-wiki – finnw Jul 15 '10 at 10:18
  • See also http://stackoverflow.com/questions/169815/java-common-gotchas – finnw Jul 15 '10 at 10:19
  • @finnw: edited as cw :) – Delta76 Jul 15 '10 at 10:21
  • I think [this is a great article that summarizes the differences.](http://www.harding.edu/fmccown/java_csharp_comparison.html) –  Jul 15 '10 at 10:20
  • I think this blog covers a lot of things that you are looking for.. http://www.ericsink.com/entries/java_eclipse_2.html – Hari Menon Jul 15 '10 at 07:44
  • To get to understand the main differences between C# and Java; this article is written in brief to cover it all http://www.codeproject.com/Articles/22854/Main-Differences-between-C-and-Java. If you want to test and analyze the C# code in the real time after being migrated into Java language then use [CodePorting C#2Java App](http://codeporting.com/apps/csharp-2-java). It is cloud based app for converting C# applications and source code into Java. –  Apr 09 '12 at 23:03

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.

Cloudanger
  • 231
  • 2
  • 6
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
  • 1
  • 5
  • 3
    +1 More confusing is the fact, that it's beginIndex INCLUSIVE and endIndex EXCLUSIVE... and that there are some API's found in the JDK which use the startIndex, length approach... – Oliver Weiler Jul 15 '10 at 07:53
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.

  • 2
    Never found the Java API bad (in fact easier to navigate) but there are definitely less examples. What's this about Egyptians? – Graphain Jul 15 '10 at 07:39
  • 3
    I'm upvoting this...... even if is a sarcastic low blow. – mpen Jul 15 '10 at 07:40
  • 3
    @Graphain see http://stackoverflow.com/questions/2349378/new-programming-jargon-you-coined/2801919#2801919 –  Jul 15 '10 at 07:42
  • @Mark: I have to agree with you, including the upvote. I don't know what the issue with apidocs is though, I mean I understand that lack of examples makes things annoying at times but that's mostly because the API writer was lazy. –  Jul 15 '10 at 07:46
  • 8
    Are these true pitfalls? I understand pitfalls as possible causes of bugs. These are just things you have to live with as you can't do any other way. –  Jul 15 '10 at 07:59
  • 2
    @cloudanger: agree with you. Pitfalls should be somethings that "work" wrongly, not somethings that does not even work. – Delta76 Jul 15 '10 at 08:01
  • True, but I think that a language without functional data access methodology (LINQ), a good UI framework (WPF) and properties is simply not state of the art anymore, it's like having only one leg and one arm. I mean, come on... while in C# one can declaratively convert XML elements into objects (Select - method of LINQ, even things like "ToDictionary", etc...!), the unbelievable amount of work one has to put into this in Java (iterating, doing every little step myself) makes baby FSM cry. And don't get me started about what Java developers seem to think is an acceptable GUI... –  Jul 15 '10 at 08:34
  • 2
    wpf doesnt make a nice looking gui by itself, nor does swing. But wpf has serious problems and there is so much awful wpf stuff out there. if you stick to UX conventions and let graphics guis do the work, wpf can be nice. if programmers do the work, it usually sucks (but at least something flashes and flies around). Its not about the api, its about what you can do with it. – atamanroman Jul 15 '10 at 09:42
  • Yes, it's about the API and what you can do with it. There is nothing even remotely comparable to it in Java. And someone who doesn't strictly adhere to MVVM when doing WPF is not a real developer anyway. I tried a lot of this Java - stuff and I didn't like ANYTHING about it, there was not a single thing about it that I thought was made well. Quite the opposite - trifle tasks in C# are complicated and a pain to do in Java, everything feels so open sourceish, so clumsy. And the language, as I stated before, isn't state of the art anymore with the lack of delegates, LINQ and all that... –  Jul 15 '10 at 09:53
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
atamanroman
  • 471
  • 3
  • 13
  • 2
    You are joking, right? -1, however. You can't be serious. –  Jul 15 '10 at 09:17
  • 3
    you should at least make clear which point you dont like, otherwise i have to assume that you are just trolling. – atamanroman Jul 15 '10 at 09:21
  • * taking absence of linq easy * seeing wpf as just another gui framework * api-doc * the ide question * considering egghead - books serious * comparing javadoc to the far superior sandcastle * collections and math are inferior to .net (by far!) * platform independance: what for? for the 0.1% 1337 linux users? ignore the nolifers, seriously... * lowercase methods (about as worse as the "walk like an egyptian thing) –  Jul 15 '10 at 09:25
  • 2
    now your not only joking but you are just ignorant: linux IS important in the backend area, javadoc is much cleaner than these stupid ms help files which wont work if you view them from network shares. sandcastle is nearly not documented and completely unusable without a proper gui. most ppl will agree that there a really nice collections in the java framework and joshua bloch did a nice job there. And "egghead" books are just books you dont understand. Eclipse is an awesome IDE which VS cant stand without external plugins. Btw: I like C# and miss linq in java. Get out of your ms-office-world. – atamanroman Jul 15 '10 at 09:34
  • @Turing Complete - this answer is **just as bad as yours**. Both answers are just lists of whinges / counter-whinges about the Java and C# ecosystems. These are not pitfalls in the sense that OP asked the question. – Stephen C Jul 15 '10 at 10:17
  • 1
    Only one of these items is a gotcha – finnw Jul 15 '10 at 10:20
  • 1
    -1 from me too, you don't know what a good ide is (and thus don't know much of anything programming related). Edit: Pffft Java more mature, Java doesn't even have real generics.. –  Jul 15 '10 at 10:20
  • 1
    now this answer should fit to the question :P @blindy if you look up what the word mature means, you will see that this statement is correct.Its not necessarily better, but well-prooven and has evolved over time. Thats why some of the features added later are not as good as they could be, but its always easier to learn from others mistakes and start cherrypicking. But i clearly missed the topic here, so i apologize. – atamanroman Jul 15 '10 at 11:01
  • I'm not going to down vote for this, but being honest that 99.9% of .NET code is written in a Microsoft-written IDE of sometype, just roll your mouse over the Interface declaration and tooltip will clearly say that is an interface and not a class that is being inherited. – ben f. May 06 '11 at 17:20
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,180
  • 6
  • 64
  • 87
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
  • 3
  • 3
  • 3
    `Also note that all non-static methods in Java are virtual.` How I wish C# was like this too – Graphain Jul 15 '10 at 07:39
  • 3
    I'm glad it isn't, as it destroys the reason of OOP. With every method being virtual by default you basically enable your whole class to be replaced, which you usually don't want. Also, changing a method from non-final to final can break deriving code, while the other way doesn't. – Femaref Mar 03 '11 at 22:16
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,042
  • 11
  • 42
  • 59
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.

  • not all pitfalls, but pitfalls which are C# programmer are likely to make, in Java :) – Delta76 Jul 15 '10 at 07:36
  • 1
    @Vimvq1987 - there's no reason to suppose that you won't run into the "Java Puzzlers" pitfalls after switching to Java. – Stephen C Jul 15 '10 at 07:39
-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.