Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share

locked by maple_shaft Jul 25 '13 at 16:30

This question exists because it has historical significance, but it is not considered a good, on-topic question for this site, so please do not use it as evidence that you can ask similar questions here. This question and its answers are frozen and cannot be changed. More info: help center.

    
I think this blog covers a lot of things that you are looking for.. ericsink.com/entries/java_eclipse_2.html –  Raze2dust Jul 15 '10 at 7:44
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. –  Roger Pate Jul 15 '10 at 9:42
    
In other words, try asking a "why" or "how" question 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. –  Roger Pate Jul 15 '10 at 10:12
    
Consider making this community-wiki –  finnw Jul 15 '10 at 10:18
    

10 Answers 10

up vote 37 down vote accepted

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

See also

Related questions

On some topics listed above:

On general Java gotchas:

share
    
+1 for the related questions –  finnw Jul 15 '10 at 15:10
8  
Now you can switch on String in Java SE 7. –  Malcolm Aug 20 '11 at 8:38
    
+1 for Generics are non-reified and parameterizable with reference types only, this helped me a lot today –  cctan Apr 10 '12 at 1:42
    
You should also add java has no structs. –  acidzombie24 May 16 '12 at 5:27

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.

share
    
This is an extension of the fact there are no operator overloads. –  Graphain Jul 15 '10 at 7:39
3  
Wrong. Package-private is Java's default access modifier. –  Oliver Weiler Jul 15 '10 at 7:50
    
@Helper Method: Oh, sorry. I meant C# had private as default, but not Java. It's now edited. –  Cloudanger Jul 15 '10 at 7:52

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.

share
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 7:53
  • 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.

share
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 7:39
3  
I'm upvoting this...... even if is a sarcastic low blow. –  Mark Jul 15 '10 at 7:40
3  
@Graphain see stackoverflow.com/questions/2349378/… –  Cloudanger Jul 15 '10 at 7:42
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. –  Cloudanger Jul 15 '10 at 7:59
2  
@cloudanger: agree with you. Pitfalls should be somethings that "work" wrongly, not somethings that does not even work. –  Vimvq1987 Jul 15 '10 at 8:01
  • 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
share
2  
You are joking, right? -1, however. You can't be serious. –  Turing Complete Jul 15 '10 at 9: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 9:21
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 9:34
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.. –  Blindy Jul 15 '10 at 10:20

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!

share
    
+1 - That is some very good advice. –  Kyle Rozendo Dec 30 '10 at 6:57

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

share
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 7: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 3 '11 at 22:16

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.

share

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.

share
    
not all pitfalls, but pitfalls which are C# programmer are likely to make, in Java :) –  Vimvq1987 Jul 15 '10 at 7: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 7:39

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.

share

Not the answer you're looking for? Browse other questions tagged or ask your own question.